Sub topic 8
Tokenising Before Filtering
This chapter assumes you know basic Python strings, loops, list comprehensions, and functions. You should also know that a tokenizer splits text into smaller units called tokens, often words or punctuation marks.
Why filtering needs tokens
Stop-word removal works on individual tokens. A stop word is a common word that a task may choose to ignore, such as the, is, or of. A stop-word list cannot reliably remove words from raw text with simple string replacement because word boundaries and punctuation matter.
For example, removing in from this sentence as a substring could damage another word:
The cat is in the garden.The desired result might be:
cat gardenHowever, substring replacement could change inside into side. Tokenisation gives the filter complete words to inspect.
A typical sequence is:
raw text → Unicode and case normalisation → tokenisation → stop-word filteringThe exact tokenizer depends on the language and task. A whitespace split is simple, but it leaves punctuation attached to words:
text = "The cat, is sleeping."
print(text.split())
# ['The', 'cat,', 'is', 'sleeping.']The tokens cat, and sleeping. do not match cat and sleeping in a vocabulary or stop-word list.
A simple word tokenizer
For a basic English example, a regular expression can select runs of word characters. Python's re.findall returns every matching token:
import re
import unicodedata
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()
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)
filtered = remove_stop_words(tokens)
print(normalised)
# the café is in the field.
print(tokens)
# ['the', 'café', 'is', 'in', 'the', 'field']
print(filtered)
# ['café', 'field']The stop-word set is already lowercase and case-folded. This matters because filtering uses exact membership tests. If the text contains The but the set contains only the, the word will not be removed unless normalisation happens first.
A set is useful for stop words because membership tests are usually faster than searching a list. More importantly, a set clearly expresses that the filter asks whether each token belongs to a collection of excluded words.
Token boundaries are language-dependent
The regular expression above is only a small demonstration. Real tokenisation has difficult cases:
can'tmay be one token, or it may becomecaandn't.mother-in-lawmay be one token or three.[email protected]may need to remain one token.$25.50contains a number and currency information.- Emojis can carry important sentiment.
- Some languages do not separate words with spaces.
Punctuation also carries meaning. In sentiment analysis, not good should not become equivalent to good after removing not. In search, removing punctuation may make matching easier, but punctuation in a product code such as AB-123 may be essential.
A tokenizer should therefore match the downstream task. General-purpose NLP libraries often provide language-aware tokenizers that handle contractions, punctuation, and special tokens more carefully than a regular expression.
Ordering and common errors
Filtering raw text before tokenisation causes several problems:
- It can remove a stop word inside a larger word.
- It can miss a stop word next to punctuation.
- It can behave differently for uppercase and lowercase text.
- It can leave extra spaces or empty pieces.
- It makes it harder to inspect exactly what was removed.
The safer pipeline is:
Do not remove every frequent word automatically. Frequency does not prove that a word is useless. Stop words can express negation, tense, relationships, and subject matter. For named entity recognition, words such as of may help identify names such as Bank of America. For sentiment analysis, not, never, and intensifiers may be important. Search systems may remove carefully chosen stop words, while a text-generation or linguistic-analysis system may remove none.
Exercise
Exercise
Why should "The cat, sleeps." be tokenised before applying a stop-word set containing "the"?
Show answer
Tokenisation separates the punctuation and normalises the word form, producing tokens such as "the", "cat", and "sleeps". The filter can then remove the exact token "the" without depending on punctuation or accidentally changing a substring inside another word.