Sentiment Analysis
Learn to detect opinions and emotions in text using VADER, ML, and Transformers.
Understanding Emotions in Text
Sentiment Analysis (Opinion Mining) automatically detects the emotional tone in text. It powers brand monitoring, product reviews, and social media analytics worldwide.
Positive
"This product is absolutely amazing! Best purchase ever."
Score: +0.92
Neutral
"The product arrived on Tuesday. It is a standard device."
Score: +0.05
Negative
"Terrible quality. Broke after two days. Complete waste of money."
Score: -0.88
Level 1 — VADER: Rule-Based Lexicon
VADER is a hand-crafted sentiment lexicon requiring zero training data. It automatically handles emojis, ALL CAPS, and negation.
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')
sia = SentimentIntensityAnalyzer()
sentences = [
"I LOVE this phone! Absolutely fantastic!",
"The battery life is not bad, but the camera is disappointing.",
"Worst product ever. Complete garbage. DO NOT BUY!",
"It's okay I guess. Nothing special.",
]
for sent in sentences:
scores = sia.polarity_scores(sent)
compound = scores['compound']
label = "POSITIVE" if compound >= 0.05 else ("NEGATIVE" if compound <= -0.05 else "NEUTRAL")
print(f"[{label:>8}] ({compound:+.3f}) {sent[:50]}")
Level 2 — Aspect-Based Sentiment Analysis (ABSA)
Instead of one overall score, ABSA identifies specific aspects and assigns individual sentiments to each.
Restaurant Review Example
"The food was incredibly delicious, but the service was painfully slow and the price is way too expensive."
| Aspect | Sentiment | Keywords |
|---|---|---|
| Food | POSITIVE (+0.91) | delicious |
| Service | NEGATIVE (-0.78) | slow |
| Price | NEGATIVE (-0.65) | expensive |
Level 3 — Transformer Sentiment (Best Accuracy)
from transformers import pipeline
sentiment = pipeline("sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english")
reviews = [
"Absolutely love it! Works perfectly and arrived fast.",
"Not great, not terrible. Does the job I suppose.",
"Horrible. Completely broke after first use. Save your money.",
]
for review in reviews:
result = sentiment(review)[0]
print(f"[{result['label']}] ({result['score']:.1%}) {review[:55]}...")
Key Challenges
| Challenge | Example | Problem |
|---|---|---|
| Sarcasm | "Oh great, another Monday" | Positive words, negative intent. |
| Negation | "Not bad at all" | Grammatically negative, semantically positive. |
| Context | "The movie was predictable" | Good for some genres, bad for others. |
| Domain Shift | "This medicine is sick" | "Sick" means bad in context, amazing in slang. |