《实战》基于情感词典的文本情感分析与LDA主题分析

article/2025/3/14 20:33:04

——基于电商领域的文本情感分析与LDA主题分析——

  • 一、情感分析
    • 1.1词典导入
    • 1.2 增加新词
    • 1.3合并到review_long_clean中
    • 1.4 修正情感倾向
    • 1.5计算每条评论的情感值
    • 1.6 查看情感分析效果
  • 二、情感分析效果
    • 2.1 将数据合并
    • 2.2 结果对比
    • 2.3 情感词云
  • 三、基于LDA模型的主题分析
    • 3.1建立词典、语料库
    • 3.2主题数寻优

一、情感分析

1.1词典导入

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.pylab import style #自定义图表风格
style.use('ggplot')
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
plt.rcParams['font.sans-serif'] = ['Simhei'] # 解决中文乱码问题import re
import jieba.posseg as psg
import itertools
#conda install -c anaconda gensim
from gensim import corpora,models #主题挖掘,提取关键信息# pip install wordcloud
from wordcloud import WordCloud,ImageColorGenerator
from collections import Counterfrom sklearn.feature_extraction.text import CountVectorizerreview_long_clean = pd.read_excel('1_review_long_clean.xlsx')      #导入处理后的数据集

导入评价情感词典

#来自知网发布的情感分析用词语集pos_comment=pd.read_csv('./正面评价词语(中文).txt',header=None,sep='\n',encoding='utf-8') 
neg_comment=pd.read_csv('./负面评价词语(中文).txt',header=None,sep='\n',encoding='utf-8')pos_emotion=pd.read_csv('./正面情感词语(中文).txt',header=None,sep='\n',encoding='utf-8')
neg_emotion=pd.read_csv('./负面情感词语(中文).txt',header=None,sep='\n',encoding='utf-8')

查看一下正负情感词典分布

print('正面评价词语:',pos_comment.shape)
print('负面评价词语:',neg_comment.shape)print('正面情感词语:',pos_emotion.shape)
print('负面情感词语:',neg_emotion.shape)

正面评价词语: (3743, 1)
负面评价词语: (3138, 1)
正面情感词语: (833, 1)
负面情感词语: (1251, 1)

#将正面和负面的合并
pos=pd.concat([pos_comment,pos_emotion],axis=0)
print('正面合并后:',pos.shape)neg=pd.concat([neg_comment,neg_emotion],axis=0)
print('负面合并后:',neg.shape)

正面合并后: (4576, 1)
负面合并后: (4389, 1)

1.2 增加新词

#判断在不在词典中
c='点赞'
print(c in pos.values)d='歇菜'
print(d in neg.values)

False
False

不在就添加进词典

#不在就添加进词典new_pos=pd.Series(['点赞'])
new_neg=pd.Series(['歇菜'])positive=pd.concat([pos,new_pos],axis=0)
print(positive.shape)negative=pd.concat([neg,new_neg],axis=0)
print(negative.shape)
#将情感词存入,Dataframe中,并且正面词赋予权重1
positive.columns=['review']
positive['weight']=pd.Series([1]*len(positive))
positive.head()

在这里插入图片描述

#将情感词存入,Dataframe中,并且负面词赋予权重-1
negative.columns=['review']
negative['weight']=pd.Series([-1]*len(negative))
negative.head()

在这里插入图片描述

#将正负情感词合并
pos_neg=pd.concat([positive,negative],axis=0)
pos_neg.shape

(8967, 2)

1.3合并到review_long_clean中

#表联接
#清洗赶紧的数据,赋值给data
data=review_long_clean.copy()
print('data原始shape:',data.shape)#将data和情感词两个表合并在一块,按照data的word列,情感数据按照review
review_mltype=pd.merge(data,pos_neg,how='left',left_on='word',right_on='review')
review_mltype.shape
print('表联接合并后:',review_mltype.shape)
print('----------完成评论中的情感词赋值------------')
print('review_mltype:')
review_mltype.head()

data原始shape: (25172, 6)
表联接合并后: (25172, 8)
----------完成评论中的情感词赋值------------
review_mltype:
在这里插入图片描述

#删除情感值列,同时赋予不是情感词的词,权重为0
review_mltype=review_mltype.drop(['review'],axis=1)
review_mltype=review_mltype.replace(np.nan,0)
review_mltype.head()

在这里插入图片描述

1.4 修正情感倾向

如有多重否定,那么奇数否定是否定,偶数否定是肯定

看该情感词前2个词,来判罚否定的语气。如果在句首,则没有否词,如果在句子的第二次词,则看前1个词,来判断否定的语气。

#读取否定词词典,新建一列为freq
notdict=pd.read_csv('./not.csv')
notdict.shapenotdict['freq']=[1]*len(notdict)
notdict.head()

在这里插入图片描述

#准备一,构建amend_weight列,其值与情感权重值相同
review_mltype['amend_weight']=review_mltype['weight']
#创建,id列,初始化值为0-到最后一个的,顺序值
review_mltype['id']=np.arange(0,review_mltype.shape[0])
review_mltype.head(10)

在这里插入图片描述
review_mltype是整理后有情感权重的,全部评论数据
在这里插入图片描述

只取出有情感值的文本

# 准备二,只取出有情感值的行
only_review_mltype=review_mltype[review_mltype['weight']!=0]#只保留有情感值的索引
only_review_mltype.index=np.arange(0,only_review_mltype.shape[0]) #索引重置print(only_review_mltype.shape)
only_review_mltype.head()

(1526, 10)
在这里插入图片描述

看该情感词前2个词,来判罚否定的语气。如果在句首,则没有否词,如果在句子的第二次词,则看前1个词,来判断否定的语气。

#看该情感词前2个词,来判罚否定的语气。如果在句首,则没有否词,如果在句子的第二次词,则看前1个词,来判断否定的语气。index=only_review_mltype['id']for i in range(0,only_review_mltype.shape[0]):review_i=review_mltype[review_mltype['index_content']==only_review_mltype['index_content'][i]] #第i个情感词的评论review_i.index=np.arange(0,review_i.shape[0])#重置索引后,索引值等价于index_wordword_ind = only_review_mltype['index_word'][i] #第i个情感值在该条评论的位置#第一种,在句首。则不用判断#第二种,在评论的第2个为位置if word_ind==2:ne=sum( [ review_i['word'][word_ind-1] in notdict['term']  ] )if ne==1:review_mltype['amend_weight'][index[i]] = -( review_mltype['weight'][index[i]] )#第三种,在评论的第2个位置以后       elif word_ind > 2:ne=sum( [ word in notdict['term'] for word in review_i['word'][[word_ind-1,word_ind-2]]  ] ) # 注意用中括号[word_ind-1,word_ind-2]if ne==1:review_mltype['amend_weight'][index[i]]=- ( review_mltype['weight'][index[i]] )
review_mltype.shape
review_mltype[(review_mltype['weight']-review_mltype['amend_weight'])!=0] #说明两列值一样

1.5计算每条评论的情感值

将一条文本中所有情感值计算,在相加到一块。

emotion_value=review_mltype.groupby('index_content',as_index=False)['amend_weight'].sum()
emotion_value.head()
emotion_value.to_csv('./1_emotion_value.csv',index=True,header=True)

在这里插入图片描述

1.6 查看情感分析效果

#每条评论的amend_weight总和不等于零content_emotion_value=emotion_value.copy()
content_emotion_value.shape#取出不等于0的的文本
content_emotion_value=content_emotion_value[content_emotion_value['amend_weight']!=0]#设置情感倾向
content_emotion_value['ml_type']=''
content_emotion_value['ml_type'][content_emotion_value['amend_weight']>0]='pos'
content_emotion_value['ml_type'][content_emotion_value['amend_weight']<0]='neg'content_emotion_value.shape
content_emotion_value.head()

在这里插入图片描述

