Sentiment analysis
“Not bad for the price.” Happy or unhappy? Your brain knew instantly. The computer got it backwards.
- Score reviews with a word list in Python
- Find exactly where the scorer fails
- Measure it with Unit 3's tools
Sentiment analysis
“Not bad for the price.” Happy or unhappy? Your brain knew instantly. The computer got it backwards.
- Score reviews with a word list in Python
- Find exactly where the scorer fails
- Measure it with Unit 3's tools
Story
Learn it
Sentiment analysis finds the feeling in a piece of text: positive, negative or neutral. It is used on reviews, complaints and what people say about a service. The simplest method is a word list: +1 for each happy word, −1 for each unhappy one. It is fast and easy to read, and it misses a great deal: "not bad", sarcasm, mixed feelings, and words it never learned. Because it sorts text into classes, you measure it like any classifier.
Words to know
finding whether text is positive, negative or neutral.
adds +1 for each positive word and −1 for each negative word: above 0 positive, below 0 negative, 0 unsure.
a word like not that flips the meaning: "not clean".
saying the opposite of what you mean: "Great. Another cold night."
a word not on either list, like zabardast. It scores 0.
happy and unhappy in one review: "friendly staff, dirty room".
code: Python with libraries like NLTK and spaCy; no code: Orange's text add-on.
Step by step: scoring four reviews
Positive words: clean, friendly, delicious, beautiful. Negative: dirty, rude, cold, bad.
| Review | Score | Scorer says | Truth |
|---|---|---|---|
| "Clean room, friendly host." | +2 | positive | positive ✓ |
| "Cold water and a rude driver." | −2 | negative | negative ✓ |
| "Not bad at all!" | −1 | negative | positive ✗ (negation) |
| "Wah, zabardast view!" | 0 | unsure | positive ✗ (unknown words) |
- Score each review by adding +1s and −1s.
- Compare with the truth: 2 right out of 4 = 50% accuracy.
- Name each mistake: negation, and unknown words.
- Improve one thing: flip the next word's score after not, and "Not bad" becomes +1 → positive.
- Measure it as a classifier: build a confusion matrix, with negative as the positive class if catching unhappy guests matters most, and check its recall.
Careful: the computer does not understand feelings. It counts words from a list someone wrote. And adding more words to the list will not fix negation or sarcasm: "not clean" and "Great. Another cold night." need context, which a word list does not have.
Your missions
programs/sentiment.py · datasets/reviews.csv · LibreOffice CalcScore by hand
5 minin pairs- 1
Open
reviews.csvin Calc: 16 houseboat reviews, each with its true label. - 2
Predict: read the reviews without the labels. Which three will a word-counting computer get wrong? Write their numbers.
Only you can see this
Run the scorer
10 min- 3
Copy
sentiment.pynext toreviews.csv. Read the two word lists and the scoring loop. - 4
Run it. It prints every review it gets wrong, and its score out of 16. How many of your three predictions were among them?
- 5
Sort its mistakes into negation, sarcasm, unknown word and other.
Improve it — honestly
8 min- 6
Make one improvement: add zabardast and wah to the positive words, or handle not by flipping the score of the next word.
- 7
Run it again. What's the new score out of 16?
- 8
Build a confusion matrix for the new version, with negative as the positive class. What is its recall for unhappy guests?
Write three new reviews your improved scorer will still get wrong. Swap them with another pair and see if their scorer gets them right.
No laptop today?
Your teacher reads the 16 reviews. The class scores each one aloud with the two word lists, adding +1 and −1 on the board. Then compare with the true labels and sort the mistakes into the four kinds.
Debate it
The booking website shows other tourists the uncle's "72% positive" score.
- If the scorer misreads "Not bad for the price" and "Zabardast!", who pays for its mistakes?
- Should a business be judged by a number a machine produced from its reviews? What should the website tell people about how the number was made?
Check yourself · practice, not a test
In the app, students answer these one at a time and each answer is checked straight away. It's practice, never a mark — anything they miss comes back later for another go.
1."The room was not clean." A word-list scorer counts clean as +1. This failure is called:
- ASarcasm
- BNegation
- CAn unknown word
- DTokenisation
2.A scorer gets 11 of 16 reviews right. Its accuracy is about:
- A11%
- B16%
- C69%
- D91%
3.A sentiment scorer is a kind of classifier, and can be evaluated with a confusion matrix.
TrueFalse4.Which would trick a simple word-list scorer? (choose all that apply)
- A"Nothing was wonderful about it."
- B"Great. Another cold night."
- C"Wah! Zabardast!"
- D"The room was dirty."
5.Finding whether a piece of text is positive or negative is called ______ analysis.
Words have feelings in context. A word list only sees the words.
Typing practice
Skill: two word lists (5 min)
-
On typing.com: the symbols lessons.
-
Type three times, eyes on the screen:
positive_words = ["clean", "friendly", "delicious", "peaceful"] -
Goal this term: 38–44 WPM at 95%.
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.