Sentiment analysis for sentences- positive, negative and neutral

Jacob Vasu picture Jacob Vasu · Dec 10, 2015 · Viewed 7k times · Source

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!

Answer

PaloDravecky picture PaloDravecky · Feb 10, 2016

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.