Text Preprocessing
From raw strings to a clean token stream
Raw text is messy. Tokenization, normalization, stopword removal, stem/lemmatization and vocabulary handling turn strings into the clean token sequences every NLP model consumes.
What you will learn
- Tokenize and normalize text with the right rules per language/domain
- Choose between stemming and lemmatization
- Handle vocabularies, OOV tokens and unicode safely
What is this?
Text preprocessing converts raw strings into the token sequences a model can learn from. The pipeline is order-sensitive: normalize case/unicode, tokenize (word, subword or character level), strip or retain stopwords by task, and reduce inflection via stemming or lemmatization. Every choice is a bias — aggressive cleaning destroys information (negation, sentiment), light cleaning leaves noise. The preprocessing should match the downstream model's needs, not an arbitrary checklist.
Why do you need it?
- Tweets, tickets and reviews are full of typo, casing, URL, emoji and spacing noise that kills naive feature work.
- Vocabulary size explodes (memory + sparsity) if you don't normalize and prune.
- Stem/lemmatize unifies surface forms so 'running' and 'run' count as one signal.
- Done wrong, cleaning silently removes the signal you wanted to predict (e.g. 'not good').
Mental model
Washing, peeling and chopping vegetables before cooking
Text preprocessing
You peel (strip formatting), rinse (unicode/entities), chop to the right size (tokens), and often keep the skin when the recipe needs it — preprocessing is task-appropriate preparation, not a universal deep-clean that strips flavor.
How it works
- Normalize: lower-case, NFC unicode fold, collapse whitespace, decode entities.
- Tokenize: whitespace+punctuation splits (simple) or library tokenizers; for large vocab use subword (BPE) tokenization.
- Filter: drop URLs/emails/mention tokens or keep placeholders — decide per task.
- Infl. reduction: Porter stemmer (crude, fast) vs WordNet/lemmatizer (context-aware, slower).
- Vocabulary: keep the top-k tokens, add <UNK> and <PAD>; encode indices and store as sparse/dense matrices.
The text-to-tokens pipeline.
Examples
Preprocess a tweet with spaCy-style rules
python3 -c "import redef clean(text: str) -> str: text = re.sub(r'http\S+', 'URL', text) text = text.lower() text = re.sub(r'[^a-z\s]', ' ', text) return re.sub(r'\s+', ' ', text).strip()print(clean('Great deal!!! http://bit.ly/xyz (love it)'))"URLs become the placeholder, case is unified, punctuation collapses — tokens are 'great deal url love it'.
In production
- Ticket-routing pipelines normalize and tokenize, but keep negations by NOT stripping all stopwords on the sentiment path.
- Search indexers lemmatize queries and docs so 'purchased' matches 'purchase'.
- LLM-era pipelines still preprocess: dedupe, de-emphasize boilerplate, normalize UTF-8 before embeddings.
Best practices
- Write one idempotent preprocessing function per pipeline; reuse it at train and serve exactly.
- Keep the language/domain explicit (hindi? dialect? code vs prose?).
- Retain negation-aware tokens for opinion tasks (replace 'not' + adj with NOT_adj).
- Log vocab stats — tokens, OOV rate, mean token length — with every experiment.
Common mistakes
- Lowercasing everything and destroying case-sensitive entities (USB, GDPR, McDonald's).
- Stripping all stopwords on tasks where 'not', 'but' and 'because' carry the meaning.
- Different preprocessing online vs offline (serve got the cleaned text; training didn't).
- Letting <UNK> dominate: >2% OOV on a fixed vocab means re-tok, grow vocab or switch to subword.
Common issues
Model treats 'not good' and 'good' identicallyStopword removal deleted 'not'
Keep negations — either skip stopword removal or turn 'not X' into a negation-feature token.
Huge OOV rate at inferenceLive vocabulary different from training
Rebuild vocab over combined corpora (or use subword tokenization) and add <UNK> handling.
Debugging tips
- Print 50 tokenized samples of each class before firing up a model — eyeball beats assumption.
- If accuracy stalls at a ceiling, check preprocessing isn't removing the predictive token (negation, price symbols).
- Compare vocab coverage on train vs serve corpora; drift inflates OOV.
- Hash-sensitive: reproduce the same token stream from the same raw string in every environment.
Interview checkpoint
Hands-on lab
- Implement a reproducible normalization + tokenization pipeline
- Measure vocab size, OOV rate and sentence-length distribution
- Compare stopword-on vs stopword-off on a sentiment-flavored sample
Build a Tokenizer With Metrics You Trust
30 min · beginner
Quick quiz
For sentiment analysis, removing all stopwords is risky because:
Aggressive text cleaning always improves model performance.