Menu

Wednesday, November 24, 2021

Sentiment Analysis using VADER

Sentiment analysis (or opinion mining) is a natural language processing technique used to determine whether data is positive, negative or neutral. It’s often used by businesses to detect sentiment in social data, monitor brand reputation, and understand customers.

It is performed on textual data to help businesses monitor brand and product sentiment in customer feedback, and understand customer needs.

Example of Applications: Document Classification: Positive / Negative / Neutral!

     Sentiment Analysis: Libraries

  • NLTK (2001, University of Pennsylvania) : It stands for Natural Language Tool Kit. It is the most popular as well as most useful library for performing Sentiment Analysis. It does entity recognition, tokenizing, part-of-speech (POS) and Topic Segmentation. 
  • TextBlob (2013): TextBlob has a rule-based integrated sentiment analysis function with two properties:-Subjectivity, Polarity. VADER (Valence Aware Dictionary and sEntiment Reasoner) is the most popular approaches to sentiment analysis with TextBlob.
  • SpaCy (2015, Explosion AI): Unlike NLTK, SpaCy is focused on industrial usage and maintains a minimal effective toolset. (https://spacy.io/)
  • VADER (Valence Aware Dictionary and sEntiment Reasoner) : It is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media. It is fully open-sourced under the [MIT License]

 Know the terms

  • Compound score: It is a metric that calculates the sum of all the lexicon ratings which have been normalized between -1(most extreme negative) and +1 (most extreme positive).
  • Polarity Scores: Scores based on compounding score.
  • positive sentiment : (compound score >= 0.05) 
    neutral sentiment : (compound score > -0.05) and (compound score < 0.05) 
    negative sentiment : (compound score <= -0.05)

Steps

1)      Install: 

      pip install vaderSentiment

2)      Create Object Sentiment Analyzer:

analyzer = SentimentIntensityAnalyzer()

3)      Call method: polarity_scores that accepts sentences.

vs = analyzer.polarity_scores(sentence)

4)      It returns Sentiment Dictionary: Array ! With ‘neg’, ‘pos’, ‘neu’, ‘compound’ as dimensions 

sentiment_dict = analyzer.polarity_scores(sentence)

5)      Check required dimension of Sentiment Dictionary ! You have got sentiment.

Code:

# import SentimentIntensityAnalyzer class

# from vaderSentiment.vaderSentiment module.

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

 

# function to print sentiments

# of the sentence.

def sentiment_scores(sentence):

           # Create a SentimentIntensityAnalyzer object.

          sid_obj = SentimentIntensityAnalyzer()

          sentiment_dict = sid_obj.polarity_scores(sentence)

         

          print("Overall sentiment dictionary is : ", sentiment_dict)

          print("sentence was rated as ", sentiment_dict['neg']*100, "% Negative")

          print("sentence was rated as ", sentiment_dict['neu']*100, "% Neutral")

          print("sentence was rated as ", sentiment_dict['pos']*100, "% Positive")

 

          print("Sentence Overall Rated As", end = " ")

           # decide sentiment as positive, negative and neutral

          if sentiment_dict['compound'] >= 0.05 :

                   print("Positive")

           elif sentiment_dict['compound'] <= - 0.05 :

                   print("Negative")

           else :

                   print("Neutral")

 # Driver code

if __name__ == "__main__" :

           print("\n1st statement :")

          sentence = "Super fine day today."

           # function calling

          sentiment_scores(sentence)

 

          print("\n2nd Statement :")

          sentence = "study is going on as usual"

          sentiment_scores(sentence)

 

          print("\n3rd Statement :")

          sentence = "I am vey sad today."

          sentiment_scores(sentence)

No comments:

Post a Comment