泰迪学院-Python实战
公众号ID:taidixueyuan
还未阅读第一篇的小可爱,在这里可以直接跳转
构建模型-评论数据情感倾向分析
1、 匹配情感词
情感倾向也称为情感极性。在某商品评论中,可以理解为用户对该商品表达自身观点所持的态度是支持、反对还是中立,即通常所指的正面情感、负面情感、中性情感。由于本案例主要是对产品的优缺点进行分析,因此只要确定用户评论信息中的情感倾向方向分析即可,不需要分析每一评论的情感程度。
对评论情感倾向进行分析首先对情感词进行匹配,主要采用词典匹配的方法,本案例使用的情感词表是2007年10月22日知网发布“情感分析用词语集(beta版)”,主要使用“中文正面评价”词表、“中文负面评价”“中文正面情感”“中文负面情感”词表。将“中文正面评论”“中文正面情感”两个词表合并,并给每个词语赋予初始权重1,作为本案例的正面评论情感词表。将“中文负面评价”“中文负面情感”两个词表合并,并给每个词语赋予初始权重-1,作为本案例的负面评论情感词表。
一般基于词表的情感分析方法,分析的效果往往与情感词表内的词语有较强的相关性,如果情感词表内的词语足够全面,并且词语符合该案例场景下所表达情感,那么情感分析的效果会更好。针对本案例场景,需要在知网提供的词表基础上进行优化,例如“好评”“超值”“差评”“五分”等词只有在网络购物评论上出现,就可以根据词语的情感倾向添加至对应的情感词表内。将“满意”“好评”“很快”“还好”“还行”“超值”“给力”“支持”“超好”“感谢”“太棒了”“厉害”“挺舒服”“辛苦”“完美”“喜欢”“值得”“省心”等词添加进正面情感词表。将“差评”“贵”“高”“漏水”等词加入负面情感词表。
读入正负面评论情感词表,正面词语赋予初始权重1,负面词语赋予初始权重-1。使用merge函数将按照词语将情感词表与分词结果进行匹配,如代码清单1所示。
代码清单1 匹配情感词
import pandas as pdimport numpy as npword = pd.read_csv("../tmp/word.csv")# 读入正面、负面情感评价词pos_comment = pd.read_csv("../data/正面评价词语(中文).txt", header=None,sep="\n",encoding = 'utf-8', engine='python')neg_comment = pd.read_csv("../data/负面评价词语(中文).txt", header=None,sep="\n",encoding = 'utf-8', engine='python')pos_emotion = pd.read_csv("../data/正面情感词语(中文).txt", header=None,sep="\n",encoding = 'utf-8', engine='python')neg_emotion = pd.read_csv("../data/负面情感词语(中文).txt", header=None,sep="\n",encoding = 'utf-8', engine='python')# 合并情感词与评价词positive = set(pos_comment.iloc[:,0])|set(pos_emotion.iloc[:,0])negative = set(neg_comment.iloc[:,0])|set(neg_emotion.iloc[:,0])intersection = positive&negative # 正负面情感词表中相同的词语positive = list(positive - intersection)negative = list(negative - intersection)positive = pd.DataFrame({"word":positive,"weight":[1]*len(positive)})negative = pd.DataFrame({"word":negative,"weight":[-1]*len(negative)})posneg = positive.append(negative)# 将分词结果与正负面情感词表合并,定位情感词data_posneg = posneg.merge(word, left_on = 'word', right_on = 'word',how = 'right')data_posneg = data_posneg.sort_values(by = ['index_content','index_word'])
*代码请联系客服领取,联系方式见文末
(2) 修正情感倾向
情感方向修正主要根据情感词前面2个位置的词语是否存在否定词而去判断情感值的正确与否,由于汉语中存在多重否定现象,即当否定词出现奇数次时,表示否定意思;当否定词出现偶数次时,表示肯定意思。按照汉语习惯,搜索每个情感词前2个词语,若出现奇数否定词,则调整为相反的情感极性。
本案例使用的否定词表共有19个否定词,分别为:不、没、无、非、莫、弗、毋、未、否、别、無、休、不是、不能、不可、没有、不用、不要、从没、不太。
读入否定词表,对情感值的方向进行修正。计算每条评论的情感得分,将评论分为正面评论和负面评论,并计算情感分析的准确率,如代码清单2所示。
代码清单2 修正情感倾向
# 根据情感词前时候有否定词或双层否定词对情感值进行修正# 载入否定词表notdict = pd.read_csv("../data/not.csv")# 处理否定修饰词data_posneg['amend_weight'] = data_posneg['weight'] # 构造新列,作为经过否定词修正后的情感值data_posneg['id'] = np.arange(0, len(data_posneg))only_inclination = data_posneg.dropna() # 只保留有情感值的词语only_inclination.index = np.arange(0, len(only_inclination))index = only_inclination['id']for i in np.arange(0, len(only_inclination)):review = data_posneg[data_posneg['index_content'] ==only_inclination['index_content'][i]] # 提取第i个情感词所在的评论review.index = np.arange(0, len(review))affective = only_inclination['index_word'][i] # 第i个情感值在该文档的位置if affective == 1:ne = sum([i in notdict['term'] for i in review['word'][affective - 1]])if ne == 1:data_posneg['amend_weight'][index[i]] = -\data_posneg['weight'][index[i]]elif affective > 1:ne = sum([i in notdict['term'] for i in review['word'][[affective - 1,affective - 2]]])if ne == 1:data_posneg['amend_weight'][index[i]] = -\data_posneg['weight'][index[i]]# 更新只保留情感值的数据only_inclination = only_inclination.dropna()# 计算每条评论的情感值emotional_value = only_inclination.groupby(['index_content'],as_index=False)['amend_weight'].sum()# 去除情感值为0的评论emotional_value = emotional_value[emotional_value['amend_weight'] != 0]
*代码请联系客服领取,联系方式见文末
(3) 查看情感分析效果
使用wordcloud包下的WordCloud函数分别对正面评论和负面评论绘制词云,查看情感分析效果,如代码清单3所示。
代码清单3 查看情感分析效果
# 给情感值大于0的赋予评论类型(content_type)为pos,小于0的为negemotional_value['a_type'] = ''emotional_value['a_type'][emotional_value['amend_weight'] > 0] = 'pos'emotional_value['a_type'][emotional_value['amend_weight'] < 0] = 'neg'# 查看情感分析结果result = emotional_value.merge(word,left_on='index_content',right_on='index_content',how='left')result = result[['index_content','content_type', 'a_type']].drop_duplicates()confusion_matrix = pd.crosstab(result['content_type'], result['a_type'],margins=True) # 制作交叉表(confusion_matrix.iat[0,0] + confusion_matrix.iat[1,1])/confusion_matrix.iat[2,2]# 提取正负面评论信息ind_pos = list(emotional_value[emotional_value['a_type'] == 'pos']['index_content'])ind_neg = list(emotional_value[emotional_value['a_type'] == 'neg']['index_content'])posdata = word[[i in ind_pos for i in word['index_content']]]negdata = word[[i in ind_neg for i in word['index_content']]]# 绘制词云import matplotlib.pyplot as pltfrom wordcloud import WordCloud# 正面情感词词云freq_pos = posdata.groupby(by=['word'])['word'].count()freq_pos = freq_pos.sort_values(ascending=False)backgroud_Image=plt.imread('../data/pl.jpg')wordcloud = WordCloud(font_path="STZHONGS.ttf",max_words=100,background_color='white',mask=backgroud_Image)pos_wordcloud = wordcloud.fit_words(freq_pos)plt.imshow(pos_wordcloud)plt.axis('off')plt.show()# 负面情感词词云freq_neg = negdata.groupby(by=['word'])['word'].count()freq_neg = freq_neg.sort_values(ascending=False)neg_wordcloud = wordcloud.fit_words(freq_neg)plt.imshow(neg_wordcloud)plt.axis('off')plt.show()# 将结果写出,每条评论作为一行posdata.to_csv("../tmp/posdata.csv", index=False, encoding='utf-8')negdata.to_csv("../tmp/negdata.csv", index=False, encoding='utf-8')
*代码请联系客服领取,联系方式见文末
运行代码清单3,可得正面情感评论词云如图1所示,负面情感评论词云如图2所示。

图1 正面情感评论词云
由图1正面情感评论词云可知,“不错”“满意”“好评”等正面情感词出现的频数较高,并且没有掺杂负面情感的词语,可以看出情感分析能较好的将正面情感评论抽取出来。

图2 负面情感评论词云
由图2负面情感评论词云可知,“差评”“垃圾”“不好”“太差”等负面情感词出现的频数较高,并且没有掺杂正面情感的词语,可以看出情感分析能较好的将负面情感评论抽取出来。
为了进一步查看情感分析效果,假定用户在评论时,不存在选了好评的标签,而写了差评内容的情况下,比较原评论的评论类型与情感分析得出的评论类型,绘制情感倾向分析混淆矩阵,如表1所示,查看词表的情感分析的准确率。
表1 情感倾向分析混淆矩阵

由表1可知,通过比较原评论的评论类型与情感分析得出的评论类型,基于词表的情感分析的准确率达到了89.46%,证明通过词表的情感分析去判断某文本的情感程度是有效的。
利用LDA主题模型提取京东评论数据(四)
使用LDA模型进行主题分析
将于下周一推送
整理不易,求三连(分享、点赞、在看)
蔡老师:
陈老师:







