Python - 使用 sentiment vader 从字符串中提取正面词

标签 python

是否可以遍历一串词,使用情绪维达将它们分类为正面、负面或中性,然后如果它们是正面的,则将这些正面的词附加到列表中?下面的 for 循环是我想要完成的非工作代码。我是 Python 的初学者,所以如果有人可以提供有关如何完成这项工作的指导,我将不胜感激。

import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
test_subset=['20170412', 'great', 'bad', 'terrible', 'dog', 'stop', 'good']
test_subset_string_fixed=" ".join(str(x) for x in test_subset)
sid = SentimentIntensityAnalyzer()
pos_word_list=[]

for word in test_subset_string_fixed:
    if (sid.polarity_scores(test_subset_string_fixed)).key() == 'pos':
        pos_word_list.append(word)

非常感谢您的帮助。

最佳答案

import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
test_subset=['20170412', 'great', 'bad', 'terrible', 'dog', 'stop', 'good']

sid = SentimentIntensityAnalyzer()
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]

for word in test_subset:
    if (sid.polarity_scores(word)['compound']) >= 0.5:
        pos_word_list.append(word)
    elif (sid.polarity_scores(word)['compound']) <= -0.5:
        neg_word_list.append(word)
    else:
        neu_word_list.append(word)                

print('Positive :',pos_word_list)        
print('Neutral :',neu_word_list)    
print('Negative :',neg_word_list)    

输出:

Positive : ['great']
Neutral : ['20170412', 'terrible', 'dog', 'stop', 'good']
Negative : ['bad']

关于Python - 使用 sentiment vader 从字符串中提取正面词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646877/

相关文章:

javascript - Selenium 不会点击最上面的元素

python - 如果存在于另一个数组中,则从一个数组中删除元素,保留重复元素 - NumPy/Python

python - 交互式 python

python - 有没有支持异步请求的Python ElasticSearch客户端?

python - 使用 Sphinx 在 HTML 中创建上下文相关的帮助文件

javascript - Tensorflow:DenseFeatures 输入层的替代方案,可移植到 tfjs

python - 我应该坚持使用bash来开发高级Linux自动化脚本,还是切换到Python?

Python:用遗传算法解决背包优化问题?

python - 如何在 python 中部署函数及其依赖项?

python - 元胞自动机算法似乎不起作用