Sub topic 5
Compatibility Forms: NFKC and NFKD
Prerequisites and the problem they solve
This chapter assumes you know basic Python strings, list comprehensions, and the idea that text can be split into tokens. You should also know that Unicode assigns code points to characters and that UTF-8 encodes those code points as bytes.
Unicode has two kinds of equivalence that matter during text cleaning:
- Canonical equivalence means two sequences represent the same character. For example,
écan be one code point or anefollowed by a combining acute accent. - Compatibility equivalence means two characters have related meanings or appearances, but may carry different formatting or semantic information. For example, the ligature
firepresents the lettersfandi, while the circled digit①represents the number1.
NFC and NFD handle canonical equivalence. NFKC and NFKD also apply compatibility mappings. The K in each name means compatibility.
NFKC and NFKD compared
NFKD first decomposes characters into simpler sequences. NFKC performs the same compatibility decomposition and then recomposes the result where possible.
| Form | Decomposes compatibility characters | Recombines characters |
|---|---|---|
| NFKD | Yes | No |
| NFKC | Yes | Yes |
For example, NFKD can convert fi into two code points, f and i. NFKC also produces the text fi, but stores it as a recomposed result when a composed form exists.
Both forms can convert:
fitofi- Full-width
ABCtoABC - Circled
①to1 - Superscript
²to2in many contexts
Use NFKC when you need a normalised string for comparison, indexing, or lookup. Use NFKD when you need to inspect or process the decomposed pieces, such as removing combining marks.
import unicodedata
samples = ["\ufb01", "\uff21\uff22\uff23", "\u2460", "\u00b2"]
for text in samples:
print(repr(text))
print("NFKC:", repr(unicodedata.normalize("NFKC", text)))
print("NFKD:", repr(unicodedata.normalize("NFKD", text)))The output shows that NFKC and NFKD often have the same visible result. Their internal code-point sequences can still differ.
Why compatibility normalisation is useful
Compatibility characters often create accidental mismatches. A user might enter a full-width form of a Latin letter, while your stored search key contains the ordinary form. Without normalisation, the strings compare as unequal.
NFKC is useful for:
- Search keys and case-insensitive lookup keys
- Usernames or identifiers where compatibility distinctions are not meaningful
- Text imported from documents with typographic ligatures
- Comparing text from systems that use full-width forms
- Preparing text before tokenisation
A common comparison pipeline applies NFKC before case folding:
import unicodedata
def comparison_key(text):
text = unicodedata.normalize("NFKC", text)
return text.casefold()
left = "File \ufb01"
right = "file fi"
print(comparison_key(left))
print(comparison_key(right))
print(comparison_key(left) == comparison_key(right))Normalising before case folding gives one predictable representation. It also ensures that any compatibility characters created or changed by earlier processing are handled before comparison.
When NFKD is the better choice
NFKD exposes the components of a string. This makes it useful when a later step needs to inspect combining marks. For example, accent stripping commonly uses NFKD, filters out combining marks, and then applies NFC to produce a conventional result.
import unicodedata
def remove_marks(text):
decomposed = unicodedata.normalize("NFKD", text)
without_marks = "".join(
character
for character in decomposed
if not unicodedata.combining(character)
)
return unicodedata.normalize("NFC", without_marks)
print(remove_marks("Crème brûlée"))NFKD alone does not remove accents. It only exposes the accents as separate combining characters. The filtering step makes the information loss explicit.
This process is appropriate for a loose search feature where resume should find résumé. It may be inappropriate for named entity recognition, language learning, legal names, or sentiment analysis, where accents can distinguish words or provide useful language information.
Decision rules and failure modes
Choose NFKC when you want one compact comparison form and compatibility distinctions do not matter. Choose NFKD when you need to examine or remove combining marks.
Do not use compatibility normalisation automatically on every stored value. Store the original user input when it must be displayed or preserved. Store a separate normalised key when searching or deduplicating.
A normalisation pipeline should also use the same policy on both sides of a comparison. Normalising only the input or only the database value still produces mismatches.
Exercise
Which form should you normally choose for a search comparison key: NFKC or NFKD, and why?
Show answer
Choose NFKC because it applies compatibility mappings while producing a compact, recomposed form. Choose NFKD instead when a later step must inspect or remove combining marks.