生活随笔
收集整理的這篇文章主要介紹了
python3---情感分析(基于词典中文)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫在前面:
現有的情感分析比較常用的有兩種,分別是基于詞典的和機器學習,前者也屬于非監督學習,后者自然一般屬于監督學習。
剛開始學情感分析,下面先從**【基于詞典的情感分析】**開始進行:
-
詞典:我東搜西找找到了一些感覺是常用的字典,主要有(臺灣大學NTUSD簡體中文情感詞典,清華大學李軍中文褒貶義詞典,BosonNLP_sentiment_score,知網hownet2007)
-
詞典下載傳送門
(積fen少的學生黨可以評論留言【年級+郵箱】,看到會發送滴)
-
因為剛學,所以設計了一些比較基礎的規則(基于文本預處理之后生成了關于每一個文檔的【詞列表向量】)
代碼實現:
-
加載詞典(我主要用的是NTUSD的中文情感極性詞典):
def dict_load(path
):dict=[]with open(path
, encoding
='utf-8-sig') as f
:for line
in f
:if line
.strip
() !='':dict.append
(line
.strip
())return(dict)
neg_dict
= []
pos_dict
= []
no_dict
= []
dict_file_path
='XXXXXX\\'
pos_dict
=dict_load
(dict_file_path
+'臺灣大學NTUSD簡體中文情感詞典/ntusd-positive.txt')
print("==pos_dict loaded successfully==")
neg_dict
=dict_load
(dict_file_path
+'臺灣大學NTUSD簡體中文情感詞典/ntusd-negative.txt')
print("==neg_dict loaded successfully==")
no_dict
=dict_load
(dict_file_path
+'否定詞典\\否定.txt')
print("==no_dict loaded successfully==")
- 把之前的規則碼出來【確定以下想要的輸入的格式,以下輸入的單個文檔的分詞列表】
def sent_count(sent
, negdict
, posdict
, nodict
):pos
= 0neg
= 0for i
in range(len(sent
)):if sent
[i
] in negdict
:if i
==1 and sent
[i
-1] in nodict
:pos
=pos
+1 elif i
==1 and sent
[i
-1] not in nodict
:neg
=neg
+1 elif i
>1 and sent
[i
-1] in nodict
:if sent
[i
-2] in nodict
:neg
=neg
+1 else: pos
=pos
+1 elif i
>1 and sent
[i
-1] not in nodict
:if sent
[i
-2] in nodict
:pos
=pos
+1 else:neg
=neg
+1 elif sent
[i
] in posdict
:if i
==1 and sent
[i
-1] in nodict
:neg
=neg
+1 elif i
==1 and sent
[i
-1] not in nodict
:pos
=pos
+1 elif i
>1 and sent
[i
-1] in nodict
:if sent
[i
-2] in nodict
:pos
=pos
+1 else: neg
=neg
+1 elif i
>1 and sent
[i
-1] not in nodict
:if sent
[i
-2] in nodict
:neg
=neg
+1 else:pos
=pos
+1 return pos
, neg
- 如果想要更加快速的分析整個文本數據(有多個文檔組成),可以用下面的,多加了一層循環:
def sent_count(sents
, negdict
, posdict
, nodict
):pos_list
= []neg_list
= []for sent
in sents
:pos
=0neg
=0for i
in range(len(sent
)):if sent
[i
] in negdict
:if i
==1 and sent
[i
-1] in nodict
:pos
=pos
+1 elif i
==1 and sent
[i
-1] not in nodict
:neg
=neg
+1 elif i
>1 and sent
[i
-1] in nodict
:if sent
[i
-2] in nodict
:neg
=neg
+1 else: pos
=pos
+1 elif i
>1 and sent
[i
-1] not in nodict
:if sent
[i
-2] in nodict
:pos
=pos
+1 else:neg
=neg
+1 elif sent
[i
] in posdict
:if i
==1 and sent
[i
-1] in nodict
:neg
=neg
+1 elif i
==1 and sent
[i
-1] not in nodict
:pos
=pos
+1 elif i
>1 and sent
[i
-1] in nodict
:if sent
[i
-2] in nodict
:pos
=pos
+1 else: neg
=neg
+1 elif i
>1 and sent
[i
-1] not in nodict
:if sent
[i
-2] in nodict
:neg
=neg
+1 else:pos
=pos
+1 pos_list
.append
(pos
)neg_list
.append
(neg
)return pos_list
, neg_list
寫在后面:
剛開始學,如有不當,請好言勸我呀~~
~~歡迎 批評 指教~
有更好的方法和規則期待分享,提前感謝!
總結
以上是生活随笔為你收集整理的python3---情感分析(基于词典中文)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。