Notes

Unicode Normalization

Two strings can look identical on screen, print identically, and still fail an equality test. The word café typed with a single accented character and the same word typed as e followed by a combining accent are different byte sequences — and to a program comparing them, different words.

Four forms, two decisions
Unicode normalization rewrites text into a canonical form so that equivalent strings become byte-equal. The standard defines four forms along two axes — composed or decomposed, canonical or compatibility:

  • NFC composes sequences into precomposed characters; NFD decomposes them into a base letter plus combining marks;
  • NFKC and NFKD additionally fold compatibility equivalents, so the fi ligature becomes two letters, full-width A becomes A, and a superscript ² becomes 2.

What it rescues downstream
The vocabulary, and everything built on it. Each unnormalised spelling becomes its own type: counts split, embeddings duplicate, dictionary lookups miss. A search box has to match café against café whichever keyboard composed it; a deduplicator has to see two scraped copies of an article as one text; a subword tokenizer trained on NFC text wastes vocabulary on NFD spellings. Python's unicodedata.normalize is the usual tool, with NFC the default for stored text and NFKC common before indexing, where typographic distinctions are noise.

Where to be careful
Compatibility folding is lossy and irreversible: it erases distinctions that sometimes carry meaning, such as superscripts in formulae or the separate mathematical alphabets. Normalising also changes string lengths, so entity offsets and annotation spans recorded against the raw text must be recomputed against the normalised version, and the same form has to be applied at training and at inference.

Unicode normalization rewrites text into a canonical form so that visually identical strings become byte-identical — a single accented character and the equivalent base letter plus combining accent collapse into one spelling. The standard's forms differ in whether characters are composed (NFC) or decomposed (NFD), and whether compatibility variants such as ligatures and full-width letters are folded (NFKC, NFKD). Without it, one word splits into several vocabulary entries and exact matching fails.

Think of two people writing the word café by hand. One draws the accented e in a single stroke. The other writes a plain e and then adds the little accent on top. The finished words look the same, but they were built differently.

Computers store text in much the same way, and they are literal-minded: two spellings that look identical to you can sit in memory as different sequences, so a search for one quietly misses the other. Unicode normalization is the tidying step that rewrites every variant into one agreed spelling. It is why a search box finds café whichever keyboard typed it, and why counting words does not accidentally treat one word as two.