avatarAmy @GrabNGoInfo

Summary

The article compares the performance of TextBlob and VADER, two Python libraries for sentiment analysis, highlighting VADER's specific focus on social media content and its consideration of capitalization, repeated words, and emojis.

Abstract

The article "TextBlob vs. VADER for Sentiment Analysis Using Python" provides a comparative analysis of two popular sentiment analysis libraries in Python. It explains that both TextBlob and VADER use a lexicon-based approach to determine sentiment, assigning scores from -1 to 1, with -1 being highly negative and 1 being highly positive. The key difference noted is VADER's optimization for social media text, which includes recognizing sentiment in emojis, capitalization, and the repetition of words. Through a series of tests using sample sentences, the article demonstrates that VADER is more sensitive to the nuances of social media language, such as capitalization for emphasis and the use of exclamation marks, whereas TextBlob shows less variation in sentiment scores under these conditions. The article concludes by recommending VADER for more granular sentiment analysis, especially for content that includes elements common in social media communication.

Opinions

  • The author suggests that VADER is more suitable for analyzing social media content due to its ability to interpret emojis, capitalization, and repeated words, which are common in such contexts.
  • It is implied that VADER's consideration of punctuation and capitalization provides a more nuanced sentiment analysis compared to TextBlob.
  • The article posits that TextBlob's sentiment analysis might be less accurate for social media text, as it does not account for the emphasis indicated by capitalized words or repeated phrases.
  • The author's preference for VADER is evident, as it is described as providing more granular sentiment analysis, which is crucial for understanding the true sentiment behind social media posts.
  • There is a subtle critique of TextBlob's simplicity, as it does not increase sentiment scores for capitalized words or repeated phrases, potentially missing important sentiment cues.
  • The inclusion of emojis in the analysis shows that VADER is better equipped to handle modern digital communication nuances, while TextBlob's sentiment score decreased with the addition of emojis, indicating a potential shortcoming.

TextBlob vs. VADER for Sentiment Analysis Using Python

A comparison of sentiment scores between TextBlob and VADER

Photo by 🇸🇮 Janko Ferlič on Unsplash

TextBlob and VADER are two of the most widely used sentiment analysis Python libraries. Compared to machine learning approaches for sentiment analysis, TextBlob and VADER use a lexicon-based method. The lexicon approach has a mapping between words and sentiment, and the sentiment of a sentence is the aggregation of the sentiment of each term.

Lexicon sentiment analysis outputs a polarity score of -1 to 1, where -1 represents the highly negative sentiment, and 1 shows the highly positive sentiment. A value near 0 represents neutral sentiment.

A critical difference between TextBlob and VADER is that VADER is focused on social media. Therefore, VADER puts a lot of effort into identifying the sentiments of content that typically appear on social media, such as emojis, repetitive words, and punctuations (exclamation marks, for example).

In this article, we will compare the performance of TextBlob and VADER using sample sentences and see which one performs better!

Resources for this post:

  • Video tutorial on YouTube
  • More video tutorials on NLP
  • More blog posts on NLP

Step 1: Import Libraries

First, we need to install the vaderSentiment package.

pip install vaderSentiment

You will see the output below after it was successfully installed. Note that your version might be different than mine.

Successfully installed vaderSentiment-3.3.2

Now import the packages for VADER and TextBlob.

# Import the packages for sentiment analysis
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from textblob import TextBlob

Step 2: Define Functions for VADER and TextBlob

Let’s create functions for VADER and TextBlob separately, and this will help us code more efficiently later.

# VADER
vader_sentiment = SentimentIntensityAnalyzer()
def vader_sentiment_scores(text):
  score = vader_sentiment.polarity_scores(text)
  return score['compound']
# TextBlob
def textblob_sentiment_scores(text):
  textblob_sentiment = TextBlob(text)
  score = textblob_sentiment.sentiment.polarity
  return score

Step 3: Check Sentiment Difference

We get the sentiment score for a sentence using VADER and TextBlob separately. Both give us a positive sentiment score, and VADER has a higher value than TextBlob.

text = 'grabngoinfo.com is a fantastic website for step by step machine learning tutorials.'
print(f"Sentence: {text} \nVADER sentiment score: {vader_sentiment_scores(text)} \nTextBlob sentiment score: {textblob_sentiment_scores(text)}")

Output:

Sentence: grabngoinfo.com is a fantastic website for step by step machine learning tutorials.
VADER sentiment score: 0.5574
TextBlob sentiment score: 0.4

Step 4: Check Impact of Capitalization

In this step, we use the same sentence. Just change the word “fantastic” to upper case. VADER considers the capitalized version to have stronger sentiment and increased the sentiment score. At the same time, TextBlob did not distinguish the sentiment between the upper and lower case version of the word.

text = 'grabngoinfo.com is a FANTASTIC website for step by step machine learning tutorials.'
print(f"Sentence: {text} \nVADER sentiment score: {vader_sentiment_scores(text)} \nTextBlob sentiment score: {textblob_sentiment_scores(text)}")

Output:

Sentence: grabngoinfo.com is a FANTASTIC website for step by step machine learning tutorials.
VADER sentiment score: 0.6523
TextBlob sentiment score: 0.4

Step 5: Check Impact of Repeated Words

In this step, we repeated the word “FANTASTIC” three times to see if that impacts the sentiment score. As a result, VADER increased the score from 0.6523 to 0.9325. However, the TextBlob sentiment score barely increased. The results show that VADER considers repeated words to have stronger sentiment, and TextBlob does not account for the repeated words.

text = 'grabngoinfo.com is a FANTASTIC FANTASTIC FANTASTIC website for step by step machine learning tutorials.'
print(f"Sentence: {text} \nVADER sentiment score: {vader_sentiment_scores(text)} \nTextBlob sentiment score: {textblob_sentiment_scores(text)}")

Output:

Sentence: grabngoinfo.com is a FANTASTIC FANTASTIC FANTASTIC website for step by step machine learning tutorials.
VADER sentiment score: 0.9325
TextBlob sentiment score: 0.4000000000000001

Step 6: Check Impact of Punctuation

In this step, we changed the punctuation from period to exclamation mark, and the sentiment score increased for both VADER and TextBlob.

text = 'grabngoinfo.com is a FANTASTIC FANTASTIC FANTASTIC website for step by step machine learning tutorials!'
print(f"Sentence: {text} \nVADER sentiment score: {vader_sentiment_scores(text)} \nTextBlob sentiment score: {textblob_sentiment_scores(text)}")

Output:

Sentence: grabngoinfo.com is a FANTASTIC FANTASTIC FANTASTIC website for step by step machine learning tutorials!
VADER sentiment score: 0.9359
TextBlob sentiment score: 0.43333333333333335

Step 7: Check the Impact of Emojis

In this step, we added three emojis, a thumbs up, a star eyes, and a heart, at the end of the sentence. People use these three emojis usually when there is a positive sentiment. However, we can see that VADER’s sentiment score did not increase, and TextBlob’s sentiment score even decreased!

Now let’s try some smiley face emojis. It looks like Textblob did not change sentiment, and VADER increased the positive sentiment from 0.9359 to 0.9538.

Summary

Based on the comparison above, we can see that VADER provides more granular sentiment than TextBlob and takes capitalization, repeated words, and emoji into consideration when evaluating the sentiment of the text.

If you are interested in the video version of the tutorial, please check out the video below. Vist the GrabNGoInfo website or GrabNGoInfo YouTube Channel for more machine learning tutorials.

Recommended tutorials

Naturallanguageprocessing
Sentiment Analysis
Textblob
Vader
Recommended from ReadMedium