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

歡迎訪問 生活随笔!

生活随笔

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

python

NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码

發布時間:2025/3/21 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

NLP之情感分析:基于python編程(jieba庫)實現中文文本情感分析(得到的是情感評分)之全部代碼

?

?

目錄

全部代碼


?

相關文章
NLP之情感分析:基于python編程(jieba庫)實現中文文本情感分析(得到的是情感評分)
NLP之情感分析:基于python編程(jieba庫)實現中文文本情感分析(得到的是情感評分)之全部代碼

?

全部代碼

# coding:utf-8 import jieba import numpy as np#打開詞典文件,返回列表 def open_dict(Dict = 'hahah', path=r'data/Textming'):path = path + '%s.txt' % Dictdictionary = open(path, 'r', encoding='utf-8')dict = []for word in dictionary:word = word.strip('\n')dict.append(word)return dictdef judgeodd(num):if (num % 2) == 0:return 'even'else:return 'odd'#注意,這里你要修改path路徑。 deny_word = open_dict(Dict = '否定詞', path= r'F:/File_Python/Resources/data/Textming/') posdict = open_dict(Dict = 'positive', path= r'F:/File_Python/Resources/data/Textming/') negdict = open_dict(Dict = 'negative', path= r'F:/File_Python/Resources/data/Textming/')degree_word = open_dict(Dict = '程度級別詞語', path= r'F:/File_Python/Resources/data/Textming/') mostdict = degree_word[degree_word.index('extreme')+1 : degree_word.index('very')]#權重4,即在情感詞前乘以4 verydict = degree_word[degree_word.index('very')+1 : degree_word.index('more')]#權重3 moredict = degree_word[degree_word.index('more')+1 : degree_word.index('ish')]#權重2 ishdict = degree_word[degree_word.index('ish')+1 : degree_word.index('last')]#權重0.5def sentiment_score_list(dataset):seg_sentence = dataset.split('。')count1 = []count2 = []for sen in seg_sentence: #循環遍歷每一個評論segtmp = jieba.lcut(sen, cut_all=False) #把句子進行分詞,以列表的形式返回i = 0 #記錄掃描到的詞的位置a = 0 #記錄情感詞的位置poscount = 0 #積極詞的第一次分值poscount2 = 0 #積極詞反轉后的分值poscount3 = 0 #積極詞的最后分值(包括嘆號的分值)negcount = 0negcount2 = 0negcount3 = 0for word in segtmp:if word in posdict: # 判斷詞語是否是情感詞poscount += 1c = 0for w in segtmp[a:i]: # 掃描情感詞前的程度詞if w in mostdict:poscount *= 4.0elif w in verydict:poscount *= 3.0elif w in moredict:poscount *= 2.0elif w in ishdict:poscount *= 0.5elif w in deny_word:c += 1if judgeodd(c) == 'odd': # 掃描情感詞前的否定詞數poscount *= -1.0poscount2 += poscountposcount = 0poscount3 = poscount + poscount2 + poscount3poscount2 = 0else:poscount3 = poscount + poscount2 + poscount3poscount = 0a = i + 1 # 情感詞的位置變化elif word in negdict: # 消極情感的分析,與上面一致negcount += 1d = 0for w in segtmp[a:i]:if w in mostdict:negcount *= 4.0elif w in verydict:negcount *= 3.0elif w in moredict:negcount *= 2.0elif w in ishdict:negcount *= 0.5elif w in degree_word:d += 1if judgeodd(d) == 'odd':negcount *= -1.0negcount2 += negcountnegcount = 0negcount3 = negcount + negcount2 + negcount3negcount2 = 0else:negcount3 = negcount + negcount2 + negcount3negcount = 0a = i + 1elif word == '!' or word == '!': ##判斷句子是否有感嘆號for w2 in segtmp[::-1]: # 掃描感嘆號前的情感詞,發現后權值+2,然后退出循環if w2 in posdict or negdict:poscount3 += 2negcount3 += 2breaki += 1 # 掃描詞位置前移# 以下是防止出現負數的情況pos_count = 0neg_count = 0if poscount3 < 0 and negcount3 > 0:neg_count += negcount3 - poscount3pos_count = 0elif negcount3 < 0 and poscount3 > 0:pos_count = poscount3 - negcount3neg_count = 0elif poscount3 < 0 and negcount3 < 0:neg_count = -poscount3pos_count = -negcount3else:pos_count = poscount3neg_count = negcount3count1.append([pos_count, neg_count])count2.append(count1)count1 = []return count2def sentiment_score(senti_score_list):score = []for review in senti_score_list:score_array = np.array(review)Pos = np.sum(score_array[:, 0])Neg = np.sum(score_array[:, 1])AvgPos = np.mean(score_array[:, 0])AvgPos = float('%.1f'%AvgPos)AvgNeg = np.mean(score_array[:, 1])AvgNeg = float('%.1f'%AvgNeg)StdPos = np.std(score_array[:, 0])StdPos = float('%.1f'%StdPos)StdNeg = np.std(score_array[:, 1])StdNeg = float('%.1f'%StdNeg)score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg]) #積極、消極情感值總和(最重要),積極、消極情感均值,積極、消極情感方差。return scoredef EmotionByScore(data):result_list=sentiment_score(sentiment_score_list(data))return result_list[0][0],result_list[0][1]def JudgingEmotionByScore(Pos, Neg):if Pos > Neg:str='1'elif Pos < Neg:str='-1'elif Pos == Neg:str='0'return strdata1= '今天上海的天氣真好!我的心情非常高興!如果去旅游的話我會非常興奮!和你一起去旅游我會更加幸福!' data2= '救命,你是個壞人,救命,你不要碰我,救命,你個大壞蛋!' data3= '美國華裔科學家,祖籍江蘇揚州市高郵縣,生于上海,斯坦福大學物理系,電子工程系和應用物理系終身教授!'print(sentiment_score(sentiment_score_list(data1))) print(sentiment_score(sentiment_score_list(data2))) print(sentiment_score(sentiment_score_list(data3)))a,b=EmotionByScore(data1)emotion=JudgingEmotionByScore(a,b) print(emotion)

?

總結

以上是生活随笔為你收集整理的NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码的全部內容,希望文章能夠幫你解決所遇到的問題。

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