AI Notes
Complete sentiment guide. Polarity classification, neural approaches. NLP 2024.
What is Sentiment Analysis?
Sentiment analysis (opinion mining) is the computational task of identifying and extracting subjective information from text—determining whether a piece of writing expresses a positive, negative, or neutral attitude toward a subject. It goes beyond simple keyword matching to understand the emotional tone, opinion strength, and target of sentiments expressed in natural language.
Sentiment analysis is commercially vital: companies monitor brand perception across millions of social media posts, product reviews drive purchasing decisions, and financial analysts gauge market sentiment from news. Understanding automated sentiment extraction is essential for modern NLP practitioners.
Levels of Sentiment Analysis
Document-Level
| Input | Entire document (review, article) |
| Output | Overall sentiment |
| and a compelling story. Highly recommended." | POSITIVE (0.92) |
Sentence-Level
| Input | Individual sentences |
| Output | Sentiment per sentence |
| "The camera quality is amazing." | POSITIVE |
| "However, the battery life is disappointing." | NEGATIVE |
| "The phone costs $999." | NEUTRAL |
Aspect-Based Sentiment Analysis (ABSA)
Approaches to Sentiment Analysis
Lexicon-Based (Rule-Based)
| Sentiment lexicon (word | score): |
| "excellent" | +3 |
| "good" | +2 |
| "okay" | +1 |
| "bad" | -2 |
| "terrible" | -3 |
| "not" | negate next word's score |
| "very" | multiply by 1.5 |
| "slightly" | multiply by 0.5 |
| Example | "The movie was not very good" |
| "movie" | neutral (0) |
| "not very good" | negate(1.5 × 2) = -3 |
| Overall | NEGATIVE |
| Tools | VADER (social media), SentiWordNet, AFINN |
Machine Learning Approach
Features
- Bag of words / TF-IDF
- N-grams ("not good" captured as bigram)
- POS tags (adjectives carry sentiment)
- Punctuation (!!!)
- Emoticons (:-) → positive)
Classifiers
- Naive Bayes (simple, fast baseline)
- SVM (good with sparse features)
- Logistic Regression (interpretable)
Training data: Labeled reviews (5000+ labeled sentences)
Deep Learning Approach
| Word Embeddings | LSTM/CNN → Sentiment class |
| Embedding | Conv1D (capture n-gram patterns) |
| Learned filters detect | "not good", "highly recommend", "waste of" |
| State-of-the-art | ~95% accuracy on standard benchmarks |
Worked Example: Sentiment Classification
Using VADER (Valence Aware Dictionary)
| Text | "The food was great but the wait was absolutely unacceptable!!!" |
| "great" | compound +0.73 |
| "but" | negation signal (contrast) |
| "absolutely" | booster (×1.3) |
| "unacceptable" | -0.58 × 1.3 = -0.75 |
| "!!!" | intensity boost (+5% to dominant sentiment) |
| VADER compound score | -0.12 (slightly negative overall) |
| Positive | 0.32 (food was great) |
| Negative | 0.43 (wait unacceptable) |
| Neutral | 0.25 |
| Classification | NEGATIVE (but mixed — aspect-level analysis reveals more) |
Challenges in Sentiment Analysis
Negation Handling
| Simple: "don't" + "bad" | double negative → positive? |
| Actually | POSITIVE (litotes/understatement for "it's good") |
| "Not only" doesn't negate | VERY POSITIVE |
Sarcasm and Irony
| Keywords: "great" | positive? |
| Context: sarcasm | actually NEGATIVE |
| Lexicon says: "lovely" | positive |
| Reality | NEGATIVE (ironic) |
| Detection | Requires understanding context, norms, expectations |
Domain Adaptation
| Movie review | POSITIVE (thrilling!) |
| Software review | NEGATIVE (buggy!) |
| Book review | POSITIVE (amazing!) |
| Safety review | NEGATIVE (dangerous!) |
Comparative Sentiment
Evaluation Metrics
Binary (positive/negative)
Accuracy: correct / total
F1-score: harmonic mean of precision and recall
Fine-grained (1-5 stars)
MAE: Mean Absolute Error (predicted vs actual stars)
Correlation: Pearson/Spearman with human ratings
Aspect-based
Aspect detection accuracy + sentiment accuracy
Both must be correct for full credit
Applications
Brand Monitoring
Track sentiment about products/companies on social media
Alert when negative spike detected (PR crisis)
Product Development
Analyze reviews to identify pain points
"Battery" mentioned negatively 500× → prioritize battery improvement
Financial Markets
News sentiment correlates with stock movements
Aggregate sentiment as trading signal
Customer Service
Route angry customers (negative sentiment) to senior agents
Prioritize urgent complaints
Political Analysis
Track public opinion on policies/candidates
Analyze debate reception in real-time
Interview Questions
Q: How do you handle sarcasm in sentiment analysis? A: Sarcasm detection is an open problem. Approaches include: (1) Context modeling—if surrounding text contradicts the statement, likely sarcastic, (2) Incongruity detection—positive words in negative situations, (3) Author history—some users are frequently sarcastic, (4) Multimodal cues—in speech, tone and facial expressions help. Current best: fine-tuned transformers with sarcasm-labeled data achieve ~75% detection accuracy.
Q: Why is aspect-based sentiment analysis harder than document-level? A: It requires identifying relevant aspects, determining which sentiment words apply to which aspects, and handling mixed sentiments. "Great food, terrible service" has no single document sentiment. The model must parse relationships between opinion words and their targets, sometimes across sentence boundaries.
Q: How does VADER differ from machine learning approaches? A: VADER is rule-based with a human-validated sentiment lexicon. It handles social media well (emoticons, slang, punctuation emphasis) without training data. ML approaches learn from data and can capture domain-specific patterns but need labeled examples. VADER is a good baseline; fine-tuned BERT is state-of-the-art.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Sentiment Analysis - Opinion Mining.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, natural, language, processing, sentiment
Related Artificial Intelligence Topics