二、情感分析效果

读取原始数据

#读取原始数据
raw_data=pd.read_csv('./reviews.csv')
raw_data.head()

在这里插入图片描述
方法缺陷:

#每条评论的amend_weight总和等于零
#这个方法其实不好用,有一半以上的评论区分不出正、负情感。content_emotion_value0=emotion_value.copy()
content_emotion_value0=content_emotion_value0[content_emotion_value0['amend_weight']==0]
content_emotion_value0.head()raw_data.content[6]
raw_data.content[7]
raw_data.content[8]

2.1 将数据合并

将情感分析结果,和原始数据集合并:

#合并到大表中content_emotion_value=content_emotion_value.drop(['amend_weight'],axis=1)review_mltype.shapereview_mltype=pd.merge(review_mltype,content_emotion_value,how='left',left_on='index_content',right_on='index_content')
review_mltype=review_mltype.drop(['id'],axis=1)
review_mltype.shape
review_mltype.head()review_mltype.to_csv('./1_review_mltype',index=True,header=True)

2.2 结果对比

与真实值对比下,情感分析结果的准确性:

cate=['index_content','content_type','ml_type']
data_type=review_mltype[cate].drop_duplicates()confusion_matrix=pd.crosstab(data_type['content_type'],data_type['ml_type'],margins=True)
confusion_matrix

在这里插入图片描述

data=data_type[['content_type','ml_type']]
data=data.dropna(axis=0)
print( classification_report(data['content_type'],data['ml_type']) )
         precision    recall  f1-score   support
     neg       0.90      0.87      0.88       437pos       0.89      0.92      0.90       501

accuracy 0.89 938
macro avg 0.90 0.89 0.89 938
weighted avg 0.89 0.89 0.89 938

2.3 情感词云

将文本按照情感分类,然后统计词云

data=review_mltype.copy()word_data_pos=data[data['ml_type']=='pos']
word_data_neg=data[data['ml_type']=='neg']font=r"C:\Windows\Fonts\msyh.ttc"background_image=plt.imread('./pl.jpg')
wordcloud = WordCloud(font_path=font, max_words = 100, mode='RGBA' ,background_color='white',mask=background_image) #width=1600,height=1200
wordcloud.generate_from_frequencies(Counter(word_data_pos.word.values))plt.figure(figsize=(15,7))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()background_image=plt.imread('./pl.jpg')
wordcloud = WordCloud(font_path=font, max_words = 100, mode='RGBA' ,background_color='white',mask=background_image) #width=1600,height=1200
wordcloud.generate_from_frequencies(Counter(word_data_neg.word.values))plt.figure(figsize=(15,7))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

在这里插入图片描述
在这里插入图片描述

三、基于LDA模型的主题分析

优点:不需要人工调试,用相对少的迭代找到最优的主题结构。

3.1建立词典、语料库

data=review_mltype.copy()word_data_pos=data[data['ml_type']=='pos']
word_data_neg=data[data['ml_type']=='neg']
#建立词典,去重pos_dict=corpora.Dictionary([ [i] for i in word_data_pos.word]) #shape=(n,1)
neg_dict=corpora.Dictionary([ [i] for i in word_data_neg.word])
#建立语料库
pos_corpus=[ pos_dict.doc2bow(j) for j in [ [i] for i in word_data_pos.word] ] #shape=(n,(2,1))
neg_corpus=[ neg_dict.doc2bow(j) for j in [ [i] for i in word_data_neg.word] ]

3.2主题数寻优

