Sentiment Analysis using VADER in Python

Learn how you can easily perform sentiment analysis on text in Python using vaderSentiment library.
  · 4 min read · Updated may 2022 · Natural Language Processing

Welcome! Meet our Python Code Assistant, your new coding buddy. Why wait? Start exploring now!

Text-Based data is known to be abundant since it is generally practically everywhere, including social media interactions, reviews, comments and even surveys.

Sentences hold many valuable information that may have a huge impact on the decision making process of a given company, since it is a way to perform customer analytics to get to better know your users hence giving them better products in the future. 

In this tutorial, we will learn on how to extract the sentiment score (-1 for negative, 0 for neutral and 1 for positive) from any given text using the vaderSentiment library. 

VADER stands for Valence Aware Dictionary and sEntiment Reasoner, which is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media, and works well on text from other domains.

Installing the requirements for this tutorial:

pip install vaderSentiment

The nice thing about this library is that you don't have to train anything in order to use it, you'll soon realize that it is pretty straightforward to use it, open up a new Python file and import SentimentIntensityAnalyzer class:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# init the sentiment analyzer
sia = SentimentIntensityAnalyzer()

We will create a list of sentences on which we will apply sentiment analysis using the polarity_score() method from SentimentIntensityAnalyzer class.

sentences = [
    "This food is amazing and tasty !",
    "Exoplanets are planets outside the solar system",
    "This is sad to see such bad behavior"
]

for sentence in sentences:
    score = sia.polarity_scores(sentence)["compound"]
    print(f'The sentiment value of the sentence :"{sentence}" is : {score}')

polarity_score() method returns a float for the sentiment strength based on the input text, the result of running the above code is the following:

The sentiment value of the sentence :"This food is amazing and tasty !" is : 0.6239 
The sentiment value of the sentence :"Exoplanets are planets outside the solar system" is : 0.0
The sentiment value of the sentence :"This is sad to see such bad behavior" is : -0.765

We can also calculate the percentage of each sentiment present in that sentence using "pos", "neu" and "neg" keys after computing the polarity score.

for sentence in sentences:
    print(f'For the sentence "{sentence}"')
    polarity = sia.polarity_scores(sentence)
    pos = polarity["pos"]
    neu = polarity["neu"]
    neg = polarity["neg"]
    print(f'The percententage of positive sentiment in :"{sentence}" is : {round(pos*100,2)} %')
    print(f'The percententage of neutral sentiment in :"{sentence}" is : {round(neu*100,2)} %')
    print(f'The percententage of negative sentiment in :"{sentence}" is : {round(neg*100,2)} %')
    print("="*50)

Output:

For the sentence "This food is amazing and tasty !"
The percententage of positive sentiment in :"This food is amazing and tasty !" is : 40.5 %
The percententage of neutral sentiment in :"This food is amazing and tasty !" is : 59.5 %
The percententage of negative sentiment in :"This food is amazing and tasty !" is : 0.0 %
==================================================
For the sentence "Exoplanets are planets outside the solar system"
The percententage of positive sentiment in :"Exoplanets are planets outside the solar system" is : 0.0 %
The percententage of neutral sentiment in :"Exoplanets are planets outside the solar system" is : 100.0 %
The percententage of negative sentiment in :"Exoplanets are planets outside the solar system" is : 0.0 %
==================================================
For the sentence "This is sad to see such bad behavior"
The percententage of positive sentiment in :"This is sad to see such bad behavior" is : 0.0 %
The percententage of neutral sentiment in :"This is sad to see such bad behavior" is : 47.6 %
The percententage of negative sentiment in :"This is sad to see such bad behavior" is : 52.4 %
==================================================

Conclusion

In this tutorial you have learned:

  • Learned the importance of sentiment analysis in Natural Language Processing.
  • Learned to extract sentimental scores from a sentence using the VaderSentiment package in Python.

Learn alsoHow to Perform Text Classification in Python using Tensorflow 2 and Keras.

Happy Coding ♥

Want to code smarter? Our Python Code Assistant is waiting to help you. Try it now!

View Full Code Auto-Generate My Code
Sharing is caring!



Read Also



Comment panel

    Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!