feat(editor): fuzzy zone matcher (EN-first, FR fallback, typo-tolerant)

ObservationCard previously bridged SVG zones to the JSON findings
catalog with a strict FR-name equality check. That broke as soon as the
JSON used "(ATM)" while the SVG carried "Articulation Temporo-
Mandibulaire (ATM)" or whenever either side picked up a minor casse /
accent / typo drift.

The new matcher (src/lib/zoneMatch.ts) normalizes both sides
aggressively (lowercase + NFD-strip accents + drop non-alphanumerics)
and scores each pairing across all four (bone-lang, zone-lang)
combinations:

  0     exact normalized match
  1     one normalized string is contained in the other
  2+d   Levenshtein distance d, capped at max(2, ceil(maxLen * 0.25))
  ∞     beyond tolerance — discarded

Preference order: English exact/substring first (no accents → fewer
normalization edge cases), then French exact/substring, then fuzzy
across both languages.

Real-world payoff for the current equine catalog:

  bone EN "Sphenoid"                                    → sphenoid (score 0)
  bone EN "Spheno-Basilar Synchondrosis (SBS)"          → ssb      (score 0)
  bone EN "Temporomandibular Joint (TMJ)"               → atm      (score 0)
  bone FR "(ATM)" alone                                 → atm      (score 1)
  bone EN "sphenoi" / "shenoide" (typo)                 → sphenoid (fuzzy)
  bone EN "Hyoid", "Hip joint", "Stifle", ...           → undefined (correct)

23 new unit tests cover normalization, Levenshtein, scorePair branches,
the full matchZoneFindings strategy ladder, and the absence of false
positives (Sphenoid ↛ SSB, Hyoid/Hanche/etc ↛ anything).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude Opus 4.7 committed 2026-05-20 04:25:05 +02:00
1 parent 1731adf620
commit 95d746b555
3 files changed
+302 -17

No files matched your search

+18 -17
View File
@@ -9,6 +9,7 @@ import type {
ZoneFindings,
} from '@/domain/schema/findings'
import type { Bone } from '@/anatomy/types'
import { matchZoneFindings } from '@/lib/zoneMatch'
import { AnimatePresence, motion } from 'motion/react'
import Card from '@/components/ui/Card'
import Badge from '@/components/ui/Badge'
@@ -83,34 +84,34 @@ export default function ObservationCard({
}
}
// SVG data-anno-id values are opaque (e.g. "point-mpbyk..."); the JSON
// catalog keys observations by stable human zone names (e.g. "sphenoid").
// We bridge the two via the French label parsed from data-anno-name-fr,
// which matches the JSON zone.name_fr exactly.
const labelToTree = useMemo(() => {
const m = new Map<string, ZoneFindings>()
if (!findings) return m
for (const tree of Object.values(findings.zones)) {
m.set(tree.name_fr, tree)
}
return m
}, [findings])
// SVG data-anno-id values are opaque generator strings (e.g.
// "point-mpbyk..."); the JSON catalog keys observations by stable
// human zone names (e.g. "sphenoid"). Bridge the two by matching the
// bone's FR/EN labels against the catalog labels — EN exact first,
// then FR, then small typos / abbreviations via fuzzy scoring.
const zonesArray = useMemo(
() => (findings ? Object.values(findings.zones) : []),
[findings],
)
const zonesWithTree = useMemo(() => {
if (!findings) return [] as { tree: ZoneFindings }[]
if (zonesArray.length === 0) return [] as { tree: ZoneFindings }[]
const seen = new Set<string>()
const out: { tree: ZoneFindings }[] = []
for (const z of observation.zones) {
const label = bones.get(z.boneId)?.fr
if (!label) continue
const tree = labelToTree.get(label)
const bone = bones.get(z.boneId)
if (!bone) continue
const tree = matchZoneFindings(
{ fr: bone.fr, en: bone.en },
zonesArray,
)
if (!tree) continue
if (seen.has(tree.id)) continue
seen.add(tree.id)
out.push({ tree })
}
return out
}, [observation.zones, bones, findings, labelToTree])
}, [observation.zones, bones, zonesArray])
return (
<Card