使用 NRC 詞典進行 Python 情感分類


情感識別情感辨識是指個人或物體感知環境中表現出的特定情感並將其歸入眾多情感類別之一的能力。

Python 中的情感分類是傳統情感分析技術的可行替代方案,傳統情感分析技術將單詞或句子標記為正面負面,並相應地分配極性得分

該演算法背後的基本思想是模仿人類思維過程,並試圖從文字中分割出表達情感的單詞。分析是使用訓練資料集進行的,其中將一組預先假設的資訊饋送到系統,作為分類的基礎。

這是一個基於NLTK 庫的 WordNet 同義詞集和加拿大國家研究委員會 (NRC)的情感詞典的軟體包,其中包含超過27,000 個術語

庫使用以下類別來衡量和分類單詞的情感影響:

  • 恐懼

  • 憤怒

  • 預期

  • 信任

  • 驚訝

  • 正面

  • 負面

  • 悲傷

  • 厭惡

  • 快樂

安裝步驟

  • 步驟 1 - 使用終端中的 pip install 命令安裝 NRC 模組。

pip install NRCLex

在 Jupyter

筆記本命令提示符中安裝通常遵循相同的步驟(如果您使用的是 Windows)。

在 macOS 中安裝也遵循相同的命令。直接使用終端。

  • 步驟 2 - 除了nrclex 之外,還要安裝 textblob 以避免遇到MissingCorpusError

pip install textblob
  • 步驟 3 - 從 textblob 下載語料庫

python -m textblob.download_corpora

安裝完成後,我們可以繼續匯入庫並建立文字物件。

基本方法

1. 將原始文字轉換為過濾後的文字(為了獲得最佳結果,“text” 應為 Unicode)。

text_object.load_raw_text(text: str)

2. 將標記化的單詞列表轉換為標記列表。

text_object.load_token_list(list_of_tokens: list)

3. 返回單詞列表。

text_object.words

4. 返回句子列表。

text_object.sentences

5. 返回情感列表。

text_object.affect_list

6. 返回情感字典。

text_object.affect_dict

7. 返回原始情感計數。

text_object.raw_emotion_scores

8. 返回最高情感。

text_object.top_emotions

9. 返回頻率。

Text_object.frequencies

在這裡,我們使用了 top_emotions 函式根據情感對單詞列表進行分類。

演算法

步驟 1 - 匯入 nrclex 匯入 nrclex

步驟 2 - 從 nrclex 匯入 NRCLex

步驟 3 - 初始化要分類的字串-單詞列表

步驟 4 - 對於範圍內的 i len(text)

步驟 4 - emotion = NRCLex(text[i]) # 為每個文字建立一個物件

步驟 5 - emotion.top_emotions # 對情感進行分類

示例

# Import module
import nrclex
from nrclex import NRCLex

text = ['happy', 'beautiful', 'exciting', 'depressed']

# Iterate through list
for i in range(len(text)):

   # call by object creation
   emotion = NRCLex(text[i])

   # Classify emotion
   print('\n', text[i], ': ', emotion.top_emotions) 

輸出

innocent : [('trust', 0.5), ('positive', 0.5)]
hate : [('fear', 0.2), ('anger', 0.2), ('negative', 0.2), ('sadness', 0.2), ('disgust', 0.2)]
irritating : [('anger', 0.3333333333333333), ('negative', 0.3333333333333333), 
('disgust', 0.3333333333333333)]
annoying : [('anger', 0.5), ('negative', 0.5)]

演算法

步驟 1 - 匯入 nrclex

步驟 2 - 從 nrclex 匯入 NRCLex

步驟 3 - 初始化要分類的字串-單詞列表

步驟 4 - 對於範圍內的 i len(text)

步驟 4 - emotion = NRCLex(text[i]) # 為每個文字建立一個物件

步驟 5 - emotion.top_emotions # 對情感進行分類

示例

import nrclex
from nrclex import NRCLex
 
# Assign list of strings
text = ['innocent','hate', 'irritating','annoying']
 
# Iterate through list
for i in range(len(text)):
 
   # Create object
   emotion = NRCLex(text[i])

   # Classify emotion
   print('\n\n', text[i], ': ', emotion.top_emotions) 

輸出

innocent :  [('trust', 0.5), ('positive', 0.5)] 
 hate :  [('fear', 0.2), ('anger', 0.2), ('negative', 0.2), ('sadness', 0.2), ('disgust', 0.2)] 
irritating :  [('anger', 0.3333333333333333), ('negative', 0.3333333333333333), ('disgust', 0.3333333333333333)] 
 annoying :  [('anger', 0.5), ('negative', 0.5)] 

結論

NRC 情感詞典廣泛應用於研究和行業的情感分析和情感分類任務。這意味著有龐大的使用者和資源社群可提供支援和進一步開發。藉助谷歌翻譯,NRCLex 還成功打破了語言障礙,為全球 100 多種語言提供了穩定的輸出。這在醫療保健領域理解疫情應對方面有著廣泛的應用。實際應用包括心理學和行為科學、虛假新聞檢測以及增強人機互動。

更新於:2023 年 5 月 22 日

1K+ 閱讀量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告