Sentiment Analysis
What Is Sentiment Analysis?
Sentiment analysis (also called opinion mining) is the task of automatically determining the attitude expressed in a piece of text. Given a review, tweet, support ticket, or comment, we want to know: is the author happy, unhappy, or neutral about whatever they are talking about? It powers product-review dashboards, brand monitoring, customer-support triage, and financial news analysis.
The most common form is polarity classification: labeling text as positive, negative, or neutral. But the task comes in several richer flavors:
Polarity (coarse)
Positive / negative / neutral. The classic 3-way (or binary) classification.
Fine-grained
An ordinal scale, e.g. 1–5 stars: very negative, negative, neutral, positive, very positive. Captures intensity, not just direction.
Aspect-based (ABSA)
Sentiment toward specific aspects of an entity. “The food was great but the service was slow” is positive on food, negative on service — a single overall label would hide that.
Emotion detection
Beyond polarity, classify discrete emotions — joy, anger, fear, sadness, surprise, disgust (e.g. Ekman's basic emotions). Two negative texts can be angry vs. sad.
Approach 1: Lexicon-Based Scoring
The simplest method needs no training data. You start from a sentiment lexicon: a dictionary mapping words to polarity scores (e.g. amazing → +3, terrible → −3). Popular lexicons include AFINN, the NRC Emotion Lexicon, SentiWordNet, and VADER. To score a sentence you look up each word and add up its polarity:
A positive sum → positive text. But a naive sum is wrong surprisingly often, because words modify each other. Two corrections are essential:
Negation handling
A negator like not, never, or no flips the sign of the sentiment words that follow it within a small window. “not good” should score around −2, not +2.
Intensifiers & downtoners
very, extremely, really scale up the next word's magnitude; slightly, barely scale it down. “very good” > “good”.
The demo below implements exactly this logic. It is transparent on purpose: every number you see is a real lookup in a small JavaScript lexicon, with negation and intensifier rules applied. Edit the text and watch the score recompute.
Interactive: Lexicon-Based Sentiment Scorer
Each word is colored by its contribution. Negators turn the next few words' signs around; intensifiers scale their magnitude. The running total Σ and the final label update live as you type.
Approach 2: Machine Learning Classifiers
Lexicons are interpretable but brittle — they ignore word order, miss domain-specific words, and require hand-tuned rules. With labeled examples we can instead learn the mapping. The classic pipeline is bag-of-words features + a linear classifier.
Bag-of-words (BoW) features
Represent each document as a vector of word counts (or TF-IDF weights), discarding word order. A vocabulary of V words gives a length-V sparse vector. N-grams (e.g. bigrams) capture a bit of order, which helps with phrases like “not good”.
Logistic Regression
Learns a weight per word and predicts P(positive) = σ(w·x + b), where σ(z) = 1/(1+e⁻ᶻ). Trained by minimizing cross-entropy loss. Weights are interpretable: large positive weight on “excellent”, large negative on “refund”. A strong, simple baseline for text.
Naive Bayes
Applies Bayes' rule with a (“naive”) conditional-independence assumption: P(class | doc) ∝ P(class) · Πw P(w | class). Multinomial NB on word counts is fast, needs little data, and is a famously tough baseline for text classification.
These models learn domain-appropriate associations automatically — in movie reviews “unpredictable” is positive; on a car's steering it is negative. But standard BoW still throws away most word order and long-range context.
Approach 3: Transformer Fine-Tuning
Modern sentiment systems fine-tune a pretrained transformer language model (BERT, RoBERTa, DistilBERT, and similar). These models are first pretrained on huge text corpora, so they already encode rich, contextual word meaning. Crucially, their self-attention produces context-dependent representations: the word “sick” gets a different vector in “I feel sick” vs. “that trick was sick.”
The fine-tuning recipe
- Take a pretrained encoder (e.g. BERT) with its learned weights.
- Add a small classification head on top (typically over the pooled [CLS] token representation).
- Train end-to-end on your labeled sentiment data with cross-entropy loss, using a small learning rate so the pretrained weights adjust gently.
- The model learns to read negation, sarcasm cues, and context far better than BoW.
Because the model understands word order and context natively, “not good” needs no special rule — the network learns the interaction from data. Fine-tuned transformers are the current state of the art for sentiment, at the cost of more compute and less direct interpretability than logistic regression or a lexicon.
Approaches Compared
| Approach | Needs labels? | Handles context | Interpretability | Typical accuracy |
|---|---|---|---|---|
| Lexicon + rules | No | Weak (only via rules) | High (every word visible) | Lowest |
| BoW + LogReg / NB | Yes | Limited (n-grams help) | Medium (per-word weights) | Good baseline |
| Fine-tuned transformer | Yes (less can suffice) | Strong (self-attention) | Low (black box) | State of the art |
Why Sentiment Is Hard
Sentiment looks easy until you meet real text. These are the failure modes every practitioner runs into:
Negation
“not good”, “hardly impressive”, “no problems at all”. A negator can flip meaning, but its scope is tricky — “not only good but great” is positive. Simple flip-the-next-few-words rules (like our demo) are a useful approximation, not a complete solution.
Sarcasm & irony
“Oh great, another delay. I just love waiting.” Every word is positive, the meaning is bitterly negative. Detecting sarcasm needs world knowledge, tone, and often context the text alone does not contain — it defeats lexicons entirely and challenges even large models.
Domain dependence
“unpredictable” praises a thriller plot but condemns a car's brakes. “Small” is good for a phone, bad for a hotel room. A model or lexicon tuned on one domain often transfers poorly to another.
Context & world knowledge
“The ending made me cry” is positive for a drama, negative for a comedy. Sentiment can depend on facts outside the sentence, on who is speaking, and on what is being evaluated.
Comparative sentences
“Phone A's camera is better than Phone B's” is positive about A and negative about B — there is no single sentiment for the sentence. These require identifying the compared entities and the direction of comparison, a job for aspect-based methods.
Key Takeaways
- Sentiment analysis ranges from coarse polarity (positive/negative/neutral) to fine-grained scales, aspect-based sentiment, and discrete emotion detection.
- Lexicon-based scoring sums per-word polarities — but it must handle negation (flip the sign of following words) and intensifiers (scale magnitude) to be even roughly correct.
- “not good” comes out negative precisely because the negator flips the polarity of the word it governs — exactly what the interactive demo visualizes.
- Machine-learning classifiers (bag-of-words + logistic regression or Naive Bayes) learn domain-appropriate word weights from labeled data and beat hand-built lexicons.
- Fine-tuned transformers (BERT/RoBERTa) give context-dependent representations and are the current state of the art, trading interpretability for accuracy.
- The hard cases — negation scope, sarcasm, domain dependence, missing context, and comparative sentences — are where naive methods break and why sentiment remains an active research problem.