// respell.mjs: a transcript's mis-hearing, corrected where a quote shows it — and the // quote checks, which apply the same respellings to both sides. import { test } from "node:test"; import assert from "node:assert/strict"; import { applyRespellings, cleanRespellings, countRespellings, mergeRespellings } from "./respell.mjs"; import { quoteVerification, quoteDrifted, strictQuoteCheck, strictTextCheck } from "./verify"; const KCUP = [{ heard: ["cake cake cup", "cake cup", "cakeup"], reads: "K-Cup" }]; test("plain by default: every heard form, case-insensitive, on word boundaries, a plural carried", () => { assert.equal(applyRespellings("because cake cup machines are expensive", KCUP), "because K-Cup machines are expensive"); assert.equal(applyRespellings("I bought my own cakeup machine", KCUP), "I bought my own K-Cup machine"); assert.equal(applyRespellings("Cake Cups everywhere", KCUP), "K-Cups everywhere"); assert.equal(applyRespellings("a pancake cupboard and cakeupper", KCUP), "a pancake cupboard and cakeupper"); }); test("the longest heard form wins, so a stutter is one correction", () => { assert.equal(applyRespellings("our own cake cake cup machine", KCUP), "our own K-Cup machine"); }); test("bracketed only when the respelling asks; 'plain' never brackets; no double brackets", () => { const b = [{ ...KCUP[0], bracket: true }]; assert.equal(applyRespellings("my own cakeup machine", b), "my own [K-Cup] machine"); assert.equal(applyRespellings("my own cakeup machine", b, "plain"), "my own K-Cup machine"); assert.equal(applyRespellings("my own [cakeup] machine", b), "my own [K-Cup] machine"); }); test("applying twice changes nothing; no respellings is the text unchanged", () => { const once = applyRespellings("cake cup and cakeup", KCUP); assert.equal(applyRespellings(once, KCUP), once); assert.equal(applyRespellings("cake cup", undefined), "cake cup"); assert.equal(applyRespellings("cake cup", []), "cake cup"); assert.equal(countRespellings("cake cup, cakeups and cake cake cup", KCUP), 3); }); test("clean drops blanks, a reading that is one of its heard forms, and non-objects", () => { assert.deepEqual( cleanRespellings([ { heard: [" cake cup ", ""], reads: " K-Cup " }, { heard: ["k-cup"], reads: "K-Cup" }, { heard: [], reads: "X" }, "nope", { heard: ["kay cup"], reads: "K-Cup", bracket: true, note: "brand" }, ]), [ { heard: ["cake cup"], reads: "K-Cup" }, { heard: ["kay cup"], reads: "K-Cup", bracket: true, note: "brand" }, ], ); }); test("merge joins heard forms per reading; a later bracket or note wins", () => { const m = mergeRespellings([{ heard: ["cake cup"], reads: "K-Cup", note: "a" }], [ { heard: ["Cake Cup", "cakeup"], reads: "K-Cup", bracket: true }, { heard: ["stix"], reads: "Styx" }, ]); assert.deepEqual(m, [ { heard: ["cake cup", "cakeup"], reads: "K-Cup", bracket: true, note: "a" }, { heard: ["stix"], reads: "Styx" }, ]); }); const cues = [ { start: 10, end: 14, text: "because cake cup machines are very very expensive" }, { start: 14, end: 18, text: "so we pay somebody with a cake cup machine" }, ]; const said = cues.map((c) => c.text).join(" "); test("a respelled quote verifies as its verbatim original did — recall and strict", () => { const quote = applyRespellings(said, KCUP); const v = quoteVerification(quote, said, "t", KCUP); assert.equal(v.quoteScore, 1); assert.equal(strictQuoteCheck(quote, cues, 10, { respellings: KCUP }).ok, true); assert.equal(strictTextCheck(quote, said, KCUP).ok, true); // Bracketed reads the same to the checks. const bracketed = applyRespellings(said, [{ ...KCUP[0], bracket: true }]); assert.equal(strictQuoteCheck(bracketed, cues, 10, { respellings: KCUP }).ok, true); }); test("without the respelling declared the corrected quote is not verbatim", () => { const quote = applyRespellings(said, KCUP); assert.equal(strictQuoteCheck(quote, cues, 10).ok, false); assert.ok((quoteVerification(quote, said, "t").quoteScore ?? 1) < 1); }); test("a respelling never makes an unrelated quote pass", () => { const v = quoteVerification("the K-Cup machines are cheap and free", said, "t", KCUP); assert.equal(strictQuoteCheck("K-Cup machines are cheap", cues, 10, { respellings: KCUP }).ok, false); assert.ok((v.quoteScore ?? 1) < 1); // A short corrected quote drifts without its respelling, and passes with it. assert.equal(quoteDrifted(quoteVerification("K-Cup machine", "cake cup machine", "t")), true); assert.equal(quoteDrifted(quoteVerification("K-Cup machine", "cake cup machine", "t", KCUP)), false); });