Classify text with simple features
Build a tiny sentiment baseline, try to fool it, and learn to evaluate it fairly.
- Use a transparent word-lookup baseline to label text
- Find the kinds of sentences that fool it
- Handle negation with a simple rule
Text classification assigns a label to a piece of text: spam or not, the topic of a news story, whether a review is positive. The simplest classifier is a lexicon baseline: count words from a positive list, subtract words from a negative list, and look at the sign.
1positive = {"bright", "helpful", "love"}
2negative = {"broken", "rude", "hate"}
3words = "helpful and bright".split()
4score = sum(word in positive for word in words) - sum(word in negative for word in words)
5print(score)2
A baseline like this is easy to read and explain - you can see exactly why it chose a label. That makes it a great first model and a yardstick: a fancier model has to beat it to be worth its cost.
Try it
Up to 15 XPCan you fool the baseline?
Imagine the baseline knows the positive words good, great, love and the negative words bad, awful, hate. For each review, decide whether its label would be right or whether the baseline is fooled.
“I love this phone, the camera is great.”
“This is not good at all.”
“Oh great, it broke after one day.”
“Awful service and bad food.”
“The battery died in an hour.”
“I hate how much I love this game.”
Handling “not”
A classic patch is to flip the polarity of a sentiment word that comes right after a negation like not or never. It fixes “not good” - but not “not exactly what I would call good”. Rules like this pile up quickly, which is a big reason the field moved to classifiers that learn from labeled examples, the topic of the next lesson.
Key takeaways
A lexicon baseline is transparent and quick - and a useful yardstick.
It misses negation, sarcasm, unlisted words and mixed feelings.
Always evaluate on examples the classifier did not learn from.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: apply NLP with Python
Try each text-processing idea in Python, run it against sample inputs, and use the results to see where the method works or falls short.
Score a tiny sentiment lexicon
Read a sentence and count matches from positive words good, great, love and negative words bad, awful, hate. Print positive, negative, or neutral from the score. This is a toy baseline, not a reliable sentiment model.
- Positive words
- Negative words
- No known words
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Flip sentiment after “not”
Extend the baseline: a sentiment word that comes right after not or never counts the opposite way. Read a sentence (use alphabetic words, lowercased) and print positive, negative or neutral.
- Negated positive
- Negated negative
- No negation
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…