Sub topic 4
Normalising Text Before and After Tokenising
What normalisation changes
This chapter assumes you know basic Python, regular expressions, and terms such as noun, verb, plural, and tense. It also assumes you have seen tokenisation, which divides text into smaller units called tokens.
Text that looks identical to a person may contain different character sequences. For example, é can be stored as one character or as e followed by an accent mark. Text may also differ only in case, whitespace, or punctuation style. Normalisation changes these variations into a consistent form.
A normalisation pipeline often includes:
- Removing unnecessary whitespace.
- Applying Unicode normalisation.
- Applying case folding when the task is not case-sensitive.
- Tokenising the result.
- Optionally normalising individual tokens.
The order matters. If you lowercase text before tokenising, the tokenizer sees the lowercased form. If you remove punctuation after tokenising, punctuation tokens can be removed without changing words that contain punctuation.
Unicode and case normalisation
Unicode is the standard that assigns codes to characters from many writing systems. Unicode normalisation makes equivalent character sequences use a consistent representation.
The most common forms are:
- NFC combines characters into a standard composed form when possible.
- NFD separates composed characters into a base character and combining marks.
- NFKC also applies compatibility conversions. For example, some full-width characters become ordinary characters.
- NFKD performs compatibility conversion and decomposition.
NFC is usually a safe default when you want equivalent text to compare consistently. NFKC can be useful for search or identifiers, but it can also remove distinctions that matter. For example, formatting characters, mathematical symbols, or special typography may be changed.
Case folding is stronger than ordinary lowercasing and is designed for case-insensitive comparison across languages. In Python, use casefold() rather than lower() when comparison is the goal.
import unicodedata
def normalise_text(text: str) -> str:
text = unicodedata.normalize("NFC", text)
text = text.casefold()
return " ".join(text.split())
values = ["CAF\u00c9", "cafe\u0301", "Stra\u00dfe"]
for value in values:
print(normalise_text(value))The first two values produce the same normalised text. Straße becomes strasse under case folding. That is useful for matching, but it means the original spelling cannot be reconstructed from the normalised value.
Normalising before tokenisation
Pre-tokenisation normalisation should remove variation that does not carry useful meaning. Common choices include:
- Converting line breaks and repeated spaces into one space.
- Converting Unicode text to NFC.
- Case folding for case-insensitive search.
- Standardising selected quotation marks or dash characters when the application needs it.
Do not remove every punctuation mark automatically. Punctuation can carry meaning. In can't, the apostrophe indicates a contraction. In 3.14, the period is part of a number. In C++, symbols identify the programming language.
Normalising after tokenisation
Some decisions are safer after tokenisation. A tokenizer can preserve punctuation, contractions, and words before you decide what to discard.
| Operation | Typical purpose | Main risk |
|---|---|---|
| Case folding | Case-insensitive matching | Losing proper-name information |
| Unicode NFC | Equivalent character comparison | Usually low risk |
| Punctuation removal | Simpler feature vectors | Losing grammatical or numeric meaning |
| Stop-word removal | Smaller search features | Removing words that affect meaning |
For example, a search system might keep only word tokens and remove punctuation. A sentiment classifier might keep ! because it can signal emphasis. A named-entity recogniser should normally preserve case because Amazon and amazon may have different meanings.
Avoid normalising words into stems or lemmas as an automatic cleanup step. Stemming and lemmatisation are separate choices. A stemmer may turn several words into a form that is not a dictionary word, while a lemmatiser may need part-of-speech information. Apply either only when the task benefits from shared word forms.
Choosing a strategy
Use a conservative strategy when the original wording matters. Store the original text and keep normalised text as a separate representation. This lets you display the source accurately while searching or classifying a consistent version.
Use stronger normalisation when recall matters more than exact form. Recall is the proportion of relevant items that a system finds. Search often benefits from case folding and Unicode normalisation. Translation, authorship analysis, and some linguistic tasks may need the original case, punctuation, and accents.
Test normalisation with examples from the real task. Include accented characters, contractions, numbers, symbols, mixed case, and malformed input. Compare both matching results and errors introduced by each step.
Exercise
A case-sensitive named-entity recogniser receives Apple announced a product. Should you case-fold the text before tokenising? Why or why not?
Show answer
No. Case can help distinguish the company name Apple from the fruit. Use Unicode normalisation if needed, but preserve case for the recogniser.