日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3---情感分析(基于词典中文)

發布時間:2023/12/9 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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) print("==pos_dict loaded successfully==") neg_dict=dict_load(dict_file_path+'臺灣大學NTUSD簡體中文情感詞典/ntusd-negative.txt') #print(neg_dict) print("==neg_dict loaded successfully==") no_dict=dict_load(dict_file_path+'否定詞典\\否定.txt') #print(no_dict) print("==no_dict loaded successfully==") #加載情感詞典結束'''
  • 把之前的規則碼出來【確定以下想要的輸入的格式,以下輸入的單個文檔的分詞列表】
#定義一個函數來判斷句子中積極詞、消極詞詞頻 #===============#sent是分好詞的列表格式或者序列格式==================== 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
  • 如果想要更加快速的分析整個文本數據(有多個文檔組成),可以用下面的,多加了一層循環:
#==sents是list of list,sent 是分好詞的列表格式或者序列格式============= #判斷句子中積極詞、消極詞詞頻 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---情感分析(基于词典中文)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。