如何解决如何为维达中的每个词典打印价分?
我正在尝试使用 vader 打印句子中每个词典(单词)的效价分数,但我在这个过程中感到困惑。我能够使用维达将句子中的单词分为正面、负面和中性。我也想打印价分。如何解决这个问题?
sid = SentimentIntensityAnalyzer()
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]
for word in tokenized_sentence:
if (sid.polarity_scores(word)['compound']) >= 0.1:
pos_word_list.append(word)
sid.score_valence(word)
elif (sid.polarity_scores(word)['compound']) <= -0.1:
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)
score = sid.polarity_scores(sentence)
print('\nscores:',score)
Positive: ['happy',1.3]
Neutral: ['paper','too','much',0]
Negative: ['missed',-1.2,'stupid',-1.9]
scores: {'neg': 0.491,'neu': 0.334,'pos': 0.175,'compound': -0.5848}
因此显示单词“happy”在句子中的效价得分为 1.3。
解决方法
如果您能提供您在代码中使用的句子,那就太好了。但是,我提供了一个句子,您可以将其替换为您的句子。
看看我的源代码:
import nltk
from nltk.tokenize import word_tokenize,RegexpTokenizer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
Analyzer = SentimentIntensityAnalyzer()
sentence = 'Make sure you stay happy and less doubtful'
tokenized_sentence = nltk.word_tokenize(sentence)
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]
for word in tokenized_sentence:
if (Analyzer.polarity_scores(word)['compound']) >= 0.1:
pos_word_list.append(word)
pos_word_list.append(Analyzer.polarity_scores(word)['compound'])
elif (Analyzer.polarity_scores(word)['compound']) <= -0.1:
neg_word_list.append(word)
neg_word_list.append(Analyzer.polarity_scores(word)['compound'])
else:
neu_word_list.append(word)
neu_word_list.append(Analyzer.polarity_scores(word)['compound'])
print('Positive:',pos_word_list)
print('Neutral:',neu_word_list)
print('Negative:',neg_word_list)
score = Analyzer.polarity_scores(sentence)
print('\nScores:',score)
从我从你的问题中了解到的,我猜你可能正在寻找这样的输出。如果没有,请告诉我。
输出:
Positive: ['sure',0.3182,'happy',0.5719]
Neutral: ['Make',0.0,'you','stay','and','less',0.0]
Negative: ['doubtful',-0.34]
Scores: {'neg': 0.161,'neu': 0.381,'pos': 0.458,'compound': 0.5984}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。