python 文本分析库_快速使用Python进行文本情感分析

文本情感分析是自然语言处理的一个重要部分,与语音情感分析类似,通过处理提取给定文本中的信息来衡量说话者/作者的态度和情绪,主要用于电影、商品以及社交媒体的用户评论分析等。

VADER是一个基于词典和规则的情感分析开源python库,该库开箱即用,不需要使用文本数据进行训练,安装好之后即可输入想要识别的文本进行情感分析。

与传统的情感分析方法相比,VADER具有很多优势:适用于社交媒体等多种文本类型

不需要任何训练数据

速度快,可以在线使用流数据

其Github代码地址与论文说明地址如下:

Github地址

https://github.com/cjhutto/vaderSentiment

论文地址

http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf

VADER安装

VADER已上传PYPI,可以直接通过pip进行安装pip install vaderSentiment

安装好以后,通过简单的三行代码即可实现你想要的文本情绪分析。即导入库、输入待测文本、打印输出情绪分类结果。例如下边一个简单的例子:from vaderSentiment import sentiment as vaderSentiment

sentences = [

"The plot was good, but the characters are uncompelling and the dialog is not great.",

"A really bad, horrible book.",

"At least it isn't a horrible book."

]

for sentence in sentences:

print (sentence,sentiment = vaderSentiment(sentence))

print ("\n\t" + str(sentiment))

输出结果:The plot was good, but the characters are uncompelling and the dialog is not great.

{'neg': 0.327, 'neu': 0.579, 'pos': 0.094, 'compound': -0.7042}

A really bad, horrible book.

{'neg': 0.791, 'neu': 0.209, 'pos': 0.0, 'compound': -0.8211}

At least it isn't a horrible book.

{'neg': 0.0, 'neu': 0.637, 'pos': 0.363, 'compound': 0.431}

由上边的小例子可以看到,vaderSentiment()返回四个值,即表示消极、积极、中性以及复合情绪值,通过这四个值来反映整体情绪状态。

VADER分析情绪的关键点:标点符号:使用标点符号可以增强情绪强度而不改变情绪。例如,“The food here is good!”比“The food here is good!!”更强烈。单词大写:与情感相关的单词使用大写字母会增加情绪强度。例如“The food here is GREAT!”传达的情感比“The food here is great!”要强。语气词:通过一些语气词来增加或减少影响情绪强度。转折词:使用像“but”这样的连词表示情绪的变化,例如“The food here is great,but the service is horrible”这句话中情绪喜忧参半,但转折词后的部分决定整体情绪评价。

表情符号:print(sentiment_analyzer_scores('?'))

print(sentiment_analyzer_scores('?'))

print(sentiment_analyzer_scores('☹️'))

#Output

I am ? today------------ {'neg': 0.0, 'neu': 0.476, 'pos': 0.524, 'compound': 0.6705}

?------------------ {'neg': 0.0, 'neu': 0.333, 'pos': 0.667, 'compound': 0.7184}

?------------------ {'neg': 0.275, 'neu': 0.268, 'pos': 0.456, 'compound': 0.3291}

☹️------------------ {'neg': 0.706, 'neu': 0.294, 'pos': 0.0, 'compound': -0.34}

?------------------ {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}

深度学习与Python,专注于深度学习、机器学习前沿知识与资讯

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值