Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sentiment analysis for sentences- positive, negative and neutral

I'm designing a text classifier in Python using NLTK. One of the features considered in every sentence is it's sentiment. I want to weight sentences with either positive or negative sentiments more that those without any sentiment(neutral sentences). Using the movie review corpus along with the naive bayes classifier results in only positive and negative labels. I tried using demo_liu_hu_lexicon in nltk.sentiment.utils but the function does not return any values but instead prints it to the output and is very slow. Does anyone know of a library which gives some sort of weight to sentences based on sentiment?

Thanks!

like image 771
Jacob Vasu Avatar asked Dec 09 '15 23:12

Jacob Vasu


1 Answers

Try the textblob module:

from textblob import TextBlob
text = '''
These laptops are horrible but I've seen worse. How about lunch today? The food was okay.
'''

blob = TextBlob(text)
for sentence in blob.sentences:
    print(sentence.sentiment.polarity)
# -0.7
# 0.0
# 0.5

It uses the nltk library to determine the polarity - which is a float measure ranging from -1 to 1 for the sentiment. Neutral sentences have zero polarity. You should be able to get the same measure directly from nltk.

like image 116
PaloDravecky Avatar answered Nov 26 '22 06:11

PaloDravecky