Python Sentiment Analysis with Vader

I was researching on sentiment analysis and stumbled upon the suggestion to use Vader. Vader stands for Valence Aware Dictionary and sEntiment Reasoner. It is a is a pre-built sentiment analysis tool specifically designed for social media text.

VADER is particularly useful for short and informal text, such as social media posts or reviews. For more sophisticated sentiment analysis tasks, one may consider using machine learning models trained on larger datasets.

Code Setup

Install nltk package using the following command.

pip install nltk

Create a file called sentiment_analysis.py and put the following code.

from nltk.sentiment.vader import SentimentIntensityAnalyzer


import nltk
nltk.download('vader_lexicon')

# Create a SentimentIntensityAnalyzer object
sid = SentimentIntensityAnalyzer()

# Example sentence
sentence = "VADER is a great tool for sentiment analysis!"

# Get sentiment scores
sentiment_scores = sid.polarity_scores(sentence)

# Print sentiment scores
compound_score = sentiment_scores['compound']

if(compound_score > 0.05):
print('Positive sentiment')
elif(compound_score < -0.05):
print('Negative sentiment')
else:
print('Neutral sentiment')

Validation

Run the program with the following command.

python sentiment_analysis.py

It will print the following output.

Positive sentiment

Now lets change the example sentance as shown below.

# Example sentence

sentence = "VADER is not so great tool for sentiment analysis!"

Run the program again, and you will see

Negative sentiment

Now lets change the example sentance as shown below. Its like a statement made, neither positive nor negative.

# Example sentence

sentence = "VADER is a tool for sentiment analysis!"

When you check the result, you will see..

Neutral sentiment

Conclusion

If you want to run sentiment analysis on small text, then you can use Vader. If you want to fine tune the vader lexicon, you may need to override in times. Lets see some fun with vader tool in the upcoming posts.