#构造主题数寻优函数def cos(vector1,vector2):'''函数功能:余玄相似度函数'''dot_product=0.0normA=0.0normB=0.0for a,b in zip(vector1,vector2):dot_product +=a*bnormA +=a**2normB +=b**2if normA==0.0 or normB==0.0:return Noneelse:return ( dot_product/((normA*normB)**0.5) )
#主题数寻优
#这个函数可以重复调用,解决其他项目的问题def LDA_k(x_corpus,x_dict):'''函数功能:'''#初始化平均余玄相似度mean_similarity=[]mean_similarity.append(1)#循环生成主题并计算主题间相似度for i in np.arange(2,11):lda=models.LdaModel(x_corpus,num_topics=i,id2word=x_dict) #LDA模型训练for j in np.arange(i):term=lda.show_topics(num_words=50)#提取各主题词top_word=[] #shape=(i,50)for k in np.arange(i):top_word.append( [''.join(re.findall('"(.*)"',i)) for i in term[k][1].split('+')]) #列出所有词#构造词频向量word=sum(top_word,[]) #列车所有词unique_word=set(word) #去重#构造主题词列表,行表示主题号,列表示各主题词mat=[] #shape=(i,len(unique_word))for j in np.arange(i):top_w=top_word[j]mat.append( tuple([ top_w.count(k) for k in unique_word ])) #统计list中元素的频次,返回元组#两两组合。方法一p=list(itertools.permutations(list(np.arange(i)),2)) #返回可迭代对象的所有数学全排列方式。y=len(p) # y=i*(i-1)top_similarity=[0]for w in np.arange(y):vector1=mat[p[w][0]]vector2=mat[p[w][1]]top_similarity.append(cos(vector1,vector2))#        #两两组合,方法二
#        for x in range(i-1):
#            for y in range(x,i):#计算平均余玄相似度mean_similarity.append(sum(top_similarity)/ y)return mean_similarity
#计算主题平均余玄相似度pos_k=LDA_k(pos_corpus,pos_dict)
neg_k=LDA_k(neg_corpus,neg_dict)pos_k
neg_k

[1,
0.04,
0.006666666666666667,
0.0033333333333333335,
0.0,
0.004,
0.006666666666666666,
0.025000000000000015,
0.03500000000000002,
0.046222222222222234]
[1,
0.02,
0.0,
0.0,
0.0,
0.0026666666666666666,
0.002857142857142857,
0.0035714285714285718,
0.01333333333333334,
0.022222222222222233]

pd.Series(pos_k,index=range(1,11)).plot()
plt.title('正面评论LDA主题数寻优')
plt.show()

<matplotlib.axes._subplots.AxesSubplot at 0x1de4b888>
Text(0.5, 1.0, ‘正面评论LDA主题数寻优’)
在这里插入图片描述

pd.Series(neg_k,index=range(1,11)).plot()
plt.title('负面评论LDA主题数寻优')
plt.show()

matplotlib.axes._subplots.AxesSubplot at 0x1d077ac8>
Text(0.5, 1.0, ‘负面评论LDA主题数寻优’)
在这里插入图片描述

确定正负面主题数目都为3

pos_lda=models.LdaModel(pos_corpus,num_topics=2,id2word=pos_dict)
neg_lda=models.LdaModel(neg_corpus,num_topics=2,id2word=neg_dict)pos_lda.print_topics(num_topics=10)
neg_lda.print_topics(num_topics=10)

[(0,
‘0.085*“安装” + 0.036*“满意” + 0.019*“服务” + 0.018*“不错” + 0.015*“好评” + 0.012*“客服” + 0.011*“人员” + 0.010*“物流” + 0.009*“送” + 0.008*“家里”’),
(1,
‘0.024*“师傅” + 0.021*“送货” + 0.019*“很快” + 0.016*“值得” + 0.013*“售后” + 0.012*“信赖” + 0.012*“东西” + 0.009*“太” + 0.009*“购物” + 0.009*“电话”’)]
[(0,
‘0.089*“安装” + 0.018*“师傅” + 0.015*“慢” + 0.013*“装” + 0.011*“打电话” + 0.011*“太慢” + 0.008*“坑人” + 0.008*“服务” + 0.007*“配件” + 0.007*“问”’),
(1,
‘0.021*“垃圾” + 0.019*“太” + 0.019*“差” + 0.016*“安装费” + 0.015*“售后” + 0.014*“东西” + 0.013*“不好” + 0.013*“客服” + 0.012*“加热” + 0.012*“小时”’)]


