詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號
很難找到關(guān)于如何使用Python使用DeepMoji的教程。我已經(jīng)嘗試了幾次,后來又出現(xiàn)了幾次錯誤,于是決定使用替代版本:torchMoji。
TorchMoji是DeepMoji的pyTorch實現(xiàn),可以在這里找到:https://github.com/huggingface/torchMoji
事實上,我還沒有找到一個關(guān)于如何將文本轉(zhuǎn)換為表情符號的教程。如果你也沒找到,那么本文就是一個了。
安裝
這些代碼并不完全是我的寫的,源代碼可以在這個鏈接上找到。
pip3 install torch==1.0.1 -f https://download.pytorch.org/whl/cpu/stable git clone https://github.com/huggingface/torchMojiimport osos.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
該代碼將下載約600 MB的數(shù)據(jù)用于訓(xùn)練人工智能。我一直在用谷歌Colab。然而,我注意到,當程序要求您重新啟動筆記本進行所需的更改時,它開始在循環(huán)中崩潰并且無法補救。如果你使用的是jupyter notebook或者colab記事本不要重新,不管它的重啟要求就可以了。
python3 scripts/download_weights.py
這個腳本應(yīng)該下載需要微調(diào)神經(jīng)網(wǎng)絡(luò)模型。詢問時,按“是”確認。
設(shè)置轉(zhuǎn)換功能函數(shù)
使用以下函數(shù),可以輸入文進行轉(zhuǎn)換,該函數(shù)將輸出最可能的n個表情符號(n將被指定)。
import numpy as npimport emoji, jsonfrom torchmoji.global_variables import PRETRAINED_PATH, VOCAB_PATHfrom torchmoji.sentence_tokenizer import SentenceTokenizerfrom torchmoji.model_def import torchmoji_emojis EMOJIS = ':joy: :unamused: :weary: :sob: :heart_eyes: :pensive: :ok_hand: :blush: :heart: :smirk: :grin: :notes: :flushed: :100: :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 = [’I hate coding AI’]for _ in text: print(deepmojify(_, top_n = 3))
輸出
如您所見,這里給出的是個列表,所以可以添加所需的字符串數(shù)。
原始神經(jīng)網(wǎng)絡(luò)
如果你不知道如何編碼,你只想試一試,你可以使用DeepMoji的網(wǎng)站:https://deepmoji.mit.edu/
源代碼應(yīng)該完全相同,事實上,如果我輸入5個表情符號而不是3個,這就是我代碼中的結(jié)果:
輸入列表而不是一句話
在進行情緒分析時,我通常會在Pandas上存儲tweets或評論的數(shù)據(jù)庫,我將使用以下代碼,將字符串列表轉(zhuǎn)換為Pandas數(shù)據(jù)幀,其中包含指定數(shù)量的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 break!’, ’If it does not challenge you, it will not change you’]
我想估計一下這個字符串列表中最有可能出現(xiàn)的5種表情:
emoji_dataset(list1, 5)
就是這么簡單
作者:Michelangiolo Mazzeschi
deephub翻譯組
到此這篇關(guān)于詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號的文章就介紹到這了,更多相關(guān)Python Torchmoji文本轉(zhuǎn)換為表情符號內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
