詳解在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í)上,我還沒有找到一個(gè)關(guān)于如何將文本轉(zhuǎn)換為表情符號(hào)的教程。如果你也沒找到,那么本文就是一個(gè)了。
安裝
這些代碼并不完全是我的寫的,源代碼可以在這個(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í),它開始在循環(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))
輸出
如您所見,這里給出的是個(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)文章
Pycharm安裝第三方庫(kù)時(shí)Non-zero exit code錯(cuò)誤解決辦法
這篇文章主要介紹了Pycharm安裝第三方庫(kù)時(shí)Non-zero exit code錯(cuò)誤解決辦法,最好的解決辦法可以通過(guò)“Pycharm”左下角的“Terminal”,在pycharm內(nèi)使用pip安裝,以安裝“requests”為例,需要的朋友可以參考下2023-01-01Python實(shí)現(xiàn)雙軸組合圖表柱狀圖和折線圖的具體流程
這篇文章主要介紹了Python雙軸組合圖表柱狀圖+折線圖,Python繪制雙軸組合的關(guān)鍵在plt庫(kù)的twinx()函數(shù),具體實(shí)例代碼跟隨小編一起看看吧2021-08-08Python實(shí)現(xiàn)隨機(jī)生成一個(gè)漢字的方法分享
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)隨機(jī)生成一個(gè)漢字的功能,文中的示例代碼講解詳細(xì),對(duì)我們深入了解Python有一定的幫助,需要的可以參考一下2023-01-01Python利用CNN實(shí)現(xiàn)對(duì)時(shí)序數(shù)據(jù)進(jìn)行分類
這篇文章主要為大家詳細(xì)介紹了Python如何利用CNN實(shí)現(xiàn)對(duì)時(shí)序數(shù)據(jù)進(jìn)行分類功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02Python while true實(shí)現(xiàn)爬蟲定時(shí)任務(wù)
這篇文章主要介紹了Python爬蟲定時(shí)任務(wù)簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Python2和Python3讀取文本文件的區(qū)別及說(shuō)明
這篇文章主要介紹了Python2和Python3讀取文本文件的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02