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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

机器学习算法Python实现:基于情感词典的文本情感分析

發(fā)布時(shí)間:2024/1/17 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习算法Python实现:基于情感词典的文本情感分析 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# -*- coding:utf-8 -* #本代碼是在jupyter notebook上實(shí)現(xiàn),author:huzhifei, create time:2018/8/14 #本腳本主要實(shí)現(xiàn)了基于python通過(guò)已有的情感詞典對(duì)文本數(shù)據(jù)做的情感分析的項(xiàng)目目的#導(dǎo)入對(duì)應(yīng)的包及相關(guān)的自定義的jieba詞典 import jieba import numpy as np jieba.load_userdict("C:\\Users\\Desktop\\中文分詞詞庫(kù)整理\\中文分詞詞庫(kù)整理\\百度分詞詞庫(kù).txt") # 打開詞典文件,返回列表 def open_dict(Dict='hahah',path = 'C:\\Users\\Desktop\\Textming\\'):path = path + '%s.txt' %Dictdictionary = open(path, 'r', encoding='utf-8',errors='ignore')dict = []for word in dictionary:word = word.strip('\n')dict.append(word)return dictdef judgeodd(num): #往情感詞前查找否定詞,找完全部否定詞,若數(shù)量為奇數(shù),乘以-1,若數(shù)量為偶數(shù),乘以1.if num % 2 == 0:return 'even'else:return 'odd'deny_word = open_dict(Dict='deny')#否定詞詞典 posdict = open_dict(Dict='positive')#積極情感詞典 negdict = open_dict(Dict = 'negative')#消極情感詞典degree_word = open_dict(Dict = 'degree',path='C:\\Users\\AAS-1413\\Desktop\\Textming\\')#程度詞詞典#為程度詞設(shè)置權(quán)重 mostdict = degree_word[degree_word.index('extreme')+1: degree_word.index('very')] #權(quán)重4,即在情感前乘以3 verydict = degree_word[degree_word.index('very')+1: degree_word.index('more')] #權(quán)重3 moredict = degree_word[degree_word.index('more')+1: degree_word.index('ish')]#權(quán)重2 ishdict = degree_word[degree_word.index('ish')+1: degree_word.index('last')]#權(quán)重0.5 seg_sentence=[]def sentiment_score_list(data):for i in data:seg_sentence.append(i.replace(' ',','))#去除逗號(hào)后的評(píng)論數(shù)據(jù)集#seg_sentence=data.replace(' ',',').split(',')#以逗號(hào)分隔count1 = []count2 = []for sen in seg_sentence:#print(sen)# 循環(huán)遍歷每一個(gè)評(píng)論segtmp = jieba.lcut(sen, cut_all=False) # 把句子進(jìn)行分詞,以列表的形式返回#print(segtmp)i = 0 #記錄掃描到的詞的位置a = 0 #記錄情感詞的位置poscount = 0 # 積極詞的第一次分值poscount2 = 0 # 積極反轉(zhuǎn)后的分值poscount3 = 0 # 積極詞的最后分值(包括嘆號(hào)的分值)negcount = 0negcount2 = 0negcount3 = 0for word in segtmp:if word in posdict: # 判斷詞語(yǔ)是否是積極情感詞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': # 掃描情感詞前的否定詞數(shù)poscount *= -1.0poscount2 += poscountposcount = 0poscount3 = poscount + poscount2 + poscount3poscount2 = 0else:poscount3 = poscount + poscount2 + poscount3poscount = 0a = i+1elif 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 == '!': # 判斷句子是否有感嘆號(hào)for w2 in segtmp[::-1]: # 掃描感嘆號(hào)前的情感詞,發(fā)現(xiàn)后權(quán)值+2,然后退出循環(huán)if w2 in posdict:poscount3 += 2elif w2 in negdict:negcount3 += 2else:poscount3 +=0negcount3 +=0breakelse:poscount3=0negcount3=0i += 1# 以下是防止出現(xiàn)負(fù)數(shù)的情況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 = -pos_countpos_count = -neg_countelse:pos_count = poscount3neg_count = negcount3count1.append([pos_count,neg_count]) #返回每條評(píng)論打分后的列表#print(count1)count2.append(count1)count1=[]#print(count2)return count2 #返回所有評(píng)論打分后的列表def sentiment_score(senti_score_list):#分析完所有評(píng)論后,正式對(duì)每句評(píng)論打情感分#score = []s=''w=''for review in senti_score_list:#senti_score_list#print(review)score_array = np.array(review)#print(score_array)Pos = np.sum(score_array[:,0])#積極總分Neg = np.sum(score_array[:,1])#消極總分AvgPos = np.mean(score_array[:,0])#積極情感均值A(chǔ)vgPos = float('%.lf' % AvgPos)AvgNeg = np.mean(score_array[:, 1])#消極情感均值A(chǔ)vgNeg = float('%.1f' % AvgNeg)StdPos = np.std(score_array[:, 0])#積極情感方差StdPos = float('%.1f' % StdPos)StdNeg = np.std(score_array[:, 1])#消極情感方差StdNeg = float('%.1f' % StdNeg)#s+=([Pos,Neg,AvgPos,AvgNeg,StdPos,StdNeg]))s+='\n'+str([Pos, Neg])#score.append([Pos,Neg])res=Pos-Negif res>0:w+='\n'+'好評(píng)'print ('該條評(píng)論是:好評(píng)')elif res<0:w+='\n'+'差評(píng)'print ('該條評(píng)論是:差評(píng)')else:w+='\n'+'中評(píng)'print ('該條評(píng)論是:中評(píng)')#print(w)return w#讀取要做情感分析的文本 data=open("content.txt","r",errors='ignore')#調(diào)用函數(shù)做實(shí)體分析 sentiment_score(sentiment_score_list(data))#將函數(shù)返回結(jié)果存入txt中 f=open('s.txt','w',errors='ignore') f.write(sentiment_score(sentiment_score_list(data))) f.close()

總結(jié)

以上是生活随笔為你收集整理的机器学习算法Python实现:基于情感词典的文本情感分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。