情感分析(sentiment analysis)是2018年公布的计算机科学技术名词。

它可以根据文本内容判断出所代表的含义是积极的还是负面的,也可以用来分析文本中的意思是褒义还是贬义。

一般应用场景就是能用来做电商的大量评论数据的分析,比如好评率或者差评率的统计等等。

我们这里使用到的情感分析的模块是snownlp,为了提高情感分析的准确度选择加入了jieba模块的分词处理。

由于以上的两个python模块都是非标准库,因此我们可以使用pip的方式进行安装。

pip install jiebapip 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 + 1else:# 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
往期精彩

python数据保存:记录pandas数据分析完成后的轻量级数据保存!
知识记录:python如何通过反射机制处理对象?
如何使用Selenium IDE浏览器插件轻松完成脚本录制,轻松搞定自动化测试!

python情感分析:基于jieba的分词及snownlp的情感分析!相关推荐

  1. python影评_python爬虫及结巴分词《攀登者》影评分析

    <攀登者>影评爬取及分析 0.项目结构 其中simkai.ttf为字体文件,Windows查看系统自带的字体 C:\Windows\Fonts 一.爬取豆瓣影评数据 # -*- codin ...

  2. python设计jieba_python实现jieba库分词制作词云

    <老人与海>是我最喜爱的一部外国名著,该作围绕一位老年古巴渔夫,与一条巨大的马林鱼在离岸很远的湾流中搏斗而展开故事的讲述.尽管海明威笔下的老人是悲剧性的,但他身上却有着尼采"超人 ...

  3. 基于linux的qos编程接口研究与分析,基于Linux的QoS编程接口研究与分析(2)

    1.4 QoS在网络应用中的重要性 随着因特网的快速爆炸式发展,新兴的业务也层出不穷.因为网络正在从初期的单一的数据网络,向集成音频.视频.数据的宽带多媒体网络方向发展.而随着骨干网和接入网上带宽的不 ...

  4. 国内android应用商城中程序隐私泄露分析,基于网络流量的安卓应用隐私泄露分析...

    Privacy Leakage Analysis Of Android Apps Based In Network Traffic YUAN Chunhuan 1 袁春欢(1993-),女,主要研究方 ...

  5. python自然语言处理 分词_Python 自然语言处理(基于jieba分词和NLTK)

    Python 自然语言处理(基于jieba分词和NLTK) 发布时间:2018-05-11 11:39, 浏览次数:1038 , 标签: Python jieba NLTK ----------欢迎加 ...

  6. matlab降压启动,基于 Matlab 的笼形异步电动机降压启动分析

    原标题:基于 Matlab 的笼形异步电动机降压启动分析 基于 Matlab 的笼形异步电动机降压启动分析 陈滨掖 摘要:三相异步电动机全压启动时的瞬间大电流及启动转矩会对负载造成很大冲击.降压启动的 ...

  7. matlab 避雷针保护范围程序,基于MATLAB避雷针保护范围可视化设计与分析.doc

    基于MATLAB避雷针保护范围可视化设计与分析 基于MATLAB避雷针保护范围可视化设计与分析 摘要: 利用MATLAB设计避雷针保护范围可视化程序与界面,对避雷针保护范围采用折线法和滚球法进行对比分 ...

  8. Python爬取《你好李焕英》豆瓣短评并基于SnowNLP做情感分析

    爬取过程在这里: Python爬取你好李焕英豆瓣短评并利用stylecloud制作更酷炫的词云图 本文基于前文爬取生成的douban.txt,基于SnowNLP做情感分析. 依赖库: 豆瓣镜像比较快: ...

  9. python jieba 文本相似度_文本相似度分析(基于jieba和gensim)

    ##基础概念 本文在进行文本相似度分析过程分为以下几个部分进行, 文本分词 语料库制作 算法训练 结果预测 分析过程主要用两个包来实现jieba,gensim jieba:主要实现分词过程 gensi ...

最新文章

  1. linux系统操作常见问题(ubuntu和opensuse)
  2. brightness temperature
  3. 用户控件的后台代码关联使用CodeBehind还是CodeFile
  4. @ExceptionHandler
  5. requests库提示警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate ver
  6. 笨办法学 Python · 续 练习 14:双链表
  7. 数组求最大公约数c语言,C语言辗转相除法求2个数的最小公约数
  8. Codeforces Round #342 (Div. 2)
  9. 本地文件上传到阿里云生成网址
  10. 称呼大全,亲戚称呼,祖宗十八代的称呼!
  11. 字节跳动后端日常实习一二+hr面面经(已OC)
  12. Windows 10 下如何显示文件的后缀名
  13. Qt 教程(传智教育)
  14. 在ubuntu下连接e-SATA硬盘
  15. 平板电脑Viewpad10安装win7与Android双系统
  16. PyTorch Python API详解大全(持续更新ing...)
  17. 为什么要标准化用户故事格式?
  18. 母亲节不能陪在妈妈身边,我用css和js给妈妈做了一个爱心飘落
  19. 【信号采集】基于FPGA的高速信号采集系统
  20. 第十章 集成ISIS协议--10.1

热门文章

  1. 基于JAVA大学生规划平台计算机毕业设计源码+系统+lw文档+部署
  2. 特征工程——缺失值显示和填充(集中趋势(众数、平均数、中位数)、缺失值矩阵图、条形图、集中趋势填充)
  3. 怎样批量查询宅急送在途信息,并分析提前签收
  4. CCF CSP 202209
  5. 关于直通车与万相台的区别,你知道多少
  6. C语言 多线程实现TCP并发服务器
  7. kubernetes高可用架构
  8. Redis修改端口号后无法连接的问题
  9. 四年级计算机wps教案,四年级上册信息技术教案-《初识“WPS文字”》苏教版新版...
  10. 模块介绍之六轴陀螺仪MPU6050篇(STM32基本使用)