Python基于jieba分词实现snownlp情感分析
作者:Sir 发布时间:2023-11-14 21:43:38
情感分析(sentiment analysis)是2018年公布的计算机科学技术名词。
它可以根据文本内容判断出所代表的含义是积极的还是负面的,也可以用来分析文本中的意思是褒义还是贬义。
一般应用场景就是能用来做电商的大量评论数据的分析,比如好评率或者差评率的统计等等。
我们这里使用到的情感分析的模块是snownlp,为了提高情感分析的准确度选择加入了jieba模块的分词处理。
由于以上的两个python模块都是非标准库,因此我们可以使用pip的方式进行安装。
pip install jieba
pip install snownlp
jieba是一个强大的中文分词处理库,能够满足大多数的中文分词处理,协助snownlp的情感分析。
# Importing the jieba module and renaming it to ja.
import jieba as ja
from snownlp import SnowNLP
# Importing the snownlp module and renaming it to nlp.
为了避免大家使用过程中出现的版本冲突问题,这里将python的内核版本展示出来。
python解释器版本:3.6.8
接下来首先创建一组需要进行情感分的数据源,最后直接分析出该文本代表的是一个积极情绪还是消极情绪。
# Creating a variable called analysis_text and assigning it the value of a string.
analysis_text = '这个实在是太好用了,我非常的喜欢,下次一定还会购买的!'
定义好了需要分析的数据来源语句,然后就是分词处理了。这里说明一下为什么需要分词处理,是因为snownlp这个情感分析模块它的中文分词结果不太标准。
比如说,'不好看',这个词如果使用snownlp来直接分词的话大概率的就会分为'不'和'好看'这两个词。
这样的明明是一个带有负面情绪的中文词汇可能就直接被定义为正面情绪了,这也就是为什么这里需要先使用jieba进行分词处理了。
# Using the jieba module to cut the analysis_text into a list of words.
analysis_list = list(ja.cut(analysis_text))
# Printing the list of words that were cut from the analysis_text.
print(analysis_list)
# ['这个', '实在', '是', '太', '好', '用', '了', ',', '我', '非常', '的', '喜欢', ',', '下次', '一定', '还会', '购买', '的', '!']
根据上面分词以后的结果来看,分词的粒度还是比较细致的,每个词都是最多两个字符串的长度。
使用jieba提供的cut()函数,关键词已经分割完成了,接着就是提取主要的关键字。
一般情况下我们做情感分析都会提取形容词类型的关键字,因为形容词能够代表该文本所表现出来的情绪。
# Importing the `posseg` module from the `jieba` module and renaming it to `seg`.
import jieba.posseg as seg
# This is a list comprehension that is creating a list of tuples. Each tuple contains the word and the flag.
analysis_words = [(word.word, word.flag) for word in seg.cut(analysis_text)]
# Printing the list of tuples that were created in the list comprehension.
print(analysis_words)
# [('这个', 'r'), ('实在', 'v'), ('是', 'v'), ('太', 'd'), ('好用', 'v'), ('了', 'ul'), (',', 'x'), ('我', 'r'), ('非常', 'd'), ('的', 'uj'), ('喜欢', 'v'), (',', 'x'), ('下次', 't'), ('一定', 'd'), ('还', 'd'), ('会', 'v'), ('购买', 'v'), ('的', 'uj'), ('!', 'x')]
根据上面的python推导式,将分词以后的关键字和该关键自对应的词性提取出来。
下面是一份jieba模块使用过程中对应的词性表,比如词性标记a代表的就是形容词。
# This is a list comprehension that is creating a list of tuples. Each tuple contains the word and the flag.
keywords = [x for x in analysis_words if x[1] in ['a', 'd', 'v']]
# Printing the list of tuples that were created in the list comprehension.
print(keywords)
# [('实在', 'v'), ('是', 'v'), ('太', 'd'), ('好用', 'v'), ('非常', 'd'), ('喜欢', 'v'), ('一定', 'd'), ('还', 'd'), ('会', 'v'), ('购买', 'v')]
根据关键词的标签提取出关键字以后,这个时候可以将情感标记去除只保留关键字就可以了。
# This is a list comprehension that is creating a list of words.
keywords = [x[0] for x in keywords]
# Printing the list of keywords that were created in the list comprehension.
print(keywords)
# ['实在', '是', '太', '好用', '非常', '喜欢', '一定', '还', '会', '购买']
到现在为至,分词的工作已经处理完了,接下来就是情感分析直接使用snownlp分析出结果。
# Creating a variable called `pos_num` and assigning it the value of 0.
pos_num = 0
# Creating a variable called `neg_num` and assigning it the value of 0.
neg_num = 0
# This is a for loop that is looping through each word in the list of keywords.
for word in keywords:
# Creating a variable called `sl` and assigning it the value of the `SnowNLP` function.
sl = SnowNLP(word)
# This is an if statement that is checking to see if the sentiment of the word is greater than 0.5.
if sl.sentiments > 0.5:
# Adding 1 to the value of `pos_num`.
pos_num = pos_num + 1
else:
# Adding 1 to the value of `neg_num`.
neg_num = neg_num + 1
# This is printing the word and the sentiment of the word.
print(word, str(sl.sentiments))
下面就是对原始文本提取关键词以后的每个词的情感分析结果,0-1之间代表情绪越接近于1代表情绪表现的越是积极向上。
# 实在 0.3047790802524796
# 是 0.5262327818078083
# 太 0.34387502381406
# 好用 0.6558628208940429
# 非常 0.5262327818078083
# 喜欢 0.6994590939824207
# 一定 0.5262327818078083
# 还 0.5746682977321914
# 会 0.5539033457249072
# 购买 0.6502590673575129
为了使得关键词的分析结果更加的符合我们的想法也可以对负面和正面的关键词进行统计得到一个结果。
# This is a string that is using the `format` method to insert the value of `pos_num` into the string.
print('正面情绪关键词数量:{}'.format(pos_num))
# This is a string that is using the `format` method to insert the value of `neg_num` into the string.
print('负面情绪关键词数量:{}'.format(neg_num))
# This is a string that is using the `format` method to insert the value of `pos_num` divided by the value of `pos_num`
# plus the value of `neg_num` into the string.
print('正面情绪所占比例:{}'.format(pos_num/(pos_num + neg_num)))
# 正面情绪关键词数量:8
# 负面情绪关键词数量:2
# 正面情绪所占比例:0.8
来源:https://mp.weixin.qq.com/s/S6YsP9pyYUuB-31QlvJT-w


猜你喜欢
- python 的语法定义和C++、matlab、java 还是很有区别的。1. 括号与函数调用def devided_3(x):  
- 酝酿了将近一个春夏秋冬的腾讯网首页终于亮剑!反响热烈!让我们来分享它成功背后的酸甜苦辣吧。腾讯网首页改版终于开花结果。于2008年3月25日
- 本文实例讲述了Python Datetime模块和Calendar模块用法。分享给大家供大家参考,具体如下:datetime模块1.1 概述
- 前言本文将记录学习基于 Socket 通信机制建立 TCP 反向连接,借助 Python 脚本实现主机远程控制的目的。我们在传输数据时,可以
- 测试配置文件test.conf内容如下:[first]w = 2v: 3c =11-3[second]sw=4test: hello测试配置
- 读取和存储dict()与.json格式文件读取.json格式文件并将数据保存到字典中数据文件:hg.json{"商家名称"
- 爬虫所需要的功能,基本上在urllib中都能找到,学习这个标准库,可以更加深入的理解后面更加便利的requests库。首先在Pytho2.x
- 本文实例讲述了Python实现小数转化为百分数的格式化输出方法。分享给大家供大家参考,具体如下:比如将 0.1234 转化为 12.34%
- 最近在使用Python的过程中,发现网上很少提到在使用post方式时,怎么传一个数组作为参数的示例,此处根据自己的实践经验,给出相关示例:单
- 图片非常重要,它们可以让你的页面更好看,更引人注目。但是,高质量和漂亮的图片常常会很大,它们会让页面加载变慢并消耗更多带宽。所以我们,这些设
- 前言:前面文章讲述了 MySQL 系统中常见的几种日志,其实还有事务相关日志 redo log 和 undo log 没有介绍。相对于其他几
- 最近做个软件,用PyQT写的,在实现菜单栏点击弹出新窗口的时候严重被卡壳,发现用WxPython的思想和方式来做完全无法实现。PyQT的中文
- 刚开始学习tensorflow,还不太会用,开个博记录,今天遇到一个问题是用tf.layers.dense创建的全连接层,如何查看权重?知道
- 前言近来chatGPT挺火的,也试玩了一下,确实挺有意思。这里记录一下在Python中如何去使用chatGPT。本篇文章的实现100%基于
- 表头固定应该是一个用得比较多的功能,参考了网上几个例子,在几个常用浏览器下显示不是很完美。而且很多都是基于固定的表格,在编码时多写一个固定的
- 正文:本文展示一些高级的Python设计结构和它们的使用方法。在日常工作中,你可以根据需要选择合适的数据结构,例如对快速查找性的
- function toBreakWord(intLen, id){ var obj=document.getElementById(id);
- 由于最近需要做项目,需要进行分词等,查了资料之后,发现python NLTK很强大,于是就想试试看。在网上找了很多安装资料,都不太完整,下载
- clone 一个新项目,发现导包的时候出错 …原因可能是 pycharm 的根目录设置不对。设置根目录的步骤 如下 :来源:https://
- 很早以前就有很多关于用CSS制作的相册,今天突然想看又找不到,反正也无聊,自己做了一下,结果做一下发现自己温习了一些东西。还行,