是否可以遍历一串词,使用情绪维达将它们分类为正面、负面或中性,然后如果它们是正面的,则将这些正面的词附加到列表中?下面的 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/