I maintain a term table for jadeite grading that has to work in English and Vietnamese. It is a small project — a few hundred rows, one column per language, one canonical key — and it broke three separate times before I understood why. All three failures came from the same cause, and it was not the translation.
The failure that looks like a translation bug
A row was entered as Ngọc phỉ thúy. A second row, added three weeks later from a different source, was entered as Ngọc phỉ thúy. They look identical on screen. The unique index on the term column accepted both. Search for one and you got one result; search for the other and you got nothing.
They are not the same string. The first uses precomposed characters — one code point per accented letter. The second uses a base letter plus a separate combining mark. Vietnamese, more than almost any Latin-script language, ends up with both forms in the wild, because input methods, PDF text extraction, and copy-paste from different operating systems disagree about which form to emit.
import unicodedata a = unicodedata.normalize('NFC', 'Ngọc phỉ thúy') b = unicodedata.normalize('NFD', 'Ngọc phỉ thúy') print(len(a), len(b)) # not equal print(a == b) # False print(unicodedata.normalize('NFC', b) == a) # True The second string is not corrupt. It is a valid, correctly rendered Vietnamese word that happens to be stored differently. Every comparison in your system — WHERE term = ?, dict[key], GROUP BY term — compares code points, not appearance. Appearance is a rendering decision made much later, by the font.
Three places this bites
Uniqueness constraints. Deduplication silently fails. You end up with two rows, both correct, both used by different parts of the app, and a report that double-counts whichever thing they describe.
Lookup. A form submitted by a user on a phone rarely matches the form that came out of a scanned supplier sheet. The lookup misses, the app shows "not found", and the person maintaining the table concludes the data is wrong when the data is fine.
Sorting and grouping. Collation of decomposed strings is not the same as collation of precomposed ones in every database. Two rows you consider identical land in different groups.
The rule I settled on
Normalize to NFC on the way in. Not on the way out, not at comparison time — at write time, in one place, before anything else touches the value.
import unicodedata def canon(s: str) -> str: if s is None: return None s = unicodedata.normalize('NFC', s) s = s.strip() s = ' '.join(s.split()) # collapse runs of whitespace return s # write path row['term_vi'] = canon(raw_term_vi) NFC rather than NFD because it is what the browser, the database driver, and every JSON tool I use emit by default. Choosing the form that is already the default everywhere means fewer conversions, and every conversion is a chance to introduce a bug.
I also strip and collapse whitespace in the same function. Non-breaking spaces pasted from web pages are the second most common invisible difference, and they cause the same class of failure.
Do not stop at the accent
Normalizing Unicode fixes the encoding layer. It does not fix the fact that the same object has several legitimate names. In jadeite grading, a single piece carries a Type A/B/C treatment status, a colour band, and a water/translucency description — and the Vietnamese retail trade, the English wholesale trade, and the lab report each prefer different words for the same property.
So the table needs two separate concepts: a canonical key that never appears in the UI, and surface variants that do. One key, many labels per language.
{ "key": "treatment.type_b", "labels": { "en": ["polymer impregnated", "B-treated", "B jade"], "vi": ["xử lý loại B", "ngọc loại B"] }, "source": "lab report field 4", "confidence": "high" } The key is stable and machine-comparable. The labels are what humans type and what search has to match against. Adding a synonym becomes a data change instead of a code change, which is the whole point of having a table.
Order of operations that actually works
- Normalize encoding (NFC) and whitespace before insert. One function, one call site.
- Store the normalized value, and keep the original in a
rawcolumn if provenance matters. - Compare on the canonical key, never on the display label.
- Treat every synonym as data, not as special-case code.
- Add a test that inserts the same term in both NFC and NFD form and asserts one surviving row. That test is the only thing that keeps the invariant from rotting.
None of this is specific to Vietnamese, and none of it is specific to gemstones. Any table that collects human-entered text from more than one source — supplier sheets, phone forms, PDF extraction, OCR — will hit the same wall. The fix is cheap if you decide on it before the table has three thousand rows.
The longer treatment of how these grading terms map onto actual lab report fields is on the Vietnamese-language reference site, and there is a shorter buyer-facing walkthrough of the A/B/C distinction on this site.
If you maintain a similar table, the one habit worth copying is the failing test. Everything else follows from being told, loudly, the moment two strings that look the same stop being the same.

