// FRAGMENT QUOTES — a quote that stops mid-thought (release 28, Q2). // // A quote is a FRAGMENT when it has fewer than FRAGMENT_MIN_WORDS words, or // one of its parts (the runs convertSweep joins with an ellipsis) ends on a // function word — "I priced it as", "my lawyer told me not to". A part that // ends on sentence punctuation (. ! ? …) is a whole sentence whatever its last // word ("What are you looking at?"). Accepted collateral of the end-word rule: // a sentence that really ends on an object pronoun ("I told her") when its // transcript is unpunctuated. // // `fullSentenceFor` is what code offers instead: the smallest run of the // record's text, from the quote's first word, that ends at sentence // punctuation or at a cue boundary where the next cue starts a new sentence; // with unpunctuated captions, the end of the cue the quote ends in, then one // more cue while it is still a fragment. Capped at FULL_SENTENCE_MAX_WORDS // (or the quote's own length and a margin, when the quote is longer). A // result still under the minimum is widened back to the sentence's start. // The words are the cues' own, joined with single spaces, so the sentence // passes the strict check wherever the quote did. // // Pure, no imports but ./verify (also pure). import { STRICT_QUOTE_WINDOW_SECONDS, quoteFragments, strictNormalize } from "./verify"; export const FRAGMENT_MIN_WORDS = 4; // The words a quote may not end on (lowercase; compared normalised). export const FRAGMENT_END_WORDS = [ "as", "to", "the", "and", "of", "a", "an", "in", "on", "for", "with", "at", "by", "from", "that", "which", "but", "or", "if", "is", "was", "be", "i", "my", "your", "their", "our", "his", "her", ] as const; export const FULL_SENTENCE_MAX_WORDS = 30; const END_WORDS = new Set(FRAGMENT_END_WORDS); // A word that ends a sentence: its last letter is followed by . ! ? or … // (closing quotes and brackets allowed after). const SENTENCE_END_RE = /[.!?…]["'”’)\]]*$/; const wordsOf = (text: string) => strictNormalize(text).split(" ").filter(Boolean); export type FragmentFinding = { // Why it is a fragment: "under 4 words", or "ends on “as”". why: string; // The part (an ellipsis-joined run, or the whole quote) that is one. part: string; }; // Whether a quote is a fragment, and which part; null when it is not. export function fragmentOf(quote: string): FragmentFinding | null { const total = wordsOf(quote).length; if (total === 0) return null; const parts = quoteFragments(quote); if (total < FRAGMENT_MIN_WORDS) return { why: `under ${FRAGMENT_MIN_WORDS} words`, part: parts.at(-1) ?? quote.trim() }; for (const part of parts) { if (SENTENCE_END_RE.test(part.trim())) continue; const last = wordsOf(part).at(-1); if (last && END_WORDS.has(last)) return { why: `ends on “${last}”`, part }; } return null; } export const isFragmentQuote = (quote: string) => fragmentOf(quote) !== null; type TimedText = { start: number; end: number; text: string }; type Word = { raw: string; cue: number; norms: string[] }; // A cue's first word starts a new sentence when it is capitalised and is not // the pronoun I (or a contraction of it). const startsSentence = (raw: string) => /^["'“‘(]?[A-Z]/.test(raw) && !/^["'“‘(]?I(['’][a-z]+)?$/.test(raw); // The full sentence a quote (or one part of it) stops short of, from the cues // overlapping ±windowSeconds of `second`; null when the quote is not found // there verbatim, or no whole sentence fits the cap. export function fullSentenceFor( quote: string, cues: readonly TimedText[], second: number, opts: { windowSeconds?: number; maxWords?: number } = {}, ): string | null { const win = opts.windowSeconds ?? STRICT_QUOTE_WINDOW_SECONDS; const window = cues.filter((c) => c.end > second - win && c.start < second + win); const words: Word[] = []; window.forEach((c, k) => { for (const raw of c.text.split(/\s+/).filter(Boolean)) words.push({ raw, cue: k, norms: wordsOf(raw) }); }); const flat: { tok: string; w: number }[] = []; words.forEach((w, i) => w.norms.forEach((tok) => flat.push({ tok, w: i }))); const q = wordsOf(quote); if (!q.length) return null; // Every place the quote occurs; the one nearest the cited second wins. let best: { from: number; to: number; dist: number } | null = null; for (let i = 0; i + q.length <= flat.length; i++) { let ok = true; for (let k = 0; k < q.length && ok; k++) ok = flat[i + k]!.tok === q[k]; if (!ok) continue; const from = flat[i]!.w; const to = flat[i + q.length - 1]!.w; const dist = Math.abs(window[words[from]!.cue]!.start - second); if (!best || dist < best.dist) best = { from, to, dist }; } if (!best) return null; const cap = Math.max(opts.maxWords ?? FULL_SENTENCE_MAX_WORDS, q.length + 15); const text = (a: number, b: number) => words.slice(a, b + 1).map((w) => w.raw).join(" "); const span = (a: number, b: number) => wordsOf(text(a, b)).length; const lastOfCue = (cue: number) => { let j = -1; words.forEach((w, i) => { if (w.cue === cue) j = i; }); return j; }; let start = best.from; let end = -1; // 1. Forward to sentence punctuation, or to a cue boundary before a new sentence. for (let j = best.to; j < words.length && span(start, j) <= cap; j++) { if (SENTENCE_END_RE.test(words[j]!.raw)) { end = j; break; } const next = words[j + 1]; if (next && next.cue !== words[j]!.cue && startsSentence(next.raw)) { end = j; break; } } // 2. Unpunctuated: the end of the quote's last cue, then one more cue. if (end < 0) { end = lastOfCue(words[best.to]!.cue); if (fragmentOf(text(start, end)) && end + 1 < words.length) end = lastOfCue(words[end + 1]!.cue); if (span(start, end) > cap) return null; } // 3. Still too short: back to the sentence's start (the previous sentence // punctuation, else the start of the quote's first cue). if (wordsOf(text(start, end)).length < FRAGMENT_MIN_WORDS) { let i = start; while (i > 0 && !SENTENCE_END_RE.test(words[i - 1]!.raw) && words[i - 1]!.cue === words[start]!.cue) i--; while (i > 0 && !SENTENCE_END_RE.test(words[i - 1]!.raw) && span(i - 1, end) <= cap && !startsSentence(words[i]!.raw)) i--; start = i; } const out = text(start, end); return fragmentOf(out) ? null : out; } // The words a check hands back for a fragment: what it is, and the sentence // to quote instead when there is one — the repair hint. export function fragmentHint(f: FragmentFinding, sentence: string | null): string { return ( `a fragment (${f.why}): "${f.part.slice(0, 120)}" — quote the whole sentence` + (sentence ? `: "${sentence}"` : " or clause") ); }