feat: chat ID/DOB with Subscriber #/month-name DOB, partial denture and amalgam CDT codes

- Chat bot detects "Subscriber #: ... Patient date of birth: Jul 31, 1982 Age: 44"
  (subscriber/member # labels, month-name dates, age ignored)
- Partial dentures coded by arch and material regardless of tooth numbers
  (D5211-D5214, D5225/D5226), plus repairs (D5611-D5622), relines (D5740-D5761)
  and per-tooth work (D5630, D5640, D5650, D5660)
- Amalgam fillings use D2140/D2150/D2160/D2161 by surface count instead of composite codes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 23:40:46 -05:00
co-authored by Claude Opus 5.5
parent 6c98f7f985
commit 108c72e570
3 changed files with 99 additions and 13 deletions
+65 -7
View File
@@ -308,6 +308,8 @@ const RCT_MOLAR = new Set([1, 2, 3, 14, 15, 16, 17, 18, 19, 30, 31, 32]); /
* Parse composite filling notation like "#29 OB", "composite #8 MO", "tooth 11 MOD", "#K O".
* Returns the correct D233x/D239x code based on tooth location and surface count.
*/
const AMALGAM_RE = /\b(amalgams?|amalg|amal|silver)\b/i;
function parseCompositeCode(input: string): CdtMatch | null {
const s = input.trim();
// Match #NN SURFACES, tooth NN SURFACES, or primary #X SURFACES (surfaces = 1-5 letters, valid dental surface chars)
@@ -337,7 +339,10 @@ function parseCompositeCode(input: string): CdtMatch | null {
}
let code: string;
if (isFront) {
if (AMALGAM_RE.test(s)) {
// Amalgam codes don't split anterior/posterior
code = surfaceCount === 1 ? "D2140" : surfaceCount === 2 ? "D2150" : surfaceCount === 3 ? "D2160" : "D2161";
} else if (isFront) {
code = surfaceCount === 1 ? "D2330" : surfaceCount === 2 ? "D2331" : surfaceCount === 3 ? "D2332" : "D2335";
} else {
code = surfaceCount === 1 ? "D2391" : surfaceCount === 2 ? "D2392" : surfaceCount === 3 ? "D2393" : "D2394";
@@ -476,6 +481,52 @@ function parsePonticCode(input: string): { match: CdtMatch; explicit: boolean }
};
}
/**
* Parse removable partial denture wording by arch and material, ignoring any tooth
* numbers (a partial is coded per arch): "upper partial #2,3" → D5211,
* "lower cast metal partial" → D5214, "flexible upper partial" → D5225.
* Also repairs and relines: "repair upper partial" → D5612, "reline lower partial lab" → D5761.
* Per-tooth work (clasp repair, add tooth/clasp, replace broken teeth) keeps the tooth#.
* Fixed partials (bridges) are left to the other lookups.
*/
function parsePartialDentureCode(input: string): CdtMatch | null {
const lower = input.toLowerCase();
if (!/\bpartials?\b/.test(lower)) return null;
if (/\b(fixed|fpd|bridge)\b/.test(lower)) return null;
const withRow = (code: string, perTooth = false): CdtMatch => {
const row = ALL_CODES.find((r) => r["Procedure Code"] === code);
return { code, description: row?.Description ?? code, input, ...(perTooth ? extractToothSurface(input) : {}) };
};
// Per-tooth codes — same for either arch
const add = /\badd(s|ed|ing)?\b/.test(lower);
const clasp = /\bclasps?\b/.test(lower);
if (add && clasp) return withRow("D5660", true);
if (add && /\b(tooth|teeth)\b/.test(lower)) return withRow("D5650", true);
if (clasp && /\b(repairs?|repaired|broken|replace[ds]?|fix)\b/.test(lower)) return withRow("D5630", true);
if (/\b(tooth|teeth)\b/.test(lower) && /\b(broken|replace[ds]?)\b/.test(lower)) return withRow("D5640", true);
const upper = /\b(upper|maxillary|max|maxilla)\b/.test(lower);
const lowerArch = /\b(lower|mandibular|mand|mandible)\b/.test(lower);
if (upper === lowerArch) return null;
const cast = /\b(cast|metal|chrome|cobalt|cr-?co|framework)\b/.test(lower);
if (/\breline[ds]?\b/.test(lower)) {
const lab = /\b(lab|laboratory|indirect)\b/.test(lower);
return withRow(lab ? (upper ? "D5760" : "D5761") : (upper ? "D5740" : "D5741"));
}
if (/\b(repairs?|repaired|broken|fix)\b/.test(lower)) {
return withRow(cast ? (upper ? "D5622" : "D5621") : (upper ? "D5612" : "D5611"));
}
if (/\b(add|replace)\b/.test(lower)) return null;
if (cast) return withRow(upper ? "D5213" : "D5214");
if (/\b(flexible|flex|valplast)\b/.test(lower)) return withRow(upper ? "D5225" : "D5226");
return withRow(upper ? "D5211" : "D5212");
}
/**
* Parse RCT/root-canal notation like "rct #29", "#14 rct", "root canal #6".
* Returns D3310 (anterior), D3320 (premolar), or D3330 (molar) based on tooth number.
@@ -635,25 +686,28 @@ const BARE_TOOTH_SURFACE_RE = /^#?\s*(\d{1,2}|[A-T])\s+([OMDBLFIV]{1,5})$/i;
* "comp on #18 O, 20 O, 21 O" → ["comp #18 O", "comp #20 O", "comp #21 O"].
* Also handles the LLM splitting that list into ["comp on #18 O", "20 O", ...]:
* a bare "20 O" right after a composite entry is another composite.
* Amalgam lists expand the same way but stay amalgam: "amalgam #15 MO, 14 O".
*/
function expandCompositeLists(procedureNames: string[]): string[] {
const out: string[] = [];
let prevComposite = false;
let prevPrefix: string | null = null;
for (const name of procedureNames) {
const s = name.trim();
if (prevComposite && BARE_TOOTH_SURFACE_RE.test(s)) {
if (prevPrefix && BARE_TOOTH_SURFACE_RE.test(s)) {
const m = s.match(BARE_TOOTH_SURFACE_RE)!;
out.push(`comp #${m[1]!.toUpperCase()} ${m[2]}`);
out.push(`${prevPrefix} #${m[1]!.toUpperCase()} ${m[2]}`);
continue;
}
const isComposite = /^(comp|composite|resin|filling)\b/i.test(s) || /^#\s*(\d{1,2}|[A-T])\s+[OMDBLFIV]{1,5}\b/i.test(s);
const isAmalgam = AMALGAM_RE.test(s);
const isComposite = isAmalgam || /^(comp|composite|resin|filling)\b/i.test(s) || /^#\s*(\d{1,2}|[A-T])\s+[OMDBLFIV]{1,5}\b/i.test(s);
const prefix = isAmalgam ? "amalgam" : "comp";
const teeth = isComposite ? [...s.matchAll(TOOTH_SURFACE_RE)] : [];
if (teeth.length > 1) {
for (const m of teeth) out.push(`comp #${(m[1] ?? m[2])!.toUpperCase()} ${m[3]}`);
for (const m of teeth) out.push(`${prefix} #${(m[1] ?? m[2])!.toUpperCase()} ${m[3]}`);
} else {
out.push(name);
}
prevComposite = teeth.length > 0;
prevPrefix = teeth.length > 0 ? prefix : null;
}
return out;
}
@@ -763,6 +817,10 @@ export function lookupCdtCodes(
return [pontic.match];
}
// 2c. Removable partial denture by arch/material — tooth numbers don't change the code
const partialMatch = parsePartialDentureCode(name);
if (partialMatch) return [partialMatch];
// 3. Multi-PA shorthand: "4PA", "4PA #3 #14 #19 #30", etc.
const multiPaMatches = parseMultiPaCode(name);
if (multiPaMatches) return multiPaMatches;
+17 -3
View File
@@ -275,12 +275,26 @@ export function restoreCodeToothLists(message: string, names: string[]): string[
return kept;
}
// Field labels copied along with the values, e.g. "Date of Birth 05/03/1952\nMember ID S01057721".
// Field labels copied along with the values, e.g. "Date of Birth 05/03/1952\nMember ID S01057721"
// or "Subscriber #: 700002459574\nPatient date of birth: Jul 31, 1982 Age: 44".
const ID_DOB_LABEL_RE =
/(?<![A-Za-z0-9])(?:date\s+of\s+birth|birth\s*date|d\.?o\.?b\.?|member\s*(?:id|number|no\.?)|subscriber\s*id|insurance\s*id|id)(?![A-Za-z0-9])\s*[:#]?/gi;
/(?<![A-Za-z0-9])(?:(?:patient\s+)?(?:date\s+of\s+birth|birth\s*date|d\.?o\.?b\.?)|(?:member|subscriber|insurance)(?:\s*(?:id|number|no\.?))?|id)(?![A-Za-z0-9])(?:\s*[:#])*/gi;
const AGE_RE = /(?<![A-Za-z0-9])age\s*[:#]?\s*\d{1,3}(?!\d)/gi;
const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
// "Jul 31, 1982" / "July 31 1982" -> "07/31/1982"
const MONTH_NAME_DATE_RE =
/\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\.?\s+(\d{1,2}),?\s+(\d{4})\b/gi;
function normalizeIdDobInput(input: string): string {
return input
.replace(AGE_RE, " ")
.replace(MONTH_NAME_DATE_RE, (_m, mon: string, d: string, y: string) =>
`${String(MONTHS.indexOf(mon.toLowerCase()) + 1).padStart(2, "0")}/${d.padStart(2, "0")}/${y}`)
.replace(ID_DOB_LABEL_RE, " ");
}
function parseBareIdDob(message: string): { memberId: string; dob: string } | null {
const text = message.replace(ID_DOB_LABEL_RE, " ").replace(/\s+/g, " ").trim();
const text = normalizeIdDobInput(message).replace(/\s+/g, " ").trim();
const dobPattern = /(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})/;
const idThenDob = text.match(new RegExp(`^([A-Za-z0-9]{3,})[\\s,]+${dobPattern.source}$`));
if (idThenDob) return { memberId: idThenDob[1]!, dob: idThenDob[2]!.replace(/-/g, "/") };
@@ -154,14 +154,28 @@ function getAutoCheck(_dobISO: string): "mh" {
return "mh";
}
// Field labels copied along with the values, e.g. "Date of Birth 05/03/1952\nMember ID S01057721".
// Field labels copied along with the values, e.g. "Date of Birth 05/03/1952\nMember ID S01057721"
// or "Subscriber #: 700002459574\nPatient date of birth: Jul 31, 1982 Age: 44".
const ID_DOB_LABEL_RE =
/(?<![A-Za-z0-9])(?:date\s+of\s+birth|birth\s*date|d\.?o\.?b\.?|member\s*(?:id|number|no\.?)|subscriber\s*id|insurance\s*id|id)(?![A-Za-z0-9])\s*[:#]?/gi;
/(?<![A-Za-z0-9])(?:(?:patient\s+)?(?:date\s+of\s+birth|birth\s*date|d\.?o\.?b\.?)|(?:member|subscriber|insurance)(?:\s*(?:id|number|no\.?))?|id)(?![A-Za-z0-9])(?:\s*[:#])*/gi;
const AGE_RE = /(?<![A-Za-z0-9])age\s*[:#]?\s*\d{1,3}(?!\d)/gi;
const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
// "Jul 31, 1982" / "July 31 1982" -> "07/31/1982"
const MONTH_NAME_DATE_RE =
/\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\.?\s+(\d{1,2}),?\s+(\d{4})\b/gi;
function normalizeIdDobInput(input: string): string {
return input
.replace(AGE_RE, " ")
.replace(MONTH_NAME_DATE_RE, (_m, mon: string, d: string, y: string) =>
`${String(MONTHS.indexOf(mon.toLowerCase()) + 1).padStart(2, "0")}/${d.padStart(2, "0")}/${y}`)
.replace(ID_DOB_LABEL_RE, " ");
}
function parseEligibilityInput(
input: string
): { memberId: string; display: string; iso: string } | null {
const raw = input.replace(ID_DOB_LABEL_RE, " ");
const raw = normalizeIdDobInput(input);
const dateMatch = raw.match(/\b(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})\b/);
if (!dateMatch) return null;
const m = dateMatch[1];