建筑兔零基础自学python记录28|实战词云可视化项目——SnowNLP情感分析6
SnowNLP情感分析(积极、消极、中性)
我们来导入SnowNLP进行情感分析,测试两个评价,看看情感分析后的打分。可以看到第一个好评得分0.69第二个差评得分0.25。
涉及代码如下:
import snownlp
word = snownlp.SnowNLP('商家做的又快又好吃,食材新鲜。味道让人流连忘返,下次还点!')
feeling1 = word.sentiments
print('feeling1=',feeling1)
word = snownlp.SnowNLP('太薄了,面料也不喜欢')
feeling2 = word.sentiments
print('feeling2=',feeling2)
(1)SnowNLP中文自然语言处理库
这里我们用到打分的SnowNLP是一个简化中文自然语言处理任务的 Python 库。针对淘宝等电商网站的评论,所以对购物类的文本情感分析准确度很高。
功能:
- 中文分词print('中文分词',s.words):将中文文本分割成一个个词语,方便后续的处理和分析。
- 情感分析print('情感分析',s.sentiments):判断文本的情感倾向,是积极、消极还是中性。
- 拼音转换print('转成拼音',s.pinyin):将中文文本转换为拼音。
- 词性标注:为每个词语标注其词性,如名词、动词、形容词等。
- 关键词提取:从文本中提取出关键的词语。
- 文本分类:将文本归类到不同的类别中。
- 摘要生成:自动生成文本的摘要,参数表示要生成的摘要句子数量。
-
词频统计
我们来测试一下部分功能
import snownlp
print('\n')
text1 = '商家做的又快又好吃,食材新鲜。味道让人流连忘返,下次还点!'
print('{:-^50}'.format('测试文本:'+text1))
s = snownlp.SnowNLP(text1)
print('\n')
print('情感分析',s.sentiments)
#情感分析 0.687843313574824
print('\n')
print('中文分词',s.words)
#中文分词 ['商家', '做', '的', '又', '快', '又', '好吃', ',', '食材', '新鲜', '。', '味道', '让', '人', '流连忘返', ',', '下次', '还点', '!']
print('\n')
print('转成拼音',s.pinyin)
#转成拼音 ['shang', 'jia', 'zuo', 'de', 'you', 'kuai', 'you', 'hao', 'chi', ',', 'shi', 'cai', 'xin', 'xian', '。', 'wei', 'dao', 'rang', 'ren', 'liu', 'lian', 'wang', 'fan', ',', 'xia', 'ci', 'huan', 'dian', '!']
print('\n')
print('词频',s.tf)
#词频 [{'商': 1}, {'家': 1}, {'做': 1}, {'的': 1}, {'又': 1}, {'快': 1}, {'又': 1}, {'好': 1}, {'吃': 1}, {',': 1}, {'食': 1}, {'材': 1}, {'新': 1}, {'鲜': 1}, {'。': 1}, {'味': 1}, {'道': 1}, {'让': 1}, {'人': 1}, {'流': 1}, {'连': 1}, {'忘': 1}, {'返': 1}, {',': 1}, {'下': 1}, {'次': 1}, {'还': 1}, {'点': 1}, {'!': 1}]
(2)print('\n')换行
'\n'
属于转义字符,代表换行符。
了解了要用到的SnowNLP之后我们来具体尝试一下:
(3)积极和消极分类词云源代码:
import jieba
import wordcloud
import imageio
import snownlp
mk = imageio.v2.imread('swk.jpg')
w1 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=mk,
scale=15)
w2 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=mk,
scale=15)
f = open('西游记.txt',encoding='utf-8')
txt = f.read()
txtlist = jieba.lcut(txt)
positivelist = []
negativelist = []
print('开始进行情感分析,请稍等,西游记全文那么长的文本需要三分钟左右')
import snownlp
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
if feeling > 0.96:
positivelist.append(each)
elif feeling < 0.06:
negativelist.append(each)
else:
pass
positive_string = " ".join(positivelist)
negative_string = " ".join(negativelist)
w1.generate(positive_string)
w2.generate(negative_string)
w1.to_file('output12-positive.png')
w2.to_file('output12-negative.png')
print('词云生成完成')
大部分代码都是学过的,可以复习建筑兔零基础自学python记录26|实战词云可视化项目——不同形状4-CSDN博客
这次我们主要来看一下新增的部分:
具体代码解读:
positivelist = []
negativelist = []
创建了两个空列表 positivelist
和 negativelist
。positivelist
用于存储情感倾向为积极的文本,negativelist
用于存储情感倾向为消极的文本。
print('开始进行情感分析,请稍等,西游记全文那么长的文本需要三分钟左右')
这段代码是个作者嵌入进去的提示,一般的词云都是运行就有结果的,但对于整篇文章来说往往需要一些运行时间。这能够提示结果并非出错而是在运行中。
print('词云生成完成')
代码最后的这一行文字也是为了提示词云制作完成。在控制台里我们可以清楚看到提示:
接下来我们看下循环代码:
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
if feeling > 0.96:
positivelist.append(each)
elif feeling < 0.06:
negativelist.append(each)
else:
pass
解读:
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
txtlist是上文代码提到的经过分词后的txt文本,each是变量
snownlp.SnowNLP(each)本行就是把变量each导入snownlp库里,导入后的结果起名为each_word
在这里我们对文本进行情感分析
if feeling > 0.96:
positivelist.append(each)
elif feeling < 0.06:
negativelist.append(each)
else:
pass
feeling得分>0.95判定属于positivelist[ ]
feeling得分<0.06判定属于negativelist[ ]
0.06<feeling得分<0.95不管
append()是向列表添加元素,这里的append(each)就是把每次判断的内容添加到列表中
positive_string = " ".join(positivelist)
negative_string = " ".join(negativelist)
join()用于将可迭代对象(如列表、元组等)中的元素连接成一个字符串。
将 positivelist 列表中的所有元素用空格连接成一个字符串
将 negativelist 列表中的所有元素用空格连接成一个字符串
(4)SnowNLP情感分析分值
SnowNLP 进行情感分析时,会为文本给出一个 0 到 1 之间的分值,这个分值代表了文本的情感倾向。
- 接近 1:表示文本具有积极的情感倾向。分值越接近 1,说明文本所表达的积极情感越强烈。
- 接近 0:意味着文本具有消极的情感倾向。分值越接近 0,表明文本所表达的消极情感越强烈。
- 接近 0.5:说明文本的情感倾向不明显,可能是中性的描述。
整段代码解读如下:
import jieba
import wordcloud
import imageio
import snownlp
mk = imageio.v2.imread('swk.jpg')
w1 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=mk,
scale=15)
w2 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=mk,
scale=15)
f = open('西游记.txt',encoding='utf-8')
txt = f.read()
txtlist = jieba.lcut(txt)
#创建了两个空列表 positivelist 和 negativelist
positivelist = []
negativelist = []
print('开始进行情感分析,请稍等,西游记全文那么长的文本需要三分钟左右')
#对文本进行情感分析
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
#feeling得分>0.95判定属于positivelist[ ]
if feeling > 0.96:
positivelist.append(each)
#feeling得分<0.06判定属于negativelist[ ]
elif feeling < 0.06:
negativelist.append(each)
#0.06<feeling得分<0.95不管
else:
pass
#分别将positivelist列表和negativelist列表中的所有元素用空格连接成一个字符串
positive_string = " ".join(positivelist)
negative_string = " ".join(negativelist)
w1.generate(positive_string)
w2.generate(negative_string)
w1.to_file('output12-positive.png')
w2.to_file('output12-negative.png')
print('词云生成完成')
(5)总结
print('\n')换行
更多推荐
所有评论(0)