NLTK 有一个用于sentiwordnet 的 API,但这可能对您的任务没有帮助。 Sentiwordnet 带有它的扭结。例如
>>> from nltk.corpus import sentiwordnet as swn
# Kind of useful.
>>> swn.senti_synsets('happy', 'a')
[SentiSynset('happy.a.01'), SentiSynset('felicitous.s.02'), SentiSynset('glad.s.02'), SentiSynset('happy.s.04')]
>>> swn.senti_synsets('happy', 'a')[0].synset.definition()
u'enjoying or showing or marked by joy or pleasure'
>>> swn.senti_synsets('happy', 'a')[0].pos_score()
0.875
>>> swn.senti_synsets('happy', 'a')[0].neg_score()
0.0
>>> swn.senti_synsets('happy', 'a')[0].obj_score()
0.125
# Not very useful...
>>> swn.senti_synsets('slow', 'a')
>>> swn.senti_synsets('slow', 'a')[0].synset.definition()
u'not moving quickly; taking a comparatively long time'
>>> swn.senti_synsets('slow', 'a')[0].pos_score()
0.0
>>> swn.senti_synsets('slow', 'a')[0].neg_score()
0.0
>>> swn.senti_synsets('slow', 'a')[0].obj_score()
1.0
NLTK 中还有VADER algorithm http://www.nltk.org/howto/sentiment.html:
>>> import nltk
>>> nltk.download('vader_lexicon')
>>> from nltk.sentiment.vader import SentimentIntensityAnalyzer
>>> sid = SentimentIntensityAnalyzer()
>>> sid.polarity_scores('happy')
{'neg': 0.0, 'neu': 0.0, 'pos': 1.0, 'compound': 0.5719}
>>> sid.polarity_scores('sad')
{'neg': 1.0, 'neu': 0.0, 'pos': 0.0, 'compound': -0.4767}
>>> sid.polarity_scores('sad man')
{'neg': 0.756, 'neu': 0.244, 'pos': 0.0, 'compound': -0.4767}
>>> sid.polarity_scores('not so happy')
{'neg': 0.616, 'neu': 0.384, 'pos': 0.0, 'compound': -0.4964}