Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Torchmoji to convert text into emoji in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Torchmoji to convert text into emoticons in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Installation

The code is not entirely mine, and the source code can be found on this link.

! pip3 install torch==1.0.1-f https://download.pytorch.org/whl/cpu/stable

! git clone https://github.com/huggingface/torchMoji

Import os

Os.chdir ('torchMoji')

! pip3 install-e.

# if you restart the package, the notebook risks to crash on a loop

# I did not restart and worked fine

The code will download about 600 MB of data for training artificial intelligence. I've been using Google Colab. However, I noticed that when the program asked you to restart your laptop to make the required changes, it began to crash in the loop and could not be remedied. If you are using jupyter notebook or colab notepad, do not restart it, regardless of its restart requirements.

! python3 scripts/download_weights.py

This script should download the neural network model that needs to be fine-tuned. When asking, press "Yes" to confirm.

Set conversion function

Using the following function, you can enter text for conversion, which will output the most likely n emoticons (n will be specified).

Import numpy as np

Import emoji, json

From torchmoji.global_variables import PRETRAINED_PATH, VOCAB_PATH

From torchmoji.sentence_tokenizer import SentenceTokenizer

From torchmoji.model_def import torchmoji_emojis

EMOJIS = ": 喜悦:: unamused:: weary:: heart_eyes:: pensive:: ok_hand:: blush:: heart:: smirk:: grin:: notes:: flushed:: sleeping:: relieved:: relaxed:: raised_hands:: two_hearts:: expressionless:: sweat_smile:: pray:: confused:: kissing_heart:: heartbeat:: neutral_face: : information_desk_person:: disappointed:: see_no_evil:: tired_face:: v:: sunglasses:: rage:: thumbsup:: cry:: sleepy:: yum:: triumph:: hand:: mask:: clap:: eyes:: gun:: persevere:: smiling_imp:: sweat:: broken_heart:: yellow_heart:: musical_note:: speak_no_evil:: Wink:: skull:: confounded:: smile:: stuck_out_tongue_winking_eye:: angry:: no_good:: muscle:: facepunch:: purple_heart:: sparkling_heart:: blue_heart:: grimacing:: sparkles: ".split ('')

Model = torchmoji_emojis (PRETRAINED_PATH)

With open (VOCAB_PATH,'r') as f:

Vocabulary = json.load (f)

St = SentenceTokenizer (vocabulary, 30) def deepmojify (sentence,top_n = 5):

Def top_elements (array, k):

Ind = np.argpartition (array,-k) [- k:]

Return ind [np.argsort (Array [ind])] [::-1] tokenized, _, _ = st.tokenize_sentences ([sentence])

Prob = model (tokenized) [0]

Emoji_ids = top_elements (prob, top_n)

Emojis = map (lambda x: EMOJIS [x], emoji_ids)

Return emoji.emojize (f "{sentence} {'.join (emojis)}", use_aliases=True) text experiment text = [' I hate coding AI'] for _ in text:

Print (deepmojify (_, top_n = 3))

Output

As you can see, what is given here is a list, so you can add the number of strings you need.

Primitive neural network

If you don't know how to code and you just want to try, you can use DeepMoji's website: https://deepmoji.mit.edu/

The source code should be exactly the same. In fact, if I enter five emoticons instead of three, this is the result in my code:

Enter a list instead of a sentence

When doing mood analysis, I usually store a database of tweets or comments on Pandas, and I will use the following code to convert a list of strings into Pandas data frames containing a specified number of emojis.

Import pandas as pddef emoji_dataset (list1, n_emoji=3):

Emoji_list = [[x] for x in list1] for _ in range (len (list1)):

For n_emo in range (1, n_emoji+1):

Emoji_ list [_] .append (deepmojify (list1 [_], top_n = n_emoji) [2*-n_emo+1]) emoji_list = pd.DataFrame (emoji_list)

Return emoji_listlist1 = ['Stay safe from the virus',' Push until you breakthroughs,'If it does not challenge you, it will not change you']

I want to estimate the five most likely expressions in this string list:

Emoji_dataset (list1, 5)

On how to use Torchmoji in Python to convert text into emoticons is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report