Before a model sees text, the text is cleaned and split into units. Typical steps are Unicode normalization, lower-casing, removing punctuation and stop words, and reducing words to stems or lemmas. Each step merges word forms that mean the same thing, which shrinks the vocabulary and helps when data is scarce, but it can also throw away information the task needs. This page covers Unicode issues, tokenization, stop words, stemming versus lemmatization, Zipf's law, and when to preprocess less (modern subword models do very little). In the exercises you write a normalizer, a tokenizer and a crude stemmer, then train naive Bayes on noisy two-topic documents. With 10 training documents, the full pipeline raised accuracy from 0.884 to 1.000 and cut the vocabulary from 369 word forms to the 16 topic stems. With 200 documents every pipeline scored about 1.00. On a task where only capitalization separates the classes ('US' against 'us'), lower-casing dropped accuracy from 1.00 to 0.515.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Cleaning text before counting it
Computers see “Launch”, “launch,” and “launched” as three different words. Preprocessing makes such variants the same: it lower-cases, strips punctuation, drops very common words like “the”, and cuts words down to their stems. This shrinks the vocabulary, so a model needs less data to learn. The demo runs a messy sentence through each stage.
Language lab
Stage 4 (crudely stemmed): rocket launch launch successful. Each step shrinks the vocabulary but can lose information (here "launches" and "launched" both become "launch").
Why engineers care
Maintenance logs, pilot reports and anomaly tickets are full of abbreviations, typos and inconsistent capitalization. Careful normalization is often the difference between a text classifier that works on 200 labelled reports and one that does not.
📝 Quick check (Level 0)
0/3 answered
1. Lower-casing makes "Rocket" and "rocket"…
2. Stop words are…
3. Stemming turns "launched" into…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
Unicode normalization: the same visible text can have different byte sequences (é as one code point or e + accent; the fi ligature). NFC/NFKC normal forms make them equal; NFKD plus dropping combining marks removes accents.
Tokenization: split text into tokens: words, numbers, punctuation. Rules matter: “don’t”, “U.S.”, “3.5”, e-mail addresses.
Lower-casing and punctuation removal: merge variants, at the cost of losing “US” vs “us” or “?” as a question signal.
Stop words: the most frequent words carry little topic information; removing them shortens documents. Function words matter for some tasks (sentiment “not”, authorship).
Stemming vs lemmatization: stemming chops suffixes by rules (Porter: “studies” → “studi”); lemmatization maps to dictionary forms using part of speech (“better” → “good”).
Zipf’s law: word frequency ≈ C / rank, so a few words dominate counts and most words are rare. Type–token ratio = distinct words / total words.
📝 Quick check (Level 1)
0/3 answered
1. Type–token ratio of 'the cat the dog':
2. Under Zipf's law the 10th word is about ___ as frequent as the 1st:
3. Lemmatization of 'better' (adjective) gives…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Preprocessing in code
Exercise 1.
Normalize Unicode, case and whitespace.
Tokenize words and numbers.
Strip common suffixes with a crude stemmer.
Practice.
Python’s unicodedata and re handle normalization and simple tokenization; spaCy and NLTK give tokenizers, stop lists, lemmatizers and Porter/Snowball stemmers.
Fit preprocessing on training data only and apply the identical pipeline at inference.
For transformer models, use the model’s own tokenizer and do almost no cleaning: they were trained on raw text, and casing and punctuation carry meaning.
Keep the raw text: you may need it for error analysis or a different model later.
import spacynlp = spacy.load("en_core_web_sm")doc = nlp("The rockets were launched successfully!")print([t.lemma_.lower() for t in doc if not (t.is_stop or t.is_punct)]) # ['rocket', 'launch', 'successfully']
🐍 Exercise 1: normalize, tokenize, stem
Implement Unicode-aware normalization, a regex tokenizer and a crude suffix stemmer.
📝 Quick check (Level 2)
0/3 answered
1. For a BERT or GPT model you should…
2. Preprocessing must be identical…
3. A library with lemmatizers:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
Why normalization helps with little data. A bag-of-words model estimates one parameter per word per class. Merging variants pools their counts, so each parameter is estimated from more examples: lower variance. With enough data, the model learns that the variants behave alike anyway, and the benefit disappears.
The bias it adds. Every merge assumes the merged forms are equivalent for the task. When they are not (case for named entities, “not” for sentiment, punctuation for questions), normalization deletes signal no amount of data can recover.
Zipf and sparsity. Because frequency falls like 1/rank, most words in any vocabulary are rare, and many test words were never seen in training. Normalization and, later, subword tokenization reduce this out-of-vocabulary problem.
📝 Quick check (Level 3)
0/3 answered
1. Normalization lowers ___ of word-count estimates:
2. Its cost is added…
3. Most words in a vocabulary are…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
When does text normalization help, and when does it hurt?
History.
M. F. Porter, “An algorithm for suffix stripping” (Program, 1980).
G. K. Zipf, Human Behavior and the Principle of Least Effort (1949).
Exercise 2.
Data: synthetic two-topic documents (spaceflight vs weather) of 12 words: 40% stop words, the rest one of 8 topic stems with a random suffix (–, s, ed, ing), with random capitalization (20%) and punctuation (15%).
Classifier: multinomial naive Bayes with add-one smoothing, trained on 10 or 200 documents, tested on 2000.
Second task: classes differ only by the word “US” (the country) versus “us” (the pronoun), among shared words.
pipeline
vocabulary (200 docs)
accuracy, 10 docs
accuracy, 200 docs
raw
369
0.884
1.000
+ lower-case
253
0.955
1.000
+ punctuation
76
0.988
1.000
+ stop words
64
0.999
1.000
+ stemming
16
1.000
1.000
case-dependent task
accuracy
raw
1.000
lower-cased
0.515
With little data every step helps: the full pipeline turns 369 surface forms into the 16 stems the topics are built from.
With enough data it hardly matters: at 200 documents all pipelines score 1.00.
Normalization can delete the signal: lower-casing merges “US” and “us” and the case task falls to chance.
Seeds: seeds 1 and 2 gave 10-document accuracy 0.806 → 0.999 and 0.860 → 1.000 from raw to full pipeline; the case task fell to 0.489 and 0.498.
🐍 Exercise 2 (research): when does text normalization help, and when does it hurt?
Train naive Bayes on noisy documents under five preprocessing pipelines with 10 and 200 training documents, and test lower-casing on a case-dependent task.
Preprocessing in AI, robotics and aerospace
Maintenance and safety reports: normalizing part numbers, abbreviations and units before classification or search.
Voice commands: normalizing transcripts before intent matching.
Design lesson: normalize as much as the task allows, and check which distinctions it erases.
References
M. F. Porter (1980). “An algorithm for suffix stripping.” Program 14(3).
C. D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval, ch. 2. Cambridge University Press.
D. Jurafsky and J. H. Martin (2024). Speech and Language Processing, 3rd ed. draft, ch. 2.
10 documents: 0.884 raw → 1.000 full pipeline; lower-casing a case-dependent task: 1.00 → 0.515.
Practice
Show:
Level 0
Problem 1Level 0
Type–token ratio of ‘the cat the dog’?
0.75.
Problem 2Level 0
Year Porter’s stemmer was published?
1980.
Problem 3Level 0
Distinct topic stems in exercise 2 (2 topics × 8)?
16.
Level 1
Problem 4Level 1
Frequency of the 10th-ranked word under Zipf’s law if the top word occurs 1000 times?
100.
Problem 5Level 1
Fractional vocabulary reduction from 369 to 16 (3 decimals)?
0.957.
Problem 6Level 1
Add-one probability of an unseen word: class count 50, vocabulary 16?
0.01515.
Problem 7Level 1
Tokens left after removing ‘the’ and ‘is’ from ‘the rocket is ready’?
2.
Level 2
Problem 8Level 2
Raw-pipeline accuracy with 10 documents?
0.884.
Problem 9Level 2
Full-pipeline accuracy with 10 documents?
1.
Problem 10Level 2
Vocabulary size after lower-casing (200 documents)?
253.
Problem 11Level 2
Accuracy with stop-word removal, 10 documents?
0.999.
Problem 12Level 2
Case task accuracy after lower-casing?
0.515.
Problem 13Level 2
Probability of a stop word at each position in exercise 2?
0.4.
Level 3
Problem 14Level 3
Explain why normalization helped with 10 documents but not with 200.
With 10 documents most of the 369 surface forms appear once or never in training, so naive Bayes has no evidence for many test words. Merging them into 16 stems gives every stem many counts. With 200 documents every surface form has been seen often enough with its class, so the merged and unmerged models make the same decisions. ∎
Problem 15Level 3
Explain why lower-casing destroyed the case task.
The only class signal is the token ‘US’ versus ‘us’. Lower-casing maps both to ‘us’, so every document of both classes has the same distribution of tokens and no classifier can beat chance. ∎
Problem 16Level 3
Compare stemming and lemmatization.
Stemming strips suffixes by fixed rules: fast, language-specific, no dictionary, but produces non-words (‘studi’) and errors (over- and under-stemming). Lemmatization uses a dictionary and part of speech to give real base forms (‘better’ → ‘good’): more accurate but slower and needs linguistic resources. ∎
Problem 17Level 3
Advise on preprocessing aircraft maintenance logs for fault classification.
Normalize Unicode, case and whitespace; expand a curated list of abbreviations and unify part-number and unit formats; keep negations (‘no leak’) and numbers. Check on validation data which steps help, and keep raw text for transformer models, which need little cleaning. ∎
Level 4
Problem 18Level 4
Raw-pipeline accuracy with 10 documents, seed 1?
0.806.
Problem 19Level 4
Case task accuracy on raw text?
1.
Problem 20Level 4
Training documents in the small setting?
10.
Coding practice
🐍 Code 1 (Level 0): type–token ratio
Distinct / total words.
🐍 Code 2 (Level 0): stop words
Remove listed words.
🐍 Code 3 (Level 0): punctuation
Strip non-word characters.
🐍 Code 4 (Level 0): vocabulary reduction
1 − after/before.
🐍 Code 5 (Level 0): Zipf's law
top / rank.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Text Preprocessing and Normalization: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.