http://chatgpt.dhexx.cn/article/g5An7PgU.shtml

相关文章

如何去做词频统计和关键词共现分析

1 对关键词的词频进行统计 下面展示一些 内联代码片。 # encodingutf-8 import openpyxl wb openpyxl.Workbook() wb openpyxl.load_workbook(copy1.xlsx) # Excel名 sh wb[all] # sheet名换一种写法 rowsh.max_row …

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

写在前面&#xff1a; 现有的情感分析比较常用的有两种&#xff0c;分别是基于词典的和机器学习&#xff0c;前者也属于非监督学习&#xff0c;后者自然一般属于监督学习。 刚开始学情感分析&#xff0c;下面先从**【基于词典的情感分析】**开始进行&#xff1a; 词典&#xf…

如何利用情感词典做中文文本的情感分析?

如何利用情感词典做中文文本的情感分析&#xff1f; 这是本学期在大数据哲学与社会科学实验室做的第四次分享了。 第一次分享的是&#xff1a;如何利用“wordcloudjieba”制作中文词云&#xff1f; 第二次分享的是&#xff1a;如何爬取知乎中问题的回答以及评论的数据&#…

基于情感词典的文本情感分析(一个最简单的举例能直接运行)

1. 使用情感词典进行情感分析的思路为 1&#xff09; 将Web文本进行分句&#xff0c;使其以句子为单位进行处理&#xff1b;2&#xff09; 从分句中抽取连词和否定词&#xff0c;并标记相应连词与否定词位置&#xff1b;3&#xff09; 访问情感词汇本体&#xff0c;确定词汇极性…

Python利用情感词典做情感分析

情感分析是大数据时代常见的一种分析方法&#xff0c;多用于对产品评论的情感挖掘&#xff0c;以探究顾客的满意度程度。在做情感分析时&#xff0c;有两种途径&#xff1a;一种是基于情感词典的分析方法&#xff0c;一种是基于机器学习的方法&#xff0c;两者各有利弊。 在此&…

共现分析

一、共现分析概念 “共现”指文献的特征项描述的信息共同出现的现象&#xff0c;这里的特征项包括文献的外部和内部特征&#xff0c;如题名、作者、关键词、机构等。 而“共现分析”是对共现现象的定量研究&#xff0c; 以揭示信息的内容关联和特征项所隐含的知识。 二、共…

python基于情感词典的情感分析

今天给大家分享的是通过情感词典来对文本进行情感分析最后计算出情感得分 通过情感得分来判断正负调性 主要步骤&#xff1a; 数据准备 本次情感词典采用的是BosonNLP的情感词典&#xff0c;来源于社交媒体文本&#xff0c;所以词典适用于处理社交媒体的情感分析 本次分析准备的…

python 热词分析_Python笔记:热词分析2020-01-01

热词分析在公众趋势分析、舆情分析有很宽广的应用&#xff0c;我们来看看怎么从一个TXT文件中分析出文章的热词出来&#xff0c;我们采用流行的第三方“结巴”包来实现。 首先安装第三方包(matplotlib&#xff0c;jieba&#xff0c;wordcloud&#xff0c;numpy)mmatplotlib主要…

【共词聚类分析】基于CNKI和WOS的小样本稳健性检验

很久之前的一篇文章&#xff0c;最近终于收到了Reviewers的回复&#xff08;一把心酸…其中有一个Comments如下&#xff0c;意思是我们原先的文章没法证明共词聚类方法的结论是合理的…于是打算新增加一个稳健型检验&#xff08;robust analysis&#xff09;&#xff0c;由于上…

python共词矩阵分析结果一步到位

import os import re import pandas as pd from PyPDF2 import PdfFileReader import string import yakeif __name__ __main__:# 运行第一部分代码pdf_files_path C:/Users/win10/Documents/美国智库/pdf_files# 定义一个函数&#xff0c;用于读取PDF文件并将其转化成文本de…

共词分析

