python - 使用 python 量化情感分析

标签 python nlp nltk stanford-nlp sentiment-analysis

我一直在Python中使用NLTK进行情感分析,它只有积极、中立和消极类别,如果我们想做情感分析并用一个数字来显示一个句子可以有多少消极或积极的程度,该怎么办?有点将其视为回归问题。有没有经过预先培训的库可以这样做?

最佳答案

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

  • Vader 以等级形式返回分数(0 到 1 之间)
  • Stanford NLP 返回分类(即 0、1、2、3)。

NLTK 方式:

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()

示例输出:

This is the worst lunch I ever had!
    compound: -0.6588   neg: 0.423      neu: 0.577      pos: 0.0  

斯坦福自然语言处理,Python 方式:

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

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]}')

示例输出:

This is the worst lunch I ever had!
    Score: 0, Value: Verynegative

关于python - 使用 python 量化情感分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51343373/

相关文章:

python - 使用python在excel中生成图形

python - 如何在 Cython 中初始化固定大小的整数 numpy 数组?

python - 我应该使用哪个 Enthought EPD 发行版?

text - 如何使用 NLP 将非结构化文本内容分成不同的段落?

Python NLTK 练习 : chapter 5

python - 使用 NLTK 简化法语 POS 标签集

python-2.7 - 如何在Python中测试一个单词是否是单数形式?

python - Python从声卡读取音频

java - OpenNLP 头规则

python - 如何将tfidf特征与自制特征结合起来