Classify text with simple features
Build a tiny sentiment baseline and learn to evaluate it fairly.
- Use a transparent word-lookup baseline and inspect its limits.
Text classification assigns a label to text, such as a topic or sentiment. A simple lexicon baseline adds positive weights for words in a positive list and negative weights for words in a negative list. It is easy to inspect, but misses context, negation, sarcasm, and words it has not seen.
1positive = {"bright", "helpful", "love"}
2negative = {"broken", "rude", "hate"}
3words = "helpful and bright".split()
4score = sum(w in positive for w in words) - sum(w in negative for w in words)
5print(score)2
Evaluate a classifier on examples it did not train on. Keeping training and test data separate helps estimate performance on new text. Accuracy is the fraction of correct predictions, but can mislead when classes are imbalanced; inspect errors and per-class results too.
Key takeaways
Use a transparent word-lookup baseline and inspect its limits.
Simple baselines help make ideas concrete.
Interpret language tools in context and check important results.
Lesson quiz
4 questions · pass with 3 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.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…