首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用python量化情感分析

用python量化情感分析
EN

Stack Overflow用户
提问于 2018-07-14 21:05:46
回答 1查看 986关注 0票数 2

我一直在用NLTK在python中做情绪分析,它只有正类、中性类和负面类,如果我们想做情感分析,并有一个数字来显示一个句子多少可以是负数或正数呢?把它看作是一个回归问题。是否有任何经过预先训练的图书馆可以这样做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-16 16:33:40

我知道有几种方法可以做到:

  • Vader将分数作为等级返回(介于0到1之间)
  • Stanford 返回一个分类(即0、1、2、3)。

NLTK方式:

代码语言:javascript
运行
复制
from nltk.sentiment.vader import SentimentIntensityAnalyzer as sia
sentences = ['This is the worst lunch I ever had!',
             'This is the best lunch I have ever had!!',
             'I don\'t like this lunch.',
             'I eat food for lunch.',
             'Red is a color.',
             'A really bad, horrible book, the plot was .']

hal = sia()
for sentence in sentences:
    print(sentence)
    ps = hal.polarity_scores(sentence)
    for k in sorted(ps):
        print('\t{}: {:>1.4}'.format(k, ps[k]), end='  ')
    print()

示例输出:

代码语言:javascript
运行
复制
This is the worst lunch I ever had!
    compound: -0.6588   neg: 0.423      neu: 0.577      pos: 0.0  

斯坦福-NLP,Python的一种方式:

(请注意,这种方式要求您启动CoreNLP服务器的一个实例来运行,例如:java -mx1g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000)

代码语言:javascript
运行
复制
from pycorenlp import StanfordCoreNLP
stanford = StanfordCoreNLP('http://localhost:9000')

for sentence in sentences:
    print(sentence)
    result = stanford.annotate(sentence,
                               properties={
                                'annotators': 'sentiment',
                                'outputFormat': 'json',
                                'timeout': '5000'
                               })
    for s in result['sentences']:
        score = (s['sentimentValue'], s['sentiment'])
    print(f'\tScore: {score[0]}, Value: {score[1]}')

示例输出:

代码语言:javascript
运行
复制
This is the worst lunch I ever had!
    Score: 0, Value: Verynegative
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51343373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档