// Convert a generated sweep.md and check its quotes (release 27, S5; the // stopgap's convert_and_verify + strict_check, in process). // // The check a GENERATED quote must pass is the strict one (D3): every quoted // fragment verbatim (normalised) in the record's cues within ±90 s of its // cited second; a post's quote in the post's text. And it may not be a // FRAGMENT (release 28 Q2, lib/citations/fragment.ts): the problem then // carries the full sentence, the repair round's hint. An outside verifier (the // editor's verify-quotes over the corpus on disk, which also reads the en-orig // track) may add problems. A finding whose quote fails after the one repair // round is DROPPED, never repaired by code. import { sweepToReport } from "../../convertSweep"; import type { CuesOf, PostChannelOf } from "../../convertShared"; import type { Report } from "../../schema"; import { strictNormalize, strictQuoteCheck, strictTextCheck, quoteFragments } from "../../../citations/verify"; import { fragmentHint, fragmentOf, fullSentenceFor } from "../../../citations/fragment"; import { evidenceOf, findingsOf, parseSweep, quotesOf, renderSweep, LINK_RE, type SweepDoc } from "../sweepModel"; import { parseCitationHref } from "../../convertShared"; import { mmss } from "../reportOps"; import type { TaskDeps } from "./types"; export type QuoteProblem = { citation: string; channel: string; id: string; second?: number; quote: string; why: string; // The transcript ±45 s around the cited second, for a repair. near?: string; // A FRAGMENT (release 28 Q2): the words are there, but the quote stops // mid-thought. `sentence` is the full sentence to quote instead (the repair // hint), null when none fits. fragment?: { why: string; part: string; sentence: string | null }; }; export type ConvertOutcome = | { ok: true; report: Report; warnings: string[] } | { ok: false; problems: string[]; warnings: string[] }; export function titleOfSweep(md: string): string | null { const first = md.split("\n").find((l) => l.startsWith("# ")); return first ? first.slice(2).trim() || null : null; } export async function convertSweepText( md: string, o: { id: string; title?: string; cuesOf?: CuesOf; postChannelOf?: PostChannelOf }, ): Promise { const converted = await sweepToReport(md, { id: o.id, ...(o.title ? { title: o.title } : {}), ...(o.cuesOf ? { cuesOf: o.cuesOf } : {}), ...(o.postChannelOf ? { postChannelOf: o.postChannelOf } : {}), }); if (converted.problems.length) { return { ok: false, problems: converted.problems.map((p) => `${p.path || "(root)"}: ${p.message}`), warnings: converted.warnings, }; } return { ok: true, report: converted.report, warnings: converted.warnings }; } const NEAR_SECONDS = 45; // Every video, audio and post citation with a quote, strictly checked. export async function checkQuotes( report: Report, deps: Pick, ): Promise { const problems: QuoteProblem[] = []; for (const [cid, c] of Object.entries(report.citations ?? {})) { if (c.kind !== "video" && c.kind !== "audio" && c.kind !== "post") continue; const quote = (c as { quote?: string }).quote ?? ""; if (!quote.trim()) continue; if (c.kind === "post") { const text = deps.postTextOf ? await deps.postTextOf(c.channel, c.id) : null; if (text === null) { if (deps.postTextOf) problems.push({ citation: cid, channel: c.channel, id: c.id, quote, why: `no post ${c.channel}/${c.id} to check the quote against` }); continue; } const r = strictTextCheck(quote, text); if (!r.ok) { problems.push({ citation: cid, channel: c.channel, id: c.id, quote, why: `"${r.failed}" is not the post's words`, near: text }); continue; } const frag = fragmentOf(quote); if (frag) { const sentence = fullSentenceFor(frag.part, [{ start: 0, end: 1, text }], 0); problems.push({ citation: cid, channel: c.channel, id: c.id, quote, why: fragmentHint(frag, sentence), fragment: { ...frag, sentence } }); } continue; } const cues = await deps.cuesOf(c.channel, c.id); if (!cues || cues.length === 0) { problems.push({ citation: cid, channel: c.channel, id: c.id, second: c.start, quote, why: `no transcript cues for ${c.channel}/${c.id}` }); continue; } const r = strictQuoteCheck(quote, cues, c.start); if (!r.ok) { const near = cues .filter((x) => x.start >= c.start - NEAR_SECONDS && x.start <= c.start + NEAR_SECONDS) .map((x) => ` [${Math.floor(x.start)}] ${x.text}`) .join("\n"); problems.push({ citation: cid, channel: c.channel, id: c.id, second: c.start, quote, why: `not verbatim within 90 s of ${Math.floor(c.start)} s: "${r.failed}"`, near, }); continue; } const frag = fragmentOf(quote); if (frag) { const sentence = fullSentenceFor(frag.part, cues, c.start); problems.push({ citation: cid, channel: c.channel, id: c.id, second: c.start, quote, why: fragmentHint(frag, sentence), fragment: { ...frag, sentence } }); } } if (deps.verifyReport) { const seen = new Set(problems.map((p) => p.citation)); for (const p of await deps.verifyReport(report)) { // A warning (inexact, fragment) is not a problem: the strict and // fragment checks above are this run's own. if (seen.has(p.citation) || p.status === "ok" || p.status === "inexact" || p.status === "fragment") continue; const c = report.citations?.[p.citation] as { channel?: string; id?: string; start?: number; quote?: string } | undefined; problems.push({ citation: p.citation, channel: c?.channel ?? "", id: c?.id ?? "", ...(typeof c?.start === "number" ? { second: c.start } : {}), quote: c?.quote ?? "", why: `verify-quotes: ${p.status}${p.message ? ` — ${p.message}` : ""}`, }); } } return problems; } // The problems as the repair round's message lists them. export function describeProblems(problems: readonly QuoteProblem[], max = 40): string { return problems .slice(0, max) .map( (p) => `- ${p.why} — ${p.channel}/${p.id}${p.second !== undefined ? ` at ${mmss(p.second)}` : ""} (citation ${p.citation}): "${p.quote.slice(0, 200)}"` + (p.near ? `\n The transcript there:\n${p.near}` : ""), ) .join("\n"); } // The findings citing a failed quote, dropped: a finding goes when one of its // links cites the problem's recording and its quoted words hold one of the // failed quote's fragments. Answers the new markdown and what went: `dropped` // as lines, `items` as the finding's claim with the problem that took it (a // sweep's trace shows each dropped quote and why). export type DroppedFinding = { claim: string; problem: QuoteProblem }; export function dropFailedFindings(md: string, problems: readonly QuoteProblem[]): { md: string; dropped: string[]; items: DroppedFinding[] } { const doc: SweepDoc = parseSweep(md); const dropped: string[] = []; const items: DroppedFinding[] = []; for (const section of doc.sections) { for (const f of findingsOf(section)) { const links = [...evidenceOf(f).matchAll(new RegExp(LINK_RE.source, "g"))] .map((m) => /\]\((.*)\)$/.exec(m[0])?.[1] ?? "") .map((href) => parseCitationHref(href)) .filter((x) => x !== null); const quoted = strictNormalize(quotesOf(f).join(" ")); const hit = problems.find( (p) => links.some((l) => "channel" in l && l.channel === p.channel && l.id === p.id) && quoteFragments(p.quote).some((frag) => { const n = strictNormalize(frag); return n.length > 0 && quoted.includes(n); }), ); if (!hit) continue; section.body.splice(section.body.indexOf(f), 1); dropped.push(`${f.claim.slice(0, 80)} — ${hit.why}`); items.push({ claim: f.claim, problem: hit }); } } if (!dropped.length) return { md, dropped, items }; return { md: renderSweep(doc), dropped, items }; }