// RESPELLINGS — a transcript's mis-hearing, corrected where an article quotes it. // // Transcripts are machine-made and mishear brand names and coinages ("cake cup" for // K-Cup). A quote is verbatim from the transcript, so it carries the mis-hearing. An // article may declare, once, what the transcript hears and what was said: // // "respellings": [{ "heard": ["cake cake cup", "cake cup", "cakeup"], "reads": "K-Cup" }] // // and its quotes then show the proper word — plainly for an obvious correction, or // bracketed as an altered quote is ("because [K-Cup] machines are…") when the respelling // says `bracket: true`. Every check of a quote against the record applies // the same respellings to BOTH sides before comparing (lib/citations/verify.ts), so a // corrected quote verifies exactly as its verbatim original did and nothing else does. // // Matching: each heard form on word boundaries, case-insensitive, longest form first // (so "cake cake cup" wins over "cake cup"), a trailing plural "s" carried onto the // reading ("cake cups" → "K-Cups"). Text already corrected is left alone, so applying // twice changes nothing. // // Pure, no imports: the export site, the browser and umtool's .mjs scripts can use it. // Types: respell.d.mts. const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // One regex over every heard form of every respelling, longest first, with the reading // of each form beside it. function compile(respellings) { const readsOf = new Map(); for (const r of respellings) { const reads = r.reads.trim(); if (!reads) continue; for (const h of r.heard) { const key = h.trim().replace(/\s+/g, " ").toLowerCase(); if (key && !readsOf.has(key)) readsOf.set(key, { reads, bracket: r.bracket === true }); } } if (readsOf.size === 0) return null; const forms = [...readsOf.keys()].sort((a, b) => b.length - a.length); // A heard form's spaces match any run of whitespace; the form must stand on word // boundaries (letters/digits on neither side), with an optional plural "s". const alt = forms.map((f) => f.split(" ").map(escapeRe).join("\\s+")).join("|"); const re = new RegExp(`(? { const hit = c.readsOf.get(form.replace(/\s+/g, " ").toLowerCase()); const word = (hit?.reads ?? form) + plural; if (mark === "plain" || !hit?.bracket) return word; // Already inside brackets ("[cake cup]" written by hand): do not double them. const before = whole[offset - 1]; const after = whole[offset + form.length + plural.length]; return before === "[" && after === "]" ? word : `[${word}]`; }); } // How many places `text` would change. export function countRespellings(text, respellings) { if (!respellings?.length || !text) return 0; const c = compile(respellings); return c ? [...text.matchAll(c.re)].length : 0; } // The respellings a document declares, cleaned: blank forms and readings dropped, a // reading that is itself one of its heard forms dropped (it would never settle). export function cleanRespellings(input) { if (!Array.isArray(input)) return []; const out = []; for (const r of input) { if (!r || typeof r !== "object") continue; const reads = typeof r.reads === "string" ? r.reads.trim() : ""; const heard = Array.isArray(r.heard) ? r.heard.filter((h) => typeof h === "string" && h.trim() !== "").map((h) => h.trim()) : []; if (!reads || heard.length === 0) continue; if (heard.some((h) => h.toLowerCase() === reads.toLowerCase())) continue; const note = typeof r.note === "string" ? r.note : undefined; const bracket = r.bracket === true; out.push({ heard, reads, ...(bracket ? { bracket } : {}), ...(note ? { note } : {}) }); } return out; } // Respellings joined: one per reading, its heard forms merged (a later `bracket` or // `note` wins). export function mergeRespellings(existing, added) { const byReads = new Map(); for (const r of [...cleanRespellings(existing ?? []), ...cleanRespellings(added)]) { const prev = byReads.get(r.reads); if (!prev) { byReads.set(r.reads, { ...r, heard: [...r.heard] }); continue; } const heard = [...prev.heard]; for (const h of r.heard) if (!heard.some((x) => x.toLowerCase() === h.toLowerCase())) heard.push(h); const bracket = r.bracket !== undefined ? r.bracket === true : prev.bracket === true; const note = r.note ?? prev.note; byReads.set(r.reads, { heard, reads: r.reads, ...(bracket ? { bracket } : {}), ...(note ? { note } : {}) }); } return [...byReads.values()]; }