一、共现分析概念及主要类型 “共现”指文献的特征项描述的信息共同出现的现象&#xff0c;这里的特征项包括文献的外部和内部特征&#xff0c;如题名、作者、关键词、机构等。而“共现分析”是对共现现象的定量研究&#xff0c;以揭示信息的内容关联和特征项所隐含的知识。常…

AD09由英文改中文菜单步骤

1&#xff1a;打开AD09&#xff0c;点击DXP&#xff0c;选择Preferences 2&#xff1a;在对话框的左边一竖列选择General 3&#xff1a;选择左边竖列后&#xff0c;在右边找到Localization选项 4&#xff1a;点击应用&#xff0c;再点击OK。 5&#xff1a;然后关闭AD09&#xf…

AD软件的常用基本设置

AD软件的基本设置 前言 工欲善其事&#xff0c;必先利其器&#xff1b;最近学弟一直在忙着画板子&#xff0c;但是效率非常低&#xff0c;在看过他的软件基本设置&#xff0c;以及对软件快捷键掌握程度后&#xff08;新手小白&#xff09;&#xff0c;我决定将常AD的常用基本…

STM32定时器做时钟源输出基于CubeMx

目录 前言 CubeMX配置 开始函数 改变频率 改占空比 结论 前言 调试使用的评估板&#xff1a;https://item.taobao.com/item.htm?spma230r.1.14.17.432b1562F8z658&id612002664117&ns1&abbucket14#detail 作者再调试AD5933过程中&#xff0c;需要输出100Hz…

Evaluation Board User Guide UG-364 文档 BUG

最近作者使用这个芯片&#xff1b;在进行硬件性能对比测试中发现ADI文档中的一个小问题&#xff1b; 相位角计算的过程中&#xff0c;就是下图 结合下面的代码&#xff0c;验证了上面文档应该是手误导致的。

2019年全国大学生电子设计竞赛D题简易电路特性测试仪试题

题目要求部分 我负责的部分就是测量阻抗的部分&#xff0c;这一次我使用的是AD5933 AD5933介绍 我这一篇主要是讲使用5933计算那个待测电路的阻抗值&#xff0c;首先就是在概括处已经说明是我们读取的数据其实是一个实部和一个虚部。 然后我们要记住的是向寄存器0x94&#xf…

电赛专题 |国一作品_线路负载及故障检测装置

有幸邀请到了在2019大学生电子设计大赛的获奖优秀队员为本公众号投稿&#xff0c;将分几次推文为大家介绍几只优秀队伍的作品。 本次推文为大家分享西安电子科技大学微电子学院的团队的作品&#xff0c;团队成员为&#xff1a;蒋昊宇 冯郑 张岳琦&#xff08;排名不分先后&…

智能电导率系统电路设计详解

电导率是一个衡量水溶液导电能力的电学物理量&#xff0c; 电阻率的倒数为电导率&#xff0c;用希腊字母κ表示&#xff0c;κ1/ρ。一般意义上&#xff0c;电导率的测量温度是标准温度&#xff08;25℃&#xff09;。在液体中&#xff0c;水的电导率是衡量水质的一个重要指标。…

AD5934阻抗变换模块实验电路板

■ 前言 本文讨论了基于AD5934构建阻抗变换模块。并对于它测试相应的阻抗进行实验。 01电路设计 1.原理图设计1 ▲ 实验电路板 原理图 2.PCB版图 ▲ 实验电路板PCB 电路板输出接口从右到左&#xff0c;前四个的功能定义如下表。后面四个是用于调试使用。 管脚(从右到左)符号功…

使用AD5933测量元器件的谐振特性

■ 前言 元器件的谐振特性 使用 使用AD5933测量电子器件复阻抗 测量元器件的谐振特性。这里记录了一些相应的的电子实验的数据。以备之后进行复习和参考。 01测量电路 在 使用AD5933测量电子器件复阻抗 中给出了直接测量一些元器件&#xff08;电阻、电容&#xff09;的结果。…