You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Sentiment analysis (also called opinion mining) is the task of determining the emotional tone or attitude expressed in a piece of text. It is one of the most popular and commercially valuable applications of NLP.
| Type | Description | Output Example |
|---|---|---|
| Polarity detection | Positive, negative, or neutral | "Great movie!" → Positive |
| Fine-grained | Star rating or scale (1–5) | "Decent but not amazing" → 3 stars |
| Aspect-based | Sentiment per aspect of an entity | "Great food but slow service" → food: +, service: - |
| Emotion detection | Specific emotions (joy, anger, sadness, etc.) | "I'm furious about this!" → Anger |
| Challenge | Example |
|---|---|
| Sarcasm | "Oh wonderful, my flight is delayed again" → Negative (not positive) |
| Negation | "This is not a bad product" → Positive (double negative) |
| Context dependence | "This phone's battery lasts forever" (positive) vs "This meeting lasts forever" (negative) |
| Mixed sentiment | "The camera is amazing but the battery life is terrible" |
| Domain specificity | "Unpredictable plot" → Positive (for a thriller), Negative (for a manual) |
| Implicit sentiment | "I waited two hours for my order" → Negative (no explicit sentiment words) |
VADER is a rule-based sentiment analyser specifically tuned for social media text. It handles emojis, slang, capitalisation, and punctuation.
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
texts = [
"I love this product! It's absolutely amazing!",
"This is the worst experience I've ever had.",
"The movie was okay, nothing special.",
"GREAT job!!! :D",
]
for text in texts:
scores = sia.polarity_scores(text)
print(f"{text}")
print(f" Scores: {scores}")
print()
VADER output format:
| Score | Meaning |
|---|---|
| neg | Proportion of negative sentiment |
| neu | Proportion of neutral sentiment |
| pos | Proportion of positive sentiment |
| compound | Overall sentiment score (-1 to +1) |
Tip: The compound score is the most useful. Typically: compound >= 0.05 → Positive, compound <= -0.05 → Negative, otherwise → Neutral.
from textblob import TextBlob
text = "The food was delicious but the service was terrible."
blob = TextBlob(text)
print(f"Polarity: {blob.sentiment.polarity:.2f}") # -1 to 1
print(f"Subjectivity: {blob.sentiment.subjectivity:.2f}") # 0 to 1
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.