1.准备数据集
2.准备相关包
from snownlp import SnowNLP
import codecs
import os
import matplotlib.pyplot as plt
import numpy as np
3.代码
对豆瓣电影《肖申克的救赎》评论进行情感分析。
1.情感各分数段出现频率
首先统计各情感分数段出现的评率并绘制对应的柱状图,代码如下
# -*- coding: utf-8 -*-
from snownlp import SnowNLP
import codecs
import os
import matplotlib.pyplot as plt
import numpy as np
source = open("C:\\Users\\Administrator\\Desktop\\data.txt","r",encoding="utf-8")
line = source.readlines()
sentimentslist = []
for i in line:
s = SnowNLP(i)
print(s.sentiments)
sentimentslist.append(s.sentiments)
plt.hist(sentimentslist, bins = np.arange(0, 1, 0.01), facecolor = 'g')
plt.xlabel('Sentiments Probability')
plt.ylabel('Quantity')
plt.title('Analysis of Sentiments')
plt.show()
评率-数量图
对应的情感系数:
2.情感波动分析
接下来分析200条评论,每条评论的波动情况,代码如下所示:
# -*- coding: utf-8 -*-
from snownlp import SnowNLP
import codecs
import os
import matplotlib.pyplot as plt
import numpy as np
source = open("C:\\Users\\Administrator\\Desktop\\data.txt","r",encoding="utf-8")
line = source.readlines()
sentimentslist = []
for i in line:
s = SnowNLP(i)
print(s.sentiments)
sentimentslist.append(s.sentiments)
#情感波动分析
result = []
i = 0
while i
位于0以上的是积极评论,反之消极评论