โ† Back to Blog
pythonnlpsentiment-analysistextblobvader

Building a Simple Sentiment Analyzer with Python (TextBlob & VADER)

Building a Simple Sentiment Analyzer with Python (TextBlob & VADER)

Sentiment analysis is the process of determining the emotional tone behind a piece of text. It's used everywhere โ€” social media monitoring, product review analysis, customer feedback systems, and chatbots.

In this tutorial we'll build a command-line sentiment analyzer that classifies input as positive, negative, or neutral using two different NLP libraries and comparing their results.


What We're Building

A Python CLI app that:

  1. Accepts user text input in real time
  2. Runs it through TextBlob and VADER simultaneously
  3. Returns an emoji-tagged classification from both models

Libraries

TextBlob

Provides a simple API returning a polarity score from -1 (very negative) to +1 (very positive).

VADER (Valence Aware Dictionary and sEntiment Reasoner)

Specialised for informal text โ€” social media posts, slang, emojis. Returns a compound score for overall classification.


Setup

bash
pip install textblob vaderSentiment

Download TextBlob corpora:

bash
python -m textblob.download_corpora

Implementation

python
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# Initialise VADER
analyzer = SentimentIntensityAnalyzer()


def analyze_sentiment_textblob(text: str) -> str:
    polarity = TextBlob(text).sentiment.polarity
    if polarity > 0:
        return "Positive ๐Ÿ˜Š"
    elif polarity < 0:
        return "Negative ๐Ÿ˜ก"
    else:
        return "Neutral ๐Ÿ˜"


def analyze_sentiment_vader(text: str) -> str:
    compound = analyzer.polarity_scores(text)["compound"]
    if compound >= 0.05:
        return "Positive ๐Ÿ˜Š"
    elif compound <= -0.05:
        return "Negative ๐Ÿ˜ก"
    else:
        return "Neutral ๐Ÿ˜"


def main():
    print("\n๐Ÿง  Sentiment Analyzer (TextBlob + VADER)")
    print("Type '0' to quit\n")

    while True:
        text = input("How are you feeling? โ†’ ")
        if text.strip().lower() == "0":
            print("Exiting. Goodbye! ๐Ÿ‘‹")
            break

        print(f"  TextBlob : {analyze_sentiment_textblob(text)}")
        print(f"  VADER    : {analyze_sentiment_vader(text)}\n")


if __name__ == "__main__":
    main()

Sample Output

text
๐Ÿง  Sentiment Analyzer (TextBlob + VADER)
Type '0' to quit

How are you feeling? โ†’ I love coding with Python
  TextBlob : Positive ๐Ÿ˜Š
  VADER    : Positive ๐Ÿ˜Š

How are you feeling? โ†’ This bug is driving me crazy
  TextBlob : Negative ๐Ÿ˜ก
  VADER    : Negative ๐Ÿ˜ก

How are you feeling? โ†’ The weather is okay
  TextBlob : Neutral ๐Ÿ˜
  VADER    : Positive ๐Ÿ˜Š

Note how VADER and TextBlob can disagree on borderline cases โ€” VADER is generally better for casual language.


When to Use Which

Scenario Recommended
Formal text, articles TextBlob
Tweets, comments, slang VADER
General purpose Both + compare

What's Next

This is your entry point into NLP. Once you're comfortable here, explore:

  • Hugging Face Transformers โ€” state-of-the-art sentiment models
  • Fine-tuning BERT on custom datasets
  • Streamlit to turn this into a web app
  • Batch processing CSV files of reviews

Sentiment analysis is one of the most practical NLP tasks โ€” you'll find it everywhere in real products.

Share this article
โ† Back to all articles