詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號(hào)
很難找到關(guān)于如何使用Python使用DeepMoji的教程。我已經(jīng)嘗試了幾次,后來(lái)又出現(xiàn)了幾次錯(cuò)誤,于是決定使用替代版本:torchMoji。
TorchMoji是DeepMoji的pyTorch實(shí)現(xiàn),可以在這里找到:https://github.com/huggingface/torchMoji

事實(shí)上,我還沒(méi)有找到一個(gè)關(guān)于如何將文本轉(zhuǎn)換為表情符號(hào)的教程。如果你也沒(méi)找到,那么本文就是一個(gè)了。
安裝
這些代碼并不完全是我的寫(xiě)的,源代碼可以在這個(gè)鏈接上找到。
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
該代碼將下載約600 MB的數(shù)據(jù)用于訓(xùn)練人工智能。我一直在用谷歌Colab。然而,我注意到,當(dāng)程序要求您重新啟動(dòng)筆記本進(jìn)行所需的更改時(shí),它開(kāi)始在循環(huán)中崩潰并且無(wú)法補(bǔ)救。如果你使用的是jupyter notebook或者colab記事本不要重新,不管它的重啟要求就可以了。
python3 scripts/download_weights.py
這個(gè)腳本應(yīng)該下載需要微調(diào)神經(jīng)網(wǎng)絡(luò)模型。詢問(wèn)時(shí),按“是”確認(rèn)。
設(shè)置轉(zhuǎn)換功能函數(shù)
使用以下函數(shù),可以輸入文進(jìn)行轉(zhuǎn)換,該函數(shù)將輸出最可能的n個(gè)表情符號(hào)(n將被指定)。
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 = ":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)
文本實(shí)驗(yàn)
text = ['I hate coding AI']for _ in text: print(deepmojify(_, top_n = 3))
輸出

如您所見(jiàn),這里給出的是個(gè)列表,所以可以添加所需的字符串?dāng)?shù)。
原始神經(jīng)網(wǎng)絡(luò)
如果你不知道如何編碼,你只想試一試,你可以使用DeepMoji的網(wǎng)站:https://deepmoji.mit.edu/

源代碼應(yīng)該完全相同,事實(shí)上,如果我輸入5個(gè)表情符號(hào)而不是3個(gè),這就是我代碼中的結(jié)果:

輸入列表而不是一句話
在進(jìn)行情緒分析時(shí),我通常會(huì)在Pandas上存儲(chǔ)tweets或評(píng)論的數(shù)據(jù)庫(kù),我將使用以下代碼,將字符串列表轉(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']
我想估計(jì)一下這個(gè)字符串列表中最有可能出現(xiàn)的5種表情:
emoji_dataset(list1, 5)

就是這么簡(jiǎn)單
作者:Michelangiolo Mazzeschi
deephub翻譯組
到此這篇關(guān)于詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號(hào)的文章就介紹到這了,更多相關(guān)Python Torchmoji文本轉(zhuǎn)換為表情符號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python之PyQt6對(duì)話框的實(shí)現(xiàn)
這篇文章主要介紹了Python之PyQt6對(duì)話框的實(shí)現(xiàn),文章內(nèi)容詳細(xì),簡(jiǎn)單易懂,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2023-01-01
用Python實(shí)現(xiàn)2024年春晚劉謙魔術(shù)
昨晚春晚上劉謙的兩個(gè)魔術(shù)表演都非常精彩,忍不住用編程去模擬一下這個(gè)過(guò)程,所以本文給大家用Python實(shí)現(xiàn)2024年春晚劉謙魔術(shù),文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
Scrapy 配置動(dòng)態(tài)代理IP的實(shí)現(xiàn)
這篇文章主要介紹了Scrapy 配置動(dòng)態(tài)代理IP的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
python 用for循環(huán)實(shí)現(xiàn)1~n求和的實(shí)例
今天小編就為大家分享一篇python 用for循環(huán)實(shí)現(xiàn)1~n求和的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python爬蟲(chóng)基礎(chǔ)初探selenium
selenium最初是一個(gè)自動(dòng)化測(cè)試工具,而爬蟲(chóng)中使用它主要是為了解決requests無(wú)法執(zhí)行javaScript代碼的問(wèn)題。本文給大家分享Python爬蟲(chóng)基礎(chǔ)selenium知識(shí),包括selenium工作原理,感興趣的朋友跟隨小編一起看看吧2021-05-05
Python使用BeautifulSoup進(jìn)行XPath和CSS選擇器定位
在 Python 中,BeautifulSoup 是一個(gè)常用的 HTML 和 XML 解析庫(kù),它允許我們輕松地定位和提取網(wǎng)頁(yè)中的特定元素,本文將詳細(xì)介紹如何在 BeautifulSoup 中使用 XPath 和 CSS 選擇器定位 HTML 元素,并提供示例代碼以幫助新手理解這些概念,需要的朋友可以參考下2024-11-11
python中的flask框架Jinja?模板入門(mén)教程
這篇文章主要介紹了?python中的flask框架Jinja?模板入門(mén),Jinja?模板其實(shí)是?html?文件,一般情況下放在?Flask?工程的?/templates?目錄下,對(duì)python?flask?Jinja?模板相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04

