// A QUOTE AGAINST THE TRANSCRIPT AS IT IS NOW — the word-level re-verify a // research board runs on a pinned quote (umtool R2, lib/research/boards.ts // verifyQuote) and a refresh runs on every quote its parent report cites // (umtool R4, draft/refresh). Pure: the caller reads the cues (a board through // the MCP engine, a refresh through the editor's corpus); this says what they // hold. // // wordsOf / holdsWords the comparison: case, punctuation and spacing // aside, whole words, in order // windowAround the cues within ±span s of the cite, the one // starting nearest the cite lit (R1's windowOf) // checkWordsInWindow ok (and where the words start now, when they // moved a second or more) or drifted (with the // words over the quote's own seconds now) // enOrigDifference the words of the `en` and the `en-orig` tracks // over the cite, when they differ — the served `en` // track can rewrite what was said export type WordCue = { start: number; end?: number; text: string }; export type WindowCue = { t: number; end?: number; text: string }; /** The words of a text, for comparing: lowercase, apostrophes kept, everything else a space. */ export function wordsOf(text: string | null | undefined): string { return String(text ?? "") .toLowerCase() .replace(/[‘’ʼ`]/g, "'") .replace(/[^\p{L}\p{N}' ]+/gu, " ") .replace(/'+/g, "'") .replace(/\s+/g, " ") .trim(); } /** Do `inside`'s words hold `quote`'s, in order and whole? */ export function holdsWords(inside: string, quote: string): boolean { const q = wordsOf(quote); if (!q) return false; return ` ${wordsOf(inside)} `.includes(` ${q} `); } /** Cue texts joined as one line. */ export const joinedCues = (cs: readonly { text: string }[]): string => cs.map((c) => c.text.replace(/\s+/g, " ").trim()).join(" "); /** The cues starting within ±span s of `t`, and which of them is the one starting nearest `t` (-1: none in the window). */ export function windowAround(all: readonly WordCue[], t: number, span = 30): { cues: WindowCue[]; lit: number } { let litAll = -1; let best = Infinity; for (let i = 0; i < all.length; i++) { const d = Math.abs(all[i]!.start - t); if (d < best) { best = d; litAll = i; } } const cues: WindowCue[] = []; let lit = -1; for (let i = 0; i < all.length; i++) { const c = all[i]!; if (c.start < t - span || c.start > t + span) continue; if (i === litAll) lit = cues.length; cues.push({ t: c.start, ...(c.end !== undefined ? { end: c.end } : {}), text: c.text }); } return { cues, lit }; } export type WordsVerdict = | { status: "ok"; nowAt?: number } // `now`: the words over the quote's own seconds now ("" when the window is empty). | { status: "drifted"; now: string; nowAt?: number; empty?: boolean }; /** The longest run of cues a quote is looked for in (a pin spans at most this many). */ export const MAX_QUOTE_CUES = 12; /** * The quote against the window around its cite: its words found → ok, with * where they start now when that is a second or more from `t`; else drifted, * with what the transcript says over the quote's seconds now. */ export function checkWordsInWindow(quote: string, cite: { t: number; end: number }, win: { cues: readonly WindowCue[]; lit: number }): WordsVerdict { const cues = win.cues; if (!cues.length) return { status: "drifted", now: "", empty: true }; if (holdsWords(joinedCues(cues), quote)) { // Where the words start now: the cue the shortest run holding them starts // with -- a run that still holds them without its first cue started too early. let nowAt: number | null = null; for (let i = 0; i < cues.length && nowAt === null; i++) { for (let k = i; k < Math.min(cues.length, i + MAX_QUOTE_CUES); k++) { if (!holdsWords(joinedCues(cues.slice(i, k + 1)), quote)) continue; if (k === i || !holdsWords(joinedCues(cues.slice(i + 1, k + 1)), quote)) nowAt = cues[i]!.t; break; } } return nowAt !== null && Math.abs(nowAt - cite.t) >= 1 ? { status: "ok", nowAt } : { status: "ok" }; } const lit = win.lit >= 0 ? win.lit : 0; const length = Math.max(0, cite.end - cite.t); const over = cues.slice(lit).filter((x, i) => i === 0 || x.t <= cues[lit]!.t + length - 0.25); return { status: "drifted", now: joinedCues(over).slice(0, 3000), nowAt: cues[lit]!.t }; } /** The `en` and `en-orig` words over a cite, when they differ (else null). */ export function enOrigDifference(en: readonly WordCue[], orig: readonly WordCue[], cite: { t: number; end: number }): { en: string; orig: string } | null { const over = (cues: readonly WordCue[]) => joinedCues(cues.filter((x) => (x.end ?? x.start + 1) > cite.t + 0.25 && x.start < Math.max(cite.end, cite.t + 1) - 0.25)); const a = over(en); const b = over(orig); if (wordsOf(a) === wordsOf(b)) return null; return { en: a, orig: b }; }