Introduction
Companies read thousands of reviews, tweets and support messages every day, and want to know how people feel. Sentiment analysis labels a text as positive, negative or neutral, or gives it a score. It is one of the most widely used NLP applications, and a good place to compare the two great families of methods: rules written by people, and classifiers learned from labelled data.
How it works
The rule-based approach starts from a sentiment lexicon: a table of words with scores, such as love +3, good +2, bad -2 and awful -3. The simplest score of a text is the sum of its words' scores. Two rules make it much better:
- Negation: "not good" is negative. A common rule flips the sign of a sentiment word when a negation word ("not", "never", "no") appears within the few words before it, its negation scope.
- Intensifiers (boosters): "very good" is more positive than "good", and "slightly boring" less negative than "boring". Multiply the next word's score by a factor such as 1.5 or 0.5.
A score above 0 is positive, below 0 negative, and exactly 0 neutral.
The learned approach treats sentiment as text classification: train Naive Bayes, logistic regression or a neural network on reviews labelled positive and negative. Classifiers learn domain words automatically ("unpredictable" is good for a thriller, bad for a car), but need labelled data. Modern systems fine-tune transformer models, which see the whole sentence and handle most negations.
To evaluate either approach, compare predictions with true labels. Accuracy is the fraction correct. For one class, say negative:
- Precision = true negatives found ÷ everything predicted negative (how trustworthy a "negative" label is).
- Recall = true negatives found ÷ all truly negative texts (how many negatives were caught).
- F1 = 2 × precision × recall ÷ (precision + recall), a single number that is high only when both are high.
The macro F1 averages the F1 of every class, so a small class counts as much as a big one.
Example
LEXICON = {"good": 2, "great": 3, "love": 3, "fine": 1, "bad": -2, "awful": -3, "boring": -2, "hate": -3}
NEGATIONS = ["not", "never", "no"]
BOOSTERS = {"very": 1.5, "really": 1.5, "slightly": 0.5}
def sentiment(text):
words = [w.strip(".,!?") for w in text.lower().split()]
score = 0.0
for i, w in enumerate(words):
if w not in LEXICON:
continue
value = LEXICON[w]
if i > 0 and words[i - 1] in BOOSTERS:
value *= BOOSTERS[words[i - 1]]
if any(n in words[max(0, i - 3):i] for n in NEGATIONS):
value = -value
score += value
return score
reviews = [
("I love this phone, the screen is great", "pos"),
("Not good. The battery is bad", "neg"),
("The film was not boring at all", "pos"),
("Really awful, I hate it", "neg"),
("It is fine but slightly boring", "neg"),
("I do not love it", "neg"),
]
tp = fp = fn = correct = 0
for text, label in reviews:
s = sentiment(text)
pred = "pos" if s > 0 else "neg" if s < 0 else "neutral"
correct += pred == label
tp += pred == "neg" and label == "neg"
fp += pred == "neg" and label != "neg"
fn += pred != "neg" and label == "neg"
print(f"{s:+.1f} {pred:7} ({label}) {text}")
precision, recall = tp / (tp + fp), tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print(f"accuracy {correct}/{len(reviews)}; neg: precision {precision:.2f}, recall {recall:.2f}, F1 {f1:.2f}")Line by line
sentimentlowercases the text, strips punctuation from each word and adds up lexicon scores.- A booster right before a sentiment word multiplies its score: "Really awful" scores -3 × 1.5 = -4.5.
- A negation in the three words before a sentiment word flips it: "not boring" becomes +2, and "do not love" becomes -3.
- "fine but slightly boring" sums to exactly 0, so the rules call it neutral; its true label is negative. Rules cannot weigh "but" the way people do.
- For the negative class there are no false alarms (precision 1.00) but one miss (recall 0.75), giving F1 0.86.
Expected result
+6.0 pos (pos) I love this phone, the screen is great
-4.0 neg (neg) Not good. The battery is bad
+2.0 pos (pos) The film was not boring at all
-7.5 neg (neg) Really awful, I hate it
+0.0 neutral (neg) It is fine but slightly boring
-3.0 neg (neg) I do not love it
accuracy 5/6; neg: precision 1.00, recall 0.75, F1 0.86
In the real world
VADER is a widely used lexicon-and-rules analyser tuned for social media; it handles negation, boosters, capital letters and exclamation marks. Through NLTK (shown, not run: NLTK is not available in StepVioz's browser Python):
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
nltk.download("vader_lexicon")
sia = SentimentIntensityAnalyzer()
print(sia.polarity_scores("The film was not boring at all!"))
It returns negative, neutral and positive proportions and a compound score from -1 to 1. Most products today use fine-tuned transformers instead, for example Hugging Face's pipeline("sentiment-analysis"), which downloads a model trained on movie reviews. Brands track sentiment over time, and support teams route angry messages first.
Common mistakes
- Ignoring negation, so "not good" counts as positive.
- Using a general lexicon in a specialized domain: in finance, "liability" is neutral, not negative.
- Reporting only accuracy on unbalanced data, where always guessing the big class looks good.
- Missing sarcasm: "Great, it broke again" fools both rules and many models.
Best practices
- Start with a lexicon baseline, then check whether a trained classifier beats it on your own data.
- Keep a labelled test set from your real domain and report precision, recall and F1 for each class.
- Read the mistakes: they show which rules or training examples are missing.
- Treat neutral as a real class with its own examples, not as leftover.
Summary
Lexicon-based sentiment analysis adds up word scores, with rules for negation and intensifiers; it needs no training but misses context. Classifiers learn sentiment from labelled examples and handle domain words better. Either way, evaluate with precision, recall and F1 per class, not with accuracy alone.
Quick check
Key takeaways
- Lexicon methods need no training data and are easy to explain, but miss context, sarcasm and domain words.
- Negation and intensifiers change sentiment and need explicit rules or a model that sees word order.
- Accuracy can hide poor performance on one class; report precision, recall and F1 per class.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.