// post-say.mjs: the pronunciation lexicon (whole words, case, phrases, regex // characters, an item's own entries, what wins) and its place in what a voice // SAYS (post-redact.mjs spokenRead: the slur filter and the redaction win over // it); a read said the old way (staleLexicon); the voice map. The slurs come // from slurs.json: no test spells one out. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { applyLexicon, handleKey, lexiconFor, lexiconHits, staleLexicon, takeLexiconErrors, validateLexicon, validateVoiceMap, voiceForPiece, voiceOverridesOf, } from "./post-say.mjs"; import { spokenRead } from "./post-redact.mjs"; import { resolveSlurs } from "./post-slurs.mjs"; import { validatePostNarration } from "./post-narration.mjs"; const LIST = JSON.parse(readFileSync(new URL("./slurs.json", import.meta.url), "utf8")); const F = LIST.entries.find((e) => e.id === "f-slur").word; const say = (text, lex, o) => applyLexicon(text, lex, o).text; test("a whole word, without case by default: tbh, TBH and Tbh all said T-B-H; never inside a word", () => { const lex = [{ word: "tbh", say: "T-B-H" }]; assert.equal(say("tbh it's fine. TBH! (Tbh)", lex), "T-B-H it's fine. T-B-H! (T-B-H)"); assert.equal(say("tbhx xtbh tbh_ _tbh tbh2", lex), "tbhx xtbh tbh_ _tbh tbh2"); // An apostrophe and a letter after it are the word going on ("can" is not the "can" of "can't"). assert.equal(say("I can't, I can.", [{ word: "can", say: "tin can" }]), "I can't, I tin can."); // With case: only as written. assert.equal(say("US us Us", [{ word: "US", say: "U.S.", case: true }]), "U.S. us Us"); }); test("a phrase across any spaces; regex characters are literal; @handles and C++", () => { assert.equal(say("at the\nmoment, at the moments", [{ word: "at the moment", say: "right now" }]), "right now, at the moments"); assert.equal(say("@grok is this true? grok", [{ word: "@grok", say: "At grok" }]), "At grok is this true? grok"); assert.equal(say("me@grok", [{ word: "@grok", say: "At grok" }]), "me@grok"); assert.equal(say("C++ and C+", [{ word: "C++", say: "C plus plus" }]), "C plus plus and C+"); assert.equal(say("a (sic) b sic", [{ word: "(sic)", say: "sick" }]), "a sick b sic"); assert.equal(say("1.5x a 1x5x", [{ word: "1.5x", say: "one and a half times" }]), "one and a half times a 1x5x"); assert.equal(say("$5 or 5", [{ word: "$5", say: "five dollars" }]), "five dollars or 5"); }); test("what wins: an item's own entry over the project's, a longer match over one inside it; a replacement is never matched again", () => { const lex = [ { word: "atm", say: "A-T-M" }, { word: "atm", say: "at the moment", item: "p-2" }, { word: "not atm", say: "not right now" }, { word: "A-T-M", say: "loop" }, ]; assert.equal(say("atm, not atm", lex, { item: "p-1" }), "A-T-M, not right now"); assert.equal(say("atm", lex, { item: "p-2" }), "at the moment"); assert.equal(say("atm", lex), "A-T-M", "no item: the project's only"); assert.deepEqual(lexiconFor(lex, "p-2").map((x) => x.say), ["at the moment", "not right now", "loop", "A-T-M"]); assert.deepEqual(applyLexicon("atm atm", lex).applied, [{ word: "atm", say: "A-T-M" }], "applied once each"); assert.deepEqual(applyLexicon("atm", lex, { item: "p-2" }).applied, [{ word: "atm", say: "at the moment", item: "p-2" }]); }); test("a protected span is never replaced", () => { const text = "tbh tbh"; assert.equal(say(text, [{ word: "tbh", say: "T-B-H" }], { protect: [{ start: 0, end: 3 }] }), "tbh T-B-H"); assert.equal(lexiconHits(text, [{ word: "tbh", say: "x" }], { protect: [{ start: 2, end: 5 }] }).length, 0); }); test("spokenRead: the lexicon applied, recorded -- and the slur filter and the redaction WIN over it", () => { const e = { id: "p-1", post: "c/1" }; const slurs = (cfg) => resolveSlurs(cfg, LIST); // A lexicon entry for a slur's own word never fires: the filter's voice has it. const lex = [{ word: F, say: "friend" }, { word: "tbh", say: "T-B-H" }]; const text = `tbh a ${F}`; assert.deepEqual(spokenRead(text, e, { terms: [], slurs: slurs({ voice: "skip" }), say: lex }), { text: "T-B-H a", slurs: "skip", lexicon: [{ word: "tbh", say: "T-B-H" }] }); assert.deepEqual(spokenRead(text, e, { terms: [], slurs: slurs({ voice: "placeholder" }), say: lex }), { text: "T-B-H a f-slur", slurs: "placeholder", lexicon: [{ word: "tbh", say: "T-B-H" }] }); assert.deepEqual(spokenRead(text, e, { terms: [], slurs: slurs({}), say: lex }), { text: `T-B-H a ${F}`, slurs: "said", lexicon: [{ word: "tbh", say: "T-B-H" }] }); // Even with the filter off: a slur's word is the filter's, never the lexicon's. assert.equal(spokenRead(text, e, { terms: [], slurs: slurs({ on: false }), say: lex }).text, `T-B-H a ${F}`); // A spoken form that says a slur: the filter masks it too. assert.equal(spokenRead("lol", e, { terms: [], slurs: slurs({ voice: "skip" }), say: [{ word: "lol", say: `ha ${F}` }] }).text, "ha"); // A redacted word is dropped, never replaced -- and a spoken form that says a blanked name is dropped too. const red = { terms: [{ term: "Quentin" }], slurs: null, say: [{ word: "Quentin", say: "Q" }, { word: "QE", say: "Quentin Example" }] }; assert.equal(spokenRead("Quentin, hi", e, red).text, "hi"); assert.equal(spokenRead("QE said", e, red).text, "Example said"); // No lexicon: no record (the shape the takes had before it). assert.deepEqual(spokenRead("tbh", e, { terms: [], slurs: null }), { text: "tbh", slurs: null }); // An item's entry, by the entry's id. assert.equal(spokenRead("atm", { id: "p-2" }, { terms: [], say: [{ word: "atm", say: "A-T-M", item: "p-2" }] }).text, "A-T-M"); assert.equal(spokenRead("atm", { id: "p-3" }, { terms: [], say: [{ word: "atm", say: "A-T-M", item: "p-2" }] }).text, "atm"); }); test("staleLexicon: a take that says an entry the old way -- unless it applied it with the same spoken form", () => { const lex = [{ word: "tbh", say: "T-B-H" }, { word: "Pepe", say: "Pepe the frog" }]; const e = { id: "p-1" }; // A take from before the lexicon: its script says tbh. assert.deepEqual(staleLexicon(e, { file: "a.wav", script: "tbh it is" }, lex), [{ word: "tbh", say: "T-B-H" }]); // A take that applied it: not stale -- even when the spoken form holds the word. assert.deepEqual(staleLexicon(e, { file: "a.wav", script: "tbh it is Pepe", lexicon: [{ word: "tbh", say: "T-B-H" }, { word: "Pepe", say: "Pepe the frog" }] }, lex), []); // A thread read's script is what it said: "T-B-H" is not "tbh". assert.deepEqual(staleLexicon(e, { file: "a.wav", script: "T-B-H it is" }, lex), []); // Applied with another spoken form since changed: stale, with what it said. assert.deepEqual(staleLexicon(e, { file: "a.wav", script: "tbh", lexicon: [{ word: "tbh", say: "tee bee aitch" }] }, lex), [{ word: "tbh", say: "T-B-H", was: "tee bee aitch" }]); // A recording is a person's read: never stale. A take with no script reads its parts' words. assert.deepEqual(staleLexicon(e, { file: "a.wav", engine: "recorded", script: "tbh" }, lex), []); assert.deepEqual(staleLexicon(e, { file: "a.wav", parts: [{ text: "hi" }, { text: "tbh" }] }, lex).map((x) => x.word), ["tbh"]); }); test("validateLexicon: the shape, the limits, a duplicate; a take's record is checked by the build", () => { assert.deepEqual(validateLexicon([{ word: "tbh", say: "T-B-H" }, { word: "tbh", say: "x", item: "p-1" }, { word: "TBH", say: "y", case: true }]), []); assert.match(validateLexicon([{ word: "tbh", say: "a" }, { word: "TBH", say: "b" }]).join(), /twice/); assert.match(validateLexicon([{ word: "!!", say: "a" }]).join(), /letter or a digit/); assert.match(validateLexicon([{ word: "a", say: "" }]).join(), /what the voice says/); assert.match(validateLexicon([{ word: "a", say: "x" }]).join(), /Orpheus's tags/); assert.deepEqual(validateLexicon([{ word: "a", say: " a" }]), []); assert.match(validateLexicon([{ word: "a", say: "b", how: 1 }]).join(), /not a lexicon key/); assert.match(validateLexicon({}).join(), /must be a list/); assert.deepEqual(takeLexiconErrors([{ word: "tbh", say: "T-B-H" }], "t"), []); assert.match(takeLexiconErrors([{ word: "tbh" }], "t").join(), /entries it applied/); const e = { id: "p-1", type: "post", narration: { takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", seconds: 1, lexicon: [{ word: "tbh", say: "T-B-H" }], read: { kind: "thread", parents: "0" } }] } }; assert.deepEqual(validatePostNarration(e), []); e.narration.takes[0].read = { kind: "other" }; assert.match(validatePostNarration(e).join(), /read must be/); }); test("the voice map: a handle keyed without @ and case; who reads in what", () => { assert.equal(handleKey("@KirscheVerstahl"), "kirscheverstahl"); assert.deepEqual(validateVoiceMap({ kirscheverstahl: "tara", "*": "leo" }), []); assert.match(validateVoiceMap({ "@Kirsche": "tara" }).join(), /not a handle/); assert.match(validateVoiceMap({ a: "no voice!" }).join(), /stock voice/); const o = { map: { kirscheverstahl: "tara", movail2: "leah" }, self: "ObviousRises", narrator: "zac", engine: "orpheus" }; assert.equal(voiceForPiece({ handle: "KirscheVerstahl", role: "parent:0" }, o), "tara"); assert.equal(voiceForPiece({ handle: "someone", role: "parent:0" }, o), "leo", "everyone else: leo on Orpheus"); assert.equal(voiceForPiece({ handle: "someone", role: "quoted" }, { ...o, map: { ...o.map, "*": "dan" } }), "dan"); assert.equal(voiceForPiece({ handle: "obviousrises", role: "parent:1" }, o), "zac", "his own words up the thread: the narrator"); assert.equal(voiceForPiece({ handle: "ObviousRises", role: "post" }, o), "zac"); assert.equal(voiceForPiece({ handle: "ObviousRises", role: "post" }, { ...o, voice: "leo" }), "leo", "--voice: his post"); assert.equal(voiceForPiece({ handle: "KirscheVerstahl", role: "post" }, { ...o, self: "KirscheVerstahl" }), "tara", "her own item: the map names her"); assert.equal(voiceForPiece({ handle: "KirscheVerstahl", role: "parent:0" }, { ...o, overrides: { kirscheverstahl: "mia" } }), "mia", "--voice-for wins"); assert.equal(voiceForPiece({ handle: "x", role: "parent:0" }, { ...o, engine: "kokoro", narrator: "af_heart" }), "af_heart", "no context default for the engine: the narrator"); assert.deepEqual(voiceOverridesOf(["@KirscheVerstahl=tara", "a=leo,b=dan"]), { kirscheverstahl: "tara", a: "leo", b: "dan" }); assert.throws(() => voiceOverridesOf(["tara"]), /=/); });