Files
HealthLog/src/lib/i18n/fold-for-match.ts
T
aucun6352 f7cc47850a fix(i18n): make the Korean locale actually work end to end
Registering `ko` in the `Locale` union left seven locale-keyed tables
without an entry, so the build could not type-check and the safety
screens, the citation-coverage grader and the reference-range parser
read nothing at all for a Korean reader.

Three failures were not missing keys but wrong assumptions:

- The shared fold decomposes with NFD and strips U+0300-U+036F. Latin
  accents become plain letters, but Hangul decomposes into jamo, which
  are not in that range and survived — so folded Korean text was a jamo
  sequence no precomposed table entry could ever match. It recomposes to
  NFC now; every Latin, Polish and German fold is byte-identical.
- `normaliseLabKey` kept `[a-z0-9]`, which reduced every Korean analyte
  name to the empty string, so the derived index held none of them and a
  Korean cholesterol result reached a FHIR export uncoded.
- The reference-range parser had the hyphen and the dashes but not the
  tilde a Korean lab actually prints (`3.5~5.0`).

The Korean safety banks carry no `\b` next to a Hangul token — `\b` is
an ASCII `\w` boundary and Hangul is not `\w`, so `/\b증량/` matches
nothing — and the clause order is the mirror of the Latin banks, since
Korean closes with the verb. The dose patterns require a target-marking
particle for the same reason the Latin banks require "to"/"by": without
it the permitted restatement "7.5mg을 복용 중이고, 체중은 내려가고
있어요" tripped the lowering pattern.

The Coach and Insights prompts route `ko` down the reviewed English body
plus a Korean reply directive; there is no `safety-contracts.ko.yaml`, so
the native builder has no Korean ground rules to compose from.
2026-08-26 20:00:44 +09:00

83 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Character folds used when app text has to be matched against text a PERSON
* or a LAB typed, rather than compared byte-for-byte.
*
* Every matcher that reads a shipped translation and looks for it in stored
* or transcribed text needs the same three tolerances — case, accents, and the
* apostrophe a word processor curled — and every matcher that grows its own
* copy of them drifts from the others. One home, so "Perte d'appétit" typed
* with a straight quote meets the bundle's "Perte d’appétit", and a French
* report's "jusqu'à" meets a table written "jusqu'a".
*
* The folds are one-directional and deliberately NOT transliteration: German
* "ue" does not fold onto "ü", "ß" is left alone (it has no canonical
* decomposition), and the Polish "ł" is a single codepoint NFD does not touch.
* That is fine — what has to agree is a shipped string with itself as a person
* retyped it, not two spellings of the same word.
*
* Client-safe: no message bundles are imported here, so a client component may
* use these. Building an index OVER the bundles is a server concern and lives
* in `./shared-resolve`.
*
* ## Why there is a second fold, and when to reach for it
*
* `./localised-label-index` carries `foldLabel`, and the two disagree ON
* PURPOSE rather than by accident. That one decomposes with NFKD, maps "ß" to
* "ss" and "œ" to "oe", and joins with "_"; this one decomposes with NFD,
* leaves "ß" alone, and joins with spaces.
*
* The difference is who typed the text. Here the other side of the comparison
* is a shipped label a person retyped, or prose a lab printed, so tolerating a
* DIFFERENT SPELLING would be reaching beyond what the fold can honestly
* claim. There, someone is naming a metric to an assistant in their own words,
* and "Suessstoff" for "Süßstoff" is the same request; the wider fold earns
* its keep because the caller is composing, not reproducing.
*
* Keep them apart. Whichever you pick, apply it to BOTH sides of the
* comparison — a fold on one side only is an exact-string match in a
* disguise.
*/
/** Curly, modifier and backtick apostrophes a keyboard or word processor emits. */
const APOSTROPHES = /[\u2018\u2019\u02bc\u00b4`']/gu;
/** Combining marks left behind by an NFD decomposition. */
const COMBINING_MARKS = /[\u0300-\u036f]/gu;
/**
* Drop accents by decomposing and removing the combining marks: "é" → "e",
* "ż" → "z", "Ü" → "U". Punctuation, case and spacing survive untouched, so
* this is the fold to use where the surrounding characters carry meaning — a
* printed range's "3,5-5,0", a unit's "mg/dL".
*/
export function stripDiacritics(raw: string): string {
// Recomposed with NFC after the strip because NFD splits Hangul into JAMO,
// and jamo are not in the COMBINING_MARKS range, so they survive: without
// this the folded form of "목표" is a jamo sequence that no precomposed
// literal in any table can match. Latin/Polish/German folds are unaffected —
// the combining marks are already gone, so there is nothing to recompose.
return raw.normalize("NFD").replace(COMBINING_MARKS, "").normalize("NFC");
}
/** Collapse every apostrophe variant onto the straight ASCII one. */
export function normaliseApostrophes(raw: string): string {
return raw.replace(APOSTROPHES, "'");
}
/**
* Fold a label to its comparison form: lower case, no accents, and every
* apostrophe / hyphen / underscore / slash reduced to a single space.
*
* This is the fold for whole LABELS — a tag, an analyte name, a qualitative
* result term — where internal punctuation is noise. Apply it identically to
* both sides of a comparison; a fold applied to only one side is an
* exact-string match wearing a disguise.
*/
export function foldForMatch(raw: string): string {
return stripDiacritics(raw.toLowerCase())
.replace(APOSTROPHES, " ")
.replace(/[-_/]/gu, " ")
.replace(/\s+/gu, " ")
.trim();
}