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
猜你喜欢
- 首先要解释一下:“极致之美”不是说月儿的这篇文章,因为本人还没有自大到这种程度:P,它形容的是Lisp和javascript结合的优美形态。
- 本文详解的讲解了使用Pillow库进行图片的简单处理,使用PyCharm开发Python的详细过程和各种第三方库的安装与使用。目标1.熟悉P
- 虽然现在有许多网页制作工具能让您轻松地完成工作,但如果使用HTML则可以得到更大控制权,下面介绍几个小技巧。1.使用语句来控制文字排版比用好
- 在CSS样式中,dl部分只是简单的把内外边距设置为0,dd部分有一个clear属性需要特别注意。当某个元素的属性设置float浮动时,它所在
- 在默认的情况下,MySQL搜索不区分大小写(但某些字符集始终区分大小写,如czech)。这意味着,如果你使用col_name LIKE
- 关于oracle 优化的内容很多,概念庞杂,不过可以总结出一个大纲性的东西作为需要考虑的方向,然后再逐步细化。oracle优化按重要性需要考
- 报错:Uncaught SyntaxError: Unexpected token o in JSON at position 1at JS
- 1、我的源码在 /home/topsec/Documents/php-7.0.11 ,安装位置在 /usr/local/php7, php.
- 如果让一个ASP页面以https开始,则在该ASP页面最顶部添加如下代码: <%Response.Buffer =
- 用途:图片经常使用onload来改变宽度,但这样会出现图片的闪烁,这个简单的类就是用来解决这个问题的。函数loadImage,用来加载图片,
- 在 Internet 连接无处不在的今天,我们忽然有了另外一个需求,离线 Web。Gmail, Google Reader, Zoho 这些
- 这个格式是我自创的,经常有人问我为什么,这里做个简单总结:1、分类,一个模块或者同类功能定义为一类定义,每类定义之间用段落隔开。2、分级,每
- 我们经常会在登录一个网站的时候被引导页挡住前进的脚步,这一点在上个世纪到本世纪初的网站中尤其明显,特别是在企业网站里,几乎每个企业网站都会有
- Golang Goroutine和线程的区别 Golang,轻松学习一、Golang Goroutine?当使用者分配足够多的任务,系统能自
- 简化了一下 YUI3 中的沙箱实现 方式:if (typeof Sandbox === 'undefined' || !Sa
- PayPal支付功能其实一直在更新文档和接口,这里说的是一个简单的支付功能大概流程如下1,在网站的结账页面,设置一个提交到PayPal网站的
- 1. 排序有什么用“排序”这个专业名词原本是来源于计算机程序操作中的,是一种很常见的算法设计,当然,对交互设计来说,探讨冒泡排序和堆排序之间
- time模块中的三种时间表示方式:时间戳结构化时间对象格式化时间字符串1.时间戳时间戳1970.1.1到指定时间到间隔,单位是秒import
- 很久以前就知道微软的Petshop的很经典,昨天抽出时间去学习,一开始还真的不适应,什么成员资格,还真的看不太懂,运行petshop想从登陆
- asp之家注:那么为什么要使用分页呢?当记录不多的时候,如10个或20个,我们可以也没必要使用分页来显示数据,但是数据是在不断增加的,当到了