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:
- Accepts user text input in real time
- Runs it through TextBlob and VADER simultaneously
- 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
pip install textblob vaderSentiment
Download TextBlob corpora:
python -m textblob.download_corpora
Implementation
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
๐ง 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.