情感分析+python

情感分析

情感分析主要基于文本数据,是自然语言处理(NPL)的主要内容。情感分析:又称意见挖掘、倾向性分析等。简单而言,情感分析是利用自然语言处理技术来分析文本中的情感信息,帮助人们更好地理解和应用大量的文本数据。

1. 数据如下所示

image

2.情感处理

#打分情感分析
#情感分析的结果是一个小数,越接近1,说明越偏向积极;越接近0,说明越偏向消极。
from snownlp import SnowNLP

score_list = []  # 情感评分值
tag_list = []  # 打标分类结果
for com in data['feedback']:
    tag = ''
    judge = SnowNLP(str(com))
    sentiment_score = judge.sentiments
    score_list.append(sentiment_score)
    if sentiment_score >= 0.75:
        tag = "好评"
    elif sentiment_score >= 0.5:
        tag = "一般"
    elif sentiment_score >= 0.25:
        tag = "较差"
    else:
        tag = "差评"
    tag_list.append(tag)
data['score'] = score_list
data['Result'] = tag_list
data.to_excel('Raing_Result.xlsx', index=None)

data.groupby(by=['Result']).count()['feedback']  # 分组统计情感分析结果

结果如图:
image

3. 画图直观展示

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei'] 
grp = data['Result'].value_counts()
print(grp)
grp.plot.pie(y='Result', autopct='%.2f%%')  # 画饼图
plt.title('对于XBB是否会导致腹泻的情感分布占比图')

结果如图:
image

4. 查看处理结果

image

5. 保存

image

posted @ 2024-03-25 20:42  停或走  阅读(109)  评论(0编辑  收藏  举报