Loading
0x20Lesson 3 of 15

Clean text: stop words, stems and lemmas

Decide which words to drop and how to reduce words to a common form.

18 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • Remove stop words and explain when that helps or hurts
  • Tell stemming from lemmatization
  • Write a tiny rule-based stemmer

Count the words in almost any English text and the winners are always the same: the, and, of, to, a. A handful of tiny words make up a large share of all text - an effect known as Zipf’s law. They hold sentences together but say little about the topic.

frequent_words.py
from collections import Counter
text = "the cat and the dog and the bird"
print(Counter(text.split()).most_common(2))
Output
[('the', 3), ('and', 2)]

Stop words

Stop words are very common words that a pipeline throws away so the meaningful words stand out. It is a classic trick for keyword search and word counts. But “very common” is not the same as “meaningless”, as you are about to see.

Try it

Clean a review

This review is negative. Turn on Remove stop words and read what is left. Which word that flips the meaning disappeared?

Then turn on Stem and look at how studies, running and learned change. Are the results always real words?

21 tokens19 distinct (the vocabulary)
  1. I
  2. am
  3. not
  4. happy
  5. with
  6. the
  7. results
  8. ,
  9. but
  10. the
  11. studies
  12. were
  13. running
  14. smoothly
  15. and
  16. the
  17. researchers
  18. learned
  19. a
  20. lot
  21. .

Stemming vs lemmatization

Both reduce related forms - study, studies, studying - to one form, so a count or a search treats them as the same word.

StemmingLemmatization
HowChops off endings by ruleLooks words up, using grammar
studiesstudistudy
betterbettergood
micemicemouse
SpeedVery fastSlower, needs a dictionary

A stemmer (like the classic Porter stemmer) is fast and simple, but its output (studi) need not be a real word. A lemmatizer returns the dictionary form, the lemma, and can handle irregular words like mice and better - but it needs a vocabulary and often the word’s part of speech.

Try it

Stem or lemma?

Each line shows a word being reduced. Was it a rule-based stemmer or a dictionary-based lemmatizer? Hint: real words and irregular forms point to one of them.

0 of 6 sortedScore 0/0
  • “studies → studi”

  • “mice → mouse”

  • “universal → univers”

  • “better → good”

  • “was → be”

  • “connection → connect”

Key takeaways

  • Stop words are very common words; removing them can sharpen counts and search - or delete meaning like “not”.

  • Stemming chops endings by rule (fast, crude); lemmatization finds the dictionary form (slower, smarter).

  • Every cleaning step is a trade-off: decide based on the task, then check what it does to real examples.

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.

Exercise 1

Remove stop words

+25 XP

Read a line, lowercase it, split it on whitespace, and drop every token in stop_words. Print the remaining tokens separated by one space.

  • Cat on a mat
  • Keeps other words
main.py
Loading editor…

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.

Exercise 2

Write a tiny stemmer

+25 XP

Read a line of lowercase words and stem each one with these rules, checked in order (apply only the first that matches):

  1. ends with ing and is longer than 5 letters: remove ing
  2. ends with ed and is longer than 4 letters: remove ed
  3. ends with s but not ss: remove the s

Print the stems separated by one space.

  • Every rule
  • Short words are left alone
main.py
Loading editor…

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…

Did you like the lesson? 😆👍
Consider a donation to support our work: