Sub topic 9
Stop Words: What to Remove and Why
This chapter assumes you know basic Python, strings, functions, list comprehensions, and the idea that text can be split into tokens. A stop word is a frequent word that a text-processing task may ignore, such as “the”, “is”, “of”, or “to”. Stop words are not automatically useless. Their value depends on the task.
Why stop word removal exists
Many documents contain common grammatical words. In a search or topic-counting system, these words can appear so often that they add little information. Removing them can:
- Reduce the number of tokens a system stores.
- Make keyword counts more useful.
- Reduce the size of an index.
- Focus a simple classifier on less frequent content words.
For example, these two sentences contain the same main topic:
- “The cat is on the mat.”
- “A cat rests on a mat.”
A simple keyword system may care more about cat, mat, and rests than about the, is, on, and a.
Stop word removal is a filtering step. It removes selected tokens from a token list. It does not understand the full meaning of a sentence.
Use the same normalisation for both lists
The text and the stop-word list must use the same representation. If the text is case-folded but the stop-word list contains capitalised words, comparisons may fail.
Case folding converts text to a form intended for caseless comparison. It is more suitable than plain lowercasing for many Unicode strings. Unicode normalisation maps equivalent or compatible character sequences to a consistent form.
The usual sequence is:
- Apply Unicode normalisation.
- Apply case folding.
- Clean repeated whitespace if needed.
- Tokenise the text.
- Remove tokens found in the normalised stop-word set.
import re
import unicodedata
RAW_STOP_WORDS = {"The", "a", "an", "is", "of", "and", "to", "in"}
def normalise(text):
text = unicodedata.normalize("NFKC", text)
text = text.casefold()
return re.sub(r"\s+", " ", text).strip()
STOP_WORDS = {normalise(word) for word in RAW_STOP_WORDS}
def tokenise(text):
return re.findall(r"\w+", text, flags=re.UNICODE)
def remove_stop_words(tokens):
return [token for token in tokens if token not in STOP_WORDS]
raw = "The Café is in the field"
normalised = normalise(raw)
tokens = tokenise(normalised)
print(tokens)
print(remove_stop_words(tokens))The output is:
['the', 'café', 'is', 'in', 'the', 'field']
['café', 'field']NFKC changes the ligature fi into fi. The stop-word set is normalised with the same function as the input, so both sides of the comparison use compatible values.
Tokenise before filtering
Filtering works on tokens, not on arbitrary pieces of a string. A token is a unit produced by tokenisation, often a word, number, punctuation mark, or subword.
If you remove the word in from raw text using a simple string replacement, you could damage other words:
in → removed
inside → sideTokenisation prevents this particular error because in and inside become separate tokens.
The tokeniser in the example uses \w+, which is useful for a small demonstration but is not a complete language-aware tokeniser. It does not preserve punctuation, and it may not handle contractions or languages with different word-boundary rules as you require. Choose a tokeniser that matches your language and downstream model.
Words that are dangerous to remove
A general-purpose stop-word list can remove information that matters. Negation words such as not, no, and never can reverse sentiment:
“This product is useful.”
“This product is not useful.”Removing not makes both sentences look similar. For sentiment analysis, keep negation words and consider preserving nearby word combinations.
Named entity recognition also needs caution. A named entity is a real-world name, such as a person, organisation, or place. Removing short words can damage names such as “The Who” or “Of Mice and Men”. Search systems may also need every word in a title or exact phrase.
Language, domain, and task should determine the list. A medical system may need to preserve common words that carry clinical meaning. A legal search system may need exact wording rather than only content words.
Choosing whether to remove stop words
| Task | Typical choice | Main reason |
|---|---|---|
| Simple topic counts | Often remove | Frequent grammatical words can dominate counts |
| Sentiment analysis | Usually keep negations | Words such as “not” change meaning |
| Named entity recognition | Usually keep | Short words can be part of names and context |
| Exact phrase search | Keep | Every word may be required for a match |
| Large keyword index | Consider removing | It can reduce index size and noise |
Modern language models often use contextual representations, so manually removing stop words may reduce useful information. Test both versions with the evaluation metric for your task.
Exercise
Why should the stop-word set be passed through the same normalisation function as the input text, and why should filtering happen after tokenisation?
Show answer
Matching normalisation makes equivalent spellings compare equal, while tokenisation prevents a stop word from being removed as part of a larger word.