2. Text Preprocessing and Tokenization
Learn how raw text becomes model-ready units.
01 Learning objectives
By the end of this module, you should be able to explain the concept in plain language, connect it to a use case, and recognize when it is — and is not — appropriate.
-
Objective 1
Understand normalization, tokenization, stop words, stemming and lemmatization.
-
Objective 2
Know when preprocessing helps and when it destroys information.
-
Objective 3
Understand subword tokenization used by Transformers.
02 Pipeline
Traditional NLP often uses:
raw text → clean/normalize → tokenize → optional stemming/lemmatization → vectorize
Modern Transformer pipelines often use much lighter manual cleaning because punctuation, casing, and word pieces may contain useful signal.
03 Tokenization
A token is a unit presented to a model. Depending on the tokenizer, unbelievable may be one word token or several subword pieces.
Subword tokenization helps with:
- out-of-vocabulary words,
- rare words,
- morphology,
- manageable vocabulary size.
04 Code: simple preprocessing
import re
def normalize_text(text):
text = text.lower()
text = re.sub(r"\s+", " ", text)
return text.strip()
text = "My ACCOUNT shows an unknown debit."
print(normalize_text(text))
05 Pitfalls and nuances
These are the mistakes most likely to create a wrong mental model or a weak implementation.
-
Pitfall 1
Removing negation words such as `not` as stop words.
-
Pitfall 2
Lowercasing when case conveys meaning (IDs, acronyms, entities).
-
Pitfall 3
Applying aggressive stemming before a pretrained Transformer.
06 Checkpoint questions
Try answering these without looking back at the module. If the explanation is clear in your own words, you are ready to continue.
-
Check 1
Why can removing `not` be harmful?
-
Check 2
Why do Transformer tokenizers use subwords?