How to Analyze Sentiment using VADER in Python

Posted in /  

How to Analyze Sentiment using VADER in Python
vinaykhatri

Vinay Khatri
Last updated on December 25, 2024

    Natural Language Processing is one of the applications and domain of Machine Learning. Analyzing a text and finding sentiment out of it, can be performed using complex Machine Learning algorithms, but luckily there is an Open Source Python library vaderSentiment , which can calculate the sentiments of the given sentence. We can use this library through a list of sentences and calculate the positive, negative, and overall(compound) score of the individual sentences.

    The VADER stands for V alence A ware D ictionary and s E ntiment R easoner, and it is a very powerful yet straightforward tool, which is specially designed to read and calculate the statements' sentiments expressed on the social media platforms. With the help of Python web scraping and vaderSentiment library, you can check out all the comments and reactions of people on a specific post.

    Here in this Python tutorial, we will not be scraping any web-page, here we are directly applying the vaderSentiment polarity_scores method on the pre-specified sentences and calculating the polarity or sentiment score in percentage. +ve percentage represents positive sentiments. -ve percentage represents negative sentiments. 0% represent neutral statements. But before jumping to the Python implementation let's install the required library.

    How to Analyze Sentiment using VADER in Python

    Install vaderSentiment Library

    vaderSentiment is an Open Source Python third Party library, it is generally used to calculate or analyze the human sentiments in the sentences. This is a Python tutorial so I am assuming Python is installed in your system and your pip terminal command also works. Run the following pip command on your command prompt or terminal to install the library.

    pip install vaderSentiment

    Now you are all set, open your best Python ide or text editor and start coding.

    Python Implementation

    Let's begin with importing the required library module.

    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

    The SentimentIntensityAnalyzer is the subclass of vaderSentiment library. Now, initialize the SentimentIntensityAnalyzer object for further analysis.

    # initialize the object 
    analyzer = SentimentIntensityAnalyzer()
    Now we will define a list of sentences, which sentiments we want to analyze.
    
    sentences_list = [
        "Food is all about life",   #neutral
        "This food is disgusting, throw it away",   #positive
        "This food is amazing and tasty, I'm loving it ",   #negetive
    
        "You are a Human",  #neutral
        "you are looking freaking awesome",  #positive
        "you are looking utterly awful"  #negetive
    ]

    Now we will loop through every sentence present in the sentances_list and analyze them with the help of polarity_scores() method.

    for sentence in sentences_list:
    
        #analyze sentence 
        analyzed  = analyzer.polarity_scores(sentence)
    
        #get ovrall sentiment score
        sentiment_score = analyzed["compound"]
        print("Sentence : ", sentence)
        print(f"Sentiment Score: {round(sentiment_score*100, 2)}% \n")

    The analyzer.polarity_scores(sentence) method will return a dictionary containing different scores for positive, negative, and overall as compound for the statement. Now put all the code together and execute.

    #Python program to Analyze Sentiments of the sentence.

    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    
    # initialize the object
    analyzer = SentimentIntensityAnalyzer()
    sentences_list = [
        "Food is all about life",   #neutral
        "This food is disgusting, throw it away",   #positive
        "This food is amazing and tasty, I'm loving it ",   #negetive
    
        "You are a Human",  #neutral
        "you are looking freaking awesome",  #positive
        "you are looking utterly awful"  #negetive
    ]
    
    for sentence in sentences_list:
    
        #analyze sentence
        analyzed  = analyzer.polarity_scores(sentence)
    
        #get ovrall sentence  score
        sentiment_score = analyzed["compound"]
        print("Sentence : ", sentence)
        print(f"Sentiment Score: {round(sentiment_score*100, 2)}% \n")

    Output

    Sentence : Food is all about life
    Sentiment Score: 0.0%
    
    Sentence : This food is disgusting, throw it away
    Sentiment Score: -52.67%
    
    Sentence : This food is amazing and tasty, I'm loving it 
    Sentiment Score: 82.71%
    
    Sentence : You are a Human
    Sentiment Score: 0.0%
    
    Sentence : you are looking freaking awesome
    Sentiment Score: 31.82%
    
    Sentence : you are looking utterly awful
    Sentiment Score: -50.95%

    From the output, you can see that the neutral sentences have 0% sentiment score, where the positive and negative sentences show sentiment percentage in positive and negative. In the above example, we have got the overall sentiment score of the sentence by accessing the compound key of the analyzed dictionary. The polarity_scores() method also returns the individual positive and negative scores for the sentence, and those can be accessed through pos and neg keys. Now let's take the same statements_list and display the positive, negative, and overall sentiment score of every statement.

    #Python program to Analyze Positive, negative, and overall Sentiments of the sentence.

    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    
    # initialize the object
    analyzer = SentimentIntensityAnalyzer()
    sentences_list = [
        "Food is all about life",   #neutral
        "This food is disgusting, throw it away",   #positive
        "This food is amazing and tasty, i'am loving it ",   #negetive
    
        "You are a Human",  #neutral
        "you are looking freaking awesome",  #positive
        "you are looking utterly awful"  #negetive
    ]
    
    for sentence in sentences_list:
    
        #analyze sentence
        analyzed  = analyzer.polarity_scores(sentence)
    
        #get positive sentiment  score
        positive = analyzed["pos"]
    
        #get negetive sentiment  score
    
        negetive = analyzed["neg"]
    
        #get neutral sentiment  score
        neutral = analyzed["neu"]
    
        #get ovrall sentiment  score
        overall = analyzed["compound"]
    
        print("Sentence : ", sentence)
        print(f"Positive Score: {round(positive*100, 2)}%", end=" | ")
        print(f"Negetive Score: {round(negetive*100, 2)}%", end=" | ")
        print(f"Neutral Score: {round(neutral*100, 2)}%", end=" | ")
        print(f"Overall Score: {round(overall*100, 2)}% \n")

    Output

    Sentence :  Food is all about life
    Positive Score: 0.0% | Negetive Score: 0.0% | Neutral Score: 100.0% | Overall Score: 0.0% 
    
    Sentence :  This food is disgusting, throw it away
    Positive Score: 0.0% | Negetive Score: 36.2% | Neutral Score: 63.8% | Overall Score: -52.67% 
    
    Sentence :  This food is amazing and tasty, i'am loving it 
    Positive Score: 52.4% | Negetive Score: 0.0% | Neutral Score: 47.6% | Overall Score: 82.71% 
    
    Sentence :  You are a Human
    Positive Score: 0.0% | Negetive Score: 0.0% | Neutral Score: 100.0% | Overall Score: 0.0% 
    
    Sentence :  you are looking freaking awesome
    Positive Score: 41.4% | Negetive Score: 28.3% | Neutral Score: 30.3% | Overall Score: 31.82% 
    
    Sentence :  you are looking utterly awful
    Positive Score: 0.0% | Negetive Score: 45.2% | Neutral Score: 54.8% | Overall Score: -50.95% 

    Conclusion

    In this Python tutorial, you learned how can you use the Python vaderSentiment library to analyze the sentiment of the sentence. The polarity_scores() method returns a dictionary containing the different score base score for the sentence, and you can also analyze the negative, positive, neutral, and overall sentiments of a sentence. This library comes in very handy when you want to analyze people's reactions to a post or tweet.

    People are also reading:

    Leave a Comment on this Post

    0 Comments