Sub topic 3
Normalisation and Pre-tokenisation Before Subword Splitting
This chapter assumes basic Python, dictionaries, loops, frequency counts, and the idea that a token is an input unit for a language model. The goal is to understand the text transformations that happen before BPE, WordPiece, or unigram splitting.
Why preprocessing matters
A subword algorithm does not see “language” directly. It sees the exact sequence of characters supplied during training. Small differences in that sequence create different vocabulary entries.
For example, these strings may look similar to a reader:
cafécaféwithefollowed by a combining accentCAFÉcafe
They are not necessarily the same sequence of Unicode characters. If the tokenizer keeps them separate, the model may learn redundant pieces.
Normalisation transforms equivalent or intentionally related text into a consistent form. Common operations include:
- Unicode normalisation, such as NFC or NFKC
- Converting characters to lowercase
- Case folding, which handles more Unicode case relationships than simple lowercasing
- Standardising whitespace
- Replacing or removing selected control characters
- Applying application-specific rules for punctuation or digits
Each operation changes information. Lowercasing can remove distinctions that matter, such as names or German capitalization. NFKC can turn compatibility characters into ordinary characters, which is useful for consistency but may erase presentation details.
| Operation | Possible benefit | Main risk |
|---|---|---|
| NFC | Combines equivalent Unicode forms | Does not remove case differences |
| NFKC | Reduces compatibility variants | May change meaningful typography |
| Lowercasing | Reduces vocabulary duplication | Loses case information |
| Whitespace cleanup | Makes spacing consistent | Can damage formatting or code |
| Punctuation rules | Creates predictable boundaries | May mishandle contractions or symbols |
Pretokenisation creates useful boundaries
Pretokenisation is the step that divides normalised text into coarse pieces before subword learning. A pretokenizer might split around spaces and punctuation:
import re
import unicodedata
def normalise_and_pretokenise(text):
text = unicodedata.normalize("NFC", text)
text = text.casefold()
text = re.sub(r"\s+", " ", text).strip()
# Keep words, numbers, and each non-whitespace symbol as separate pieces.
return re.findall(r"\w+|[^\w\s]", text, flags=re.UNICODE)
sample = " Café's price is €5.00! "
print(normalise_and_pretokenise(sample))The output is:
['café', "'", 's', 'price', 'is', '€', '5', '.', '00', '!']A BPE trainer can now learn frequent patterns inside these pieces. For example, it may learn ca, fé, or price. The exact result depends on the corpus and the rules used.
Pretokenisation is not the same as final tokenisation. It establishes constraints. If punctuation is always separated, a later merge algorithm may be unable to create a token that crosses that boundary. Some tokenizers allow selected merges across spaces or punctuation; others do not.
Keep training and inference identical
The same normalisation and pretokenisation pipeline must run when preparing training data and when encoding new input. Otherwise, the vocabulary is learned from one representation and applied to another.
Common errors
- Applying lowercasing at inference time when the tokenizer was trained with case preserved.
- Normalising only some data sources.
- Splitting punctuation differently between training and serving.
- Treating code, URLs, or identifiers like ordinary prose.
- Removing whitespace without checking whether it carries meaning.
- Assuming Unicode characters that look alike have identical representations.
- Forgetting that a pretokenizer can affect sequence length and vocabulary coverage.
Exercise
Why can changing the pretokenisation rule after training reduce tokenizer quality?
Show answer
The learned vocabulary and merge statistics depend on the original boundaries. New boundaries can prevent useful merges and produce unfamiliar or longer sequences.