[英]Python -extract positive words from a string using sentiment vader
是否可以遍歷一串單詞,使用情緒維達將它們分類為正面、負面或中性,然后如果它們是正面的,則將這些正面詞附加到列表中? 下面的 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']
如果有人想要使用 TextBlob 的解決方案
from textblob import TextBlob
def word_polarity(test_subset):
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]
for word in test_subset:
testimonial = TextBlob(word)
if testimonial.sentiment.polarity >= 0.5:
pos_word_list.append(word)
elif testimonial.sentiment.polarity <= -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)
word_polarity(['20170412', 'great', 'bad', 'terrible', 'dog', 'stop', 'good'])
輸出 :
('正面:', ['很棒', '好'])
('中性 :', ['20170412', 'dog', 'stop'])
('否定:', ['糟糕', '糟糕'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.