2016-09-29 5 views
0

私はちょうど私のデータセットでベイダーのセンチメント分析を実行しました:NLTKの感情ベイダー:発注結果

from nltk.sentiment.vader import SentimentIntensityAnalyzer 
from nltk import tokenize 
sid = SentimentIntensityAnalyzer() 
for sentence in filtered_lines2: 
    print(sentence) 
    ss = sid.polarity_scores(sentence) 
    for k in sorted(ss): 
     print('{0}: {1}, '.format(k, ss[k]),) 
     print() 

ここに私の結果のサンプル:

Are these guests on Samsung and Google event mostly Chinese Wow Theyre 
boring 

Google Samsung 

('compound: 0.3612, ',) 

() 

('neg: 0.12, ',) 

() 


('neu: 0.681, ',) 


() 


('pos: 0.199, ',) 


() 

Adobe lose 135bn to piracy Report 


('compound: -0.4019, ',) 


() 


('neg: 0.31, ',) 


() 


('neu: 0.69, ',) 


() 


('pos: 0.0, ',) 


() 

Samsung Galaxy Nexus announced 

('compound: 0.0, ',) 

() 

('neg: 0.0, ',) 

() 

('neu: 1.0, ',) 

() 

('pos: 0.0, ',) 

() 

私は何回知りたいです「化合物」は、等しい、より大きい、またはゼロ未満である。

おそらくそれはおそらく非常に簡単だと私は知っていますが、私はPythonとPythonの一般的な新機能です。 私は必要なものを作成するためにさまざまな方法で試しましたが、解決策が見つかりません。

+0

が、(これはあなたの問題とは何の関係もありませんが、あなたを得る可能性がありますのPython 2とそれを実行します最終的にトラブルになります)。 – lenz

+0

アドバイスありがとうございます! –

答えて

1

これまでのPythonのやり方ではありませんが、Pythonで多くの経験がなければ、これが最も簡単に理解できると思います。基本的には、0の値を持つ辞書を作成し、それぞれのケースで値を増やします。

from nltk.sentiment.vader import SentimentIntensityAnalyzer 
from nltk import tokenize 
sid = SentimentIntensityAnalyzer() 
res = {"greater":0,"less":0,"equal":0} 
for sentence in filtered_lines2: 
    ss = sid.polarity_scores(sentence) 
    if ss["compound"] == 0.0: 
     res["equal"] +=1 
    elif ss["compound"] > 0.0: 
     res["greater"] +=1 
    else: 
     res["less"] +=1 
print(res) 
+1

私はこれがかなり不快だと思います。結局のところ、Pythonはすべてが理解しやすいことです!簡単な問題に対する洗練されたソリューションは必要ありません。 – lenz

+1

@lenz私は完全に同意します。しかし、Pythonのためにforループは3行のコード(少なくとも一見)で達成することができます。 –

+0

ありがとう、私はこれが最も簡単な方法だと思うし、それは完全に動作します! –

1

あなたはクラスごとの単純なカウンタを使用することができます(「結果のサ​​ンプルは、」間違っている場合、私はそれを書くための正しい方法を知らないので、私の質問を編集してください):

positive, negative, neutral = 0, 0, 0 

そして、文章ループ内で、化合物の値をテストし、対応するカウンタを増加させる:

... 
    if ss['compound'] > 0: 
     positive += 1 
    elif ss['compound'] == 0: 
     neutral += 1 
    elif ... 

0

私は文書によって表されているの不等式の種類を返す関数定義できます。対応のカウントをインクリメントするために、すべての文章の化合物のスコアにこれを使用すると

def inequality_type(val): 
    if val == 0.0: 
     return "equal" 
    elif val > 0.0: 
     return "greater" 
    return "less" 

を不等式。

from collections import defaultdict 

def count_sentiments(sentences): 
    # Create a dictionary with values defaulted to 0 
    counts = defaultdict(int) 

    # Create a polarity score for each sentence 
    for score in map(sid.polarity_scores, sentences): 
     # Increment the dictionary entry for that inequality type 
     counts[inequality_type(score["compound"])] += 1 

    return counts 

フィルタリングされた行で呼び出すことができます。

しかし、これは単にcollections.Counterを使用することによって回避することができます:あなたは、Python 3のコードを書いているように見えます

from collections import Counter 

def count_sentiments(sentences): 
    # Count the inequality type for each score in the sentences' polarity scores 
    return Counter((inequality_type(score["compound"]) for score in map(sid.polarity_scores, sentences))) 
+2

'collections.Counter'は2番目のステップを自明にします。 – alexis

+0

@alexisはい、非常に良い点!それを追加します。 – erip

+1

@eripありがとうございました。それは非常にうまくいく!しかし私は、アレックスのソリューションは、私のような誰かに理解して使いやすくし、コーディングの最初のステップを動かすと思います。 –