2017-04-27 2 views
0

単語の文字列を繰り返して、センチメントベーダーを使用して肯定的、否定的、または中立的に分類し、 ?以下のforループは、私が達成しようとしているものの非動作コードです。私はPythonの初心者ですので、誰でもこの作業を行う方法についてのガイダンスを提供できると大変感謝しています。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) 

ありがとうございました。

+0

出力は次のようになります。はAttributeError:「辞書」オブジェクトが属性「キー」 を持っていない感情ベイダーから出力されているので、私は上記の.KEY()フォーマットを使用してみましたキー値のペアであなたはこれを修正する方法を知っていますか? – learningcompsci

答えて

2
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'] 
+0

ありがとうございました。上記と同じコードを実行すると、あなたの出力よりも中立の出力が異なります。私の出力はニュートラルです:['20170412'、 'great'、 'terrible'、 'dog'、 'stop'、 'good']です。私のニュートラルな出力はtest_subset全体で、「bad」を除きます。私たちのアウトプットとそれを修正する方法との間に相違があるのはなぜか分かりますか? – learningcompsci

+0

申し訳ありません編集済みコード – SmartManoj

+0

素晴らしい作品です。助けてくれてありがとうございました。 – learningcompsci

関連する問題