這篇文章講到了使用情感詞典進行英文情感分析的方法和代碼講解,非常詳細。
#! /usr/bin/env python2.7
#coding=utf-8
import pickle
import textprocessing as tp
import numpy as np
posdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/posdict.pkl', 'r'))
negdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/negdict.pkl', 'r'))
mostdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/mostdict.pkl', 'r'))
verydict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/verydict.pkl', 'r'))
moredict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/moredict.pkl', 'r'))
ishdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/ishdict.pkl', 'r'))
insufficientdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/insufficentdict.pkl', 'r'))
inversedict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/inversedict.pkl', 'r'))
review = pickle.load(open('D:/code/review_set/review_pkl/Motorala.pkl', 'r'))
def judgeodd(num):
if (num/2)*2 == num:
return 'even'
else:
return 'odd'
def sentiment_score_list(dataset):
cuted_data = []
for cell in dataset:
cuted_data.append(tp.cut_sentence(cell))
count1 = []
count2 = []
for sents in cuted_data: #循環遍歷每一個評論
for sent in sents: #循環遍歷評論中的每一個分句
segtmp = tp.segmentation(sent, 'list') #把句子進行分詞,以列表的形式返回
i = 0 #記錄掃描到的詞的位置
a = 0 #記錄情感詞的位置
poscount = 0 #積極詞的第一次分值
poscount2 = 0 #積極詞反轉后的分值
poscount3 = 0 #積極詞的最后分值(包括嘆號的分值)
negcount = 0
negcount2 = 0
negcount3 = 0
for word in segtmp:
if word in posdict: #判斷詞語是否是情感詞
poscount += 1
c = 0
for w in segtmp[a:i]: #掃描情感詞前的程度詞
if w in mostdict:
poscount *= 4.0
elif w in verydict:
poscount *= 3.0
elif w in moredict:
poscount *= 2.0
elif w in ishdict:
poscount /= 2.0
elif w in insufficientdict:
poscount /= 4.0
elif w in inversedict:
c += 1
if judgeodd(c) == 'odd': #掃描情感詞前的否定詞數
poscount *= -1.0
poscount2 += poscount
poscount = 0
poscount3 = poscount + poscount2 + poscount3
poscount2 = 0
else:
poscount3 = poscount + poscount2 + poscount3
poscount = 0
a = i + 1 #情感詞的位置變化
elif word in negdict: #消極情感的分析,與上面一致
negcount += 1
d = 0
for w in segtmp[a:i]:
if w in mostdict:
negcount *= 4.0
elif w in verydict:
negcount *= 3.0
elif w in moredict:
negcount *= 2.0
elif w in ishdict:
negcount /= 2.0
elif w in insufficientdict:
negcount /= 4.0
elif w in inversedict:
d += 1
if judgeodd(d) == 'odd':
negcount *= -1.0
negcount2 += negcount
negcount = 0
negcount3 = negcount + negcount2 + negcount3
negcount2 = 0
else:
negcount3 = negcount + negcount2 + negcount3
negcount = 0
a = i + 1
elif word == '!'.decode('utf8') or word == '!'.decode('utf8'): ##判斷句子是否有感嘆號
for w2 in segtmp[::-1]: #掃描感嘆號前的情感詞,發現后權值+2,然后退出循環
if w2 in posdict or negdict:
poscount3 += 2
negcount3 += 2
break
i += 1 #掃描詞位置前移
#以下是防止出現負數的情況
pos_count = 0
neg_count = 0
if poscount3 < 0 and negcount3 > 0:
neg_count += negcount3 - poscount3
pos_count = 0
elif negcount3 < 0 and poscount3 > 0:
pos_count = poscount3 - negcount3
neg_count = 0
elif poscount3 < 0 and negcount3 < 0:
neg_count = -poscount3
pos_count = -negcount3
else:
pos_count = poscount3
neg_count = negcount3
count1.append([pos_count, neg_count])
count2.append(count1)
count1 = []
return count2
def 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])
AvgNeg = np.mean(score_array[:,1])
StdPos = np.std(score_array[:,0])
StdNeg = np.std(score_array[:,1])
score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg])
return score