Sentiment analysis for sentences- positive, negative and neutral
Asked Answered
F

2

5

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!

Freddafreddi answered 9/12, 2015 at 23:16 Comment(0)
M
6

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.

Maniac answered 10/2, 2016 at 22:33 Comment(0)
S
4

Vader is a rule-based sentiment analysis tool that works well for social media texts as well regular texts.

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(tweets):
    vadersenti = analyser.polarity_scores(tweets)
    return pd.Series([vadersenti['pos'], vadersenti['neg'], vadersenti['neu'], vadersenti['compound']])

text = 'This goes beyond party lines.  Separating families betrays our values as Texans, Americans and fellow human beings'

print_sentiment_scores(text)

The results are:
0    0.2470
1    0.0000
2    0.7530
3    0.5067

The compound score is computed by summing the valence scores of each word in the lexicon, adjusted according to the rules, and then normalized to be between -1 (most extreme negative) and +1 (most extreme positive). This is the most useful metric if you want a single unidimensional measure of sentiment for a given sentence. Calling it a 'normalized, weighted composite score' is accurate

Though positive sentiment is derived with the compound score >= 0.05, we always have an option to determine the positive, negative & neutrality of the sentence, by changing these scores

I personally find Vader Sentiment to figure out the sentiment based on the emotions, special characters, emojis very well.

Shir answered 12/7, 2018 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.