// Stitch per-page transcriptions (see providers.mjs) into one play file. // Split model output into { num, body }: the first "=N" line is the page number. export function splitPage(text) { const lines = text.replace(/\r/g, "").replace(/^```\w*\n|\n```$/g, "").split("\n"); let num = "?"; const first = lines.findIndex((l) => l.trim() !== ""); if (first >= 0 && /^=/.test(lines[first].trim())) { num = lines[first].trim().slice(1).replace(/^[-–\s]+|[-–.\s]+$/g, "") || "?"; lines.splice(0, first + 1); } // The page structure is ours to set; drop any further page markers the model wrote. const body = lines.filter((l) => !/^[=!]/.test(l)).join("\n").trim(); return { num, body }; } const nameKey = (n) => n.normalize("NFD").replace(/\p{M}/gu, "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim(); // Different pages may spell a name differently (LÍDA / Lída / Lida:). Map every // variant to one spelling: an ALL-CAPS name counts as its Title-case form, and a // spelling with diacritics wins over one without (OCR drops accents far more // often than it invents them), then the most frequent one. const titleCase = (s) => s.toLocaleLowerCase("cs").replace(/(^|[\s-])(\p{L})/gu, (m, a, b) => a + b.toLocaleUpperCase("cs")); const accents = (s) => s.normalize("NFD").match(/\p{M}/gu)?.length || 0; export function normalizeSpeakers(bodies) { const variants = new Map(); // key -> Map(spelling -> count) const fromCaps = new Set(); // spellings we made up by Title-casing const clean = (raw) => { const n = raw.trim().replace(/[:.]+$/, "").trim(); if (n.length > 1 && n === n.toLocaleUpperCase("cs") && n !== n.toLocaleLowerCase("cs")) { fromCaps.add(titleCase(n)); return titleCase(n); } return n; }; for (const body of bodies) for (const line of body.split("\n")) if (line.startsWith("@")) { const name = clean(line.slice(1)); const key = nameKey(name); if (!key) continue; if (!variants.has(key)) variants.set(key, new Map()); const m = variants.get(key); m.set(name, (m.get(name) || 0) + 1); } const best = new Map(); for (const [key, m] of variants) { const asWritten = (s) => (fromCaps.has(s) ? 0 : 1); const rank = [...m.keys()].sort((a, b) => accents(b) - accents(a) || m.get(b) - m.get(a) || asWritten(b) - asWritten(a)); best.set(key, rank[0]); } return bodies.map((body) => body.split("\n").map((line) => { if (!line.startsWith("@")) return line; const key = nameKey(clean(line.slice(1))); return best.has(key) ? `@${best.get(key)}` : line; }).join("\n")); } // Build the play file from per-page results [{ num, body } | { error }], in upload order. // Pages without a readable number get the next number in sequence; numeric gaps // between pages become "missing page" markers. export function assemble(pages) { const bodies = normalizeSpeakers(pages.map((p) => p.body || "")); const out = []; let prev = null; pages.forEach((p, i) => { let num = p.num && p.num !== "?" ? p.num : null; if (!num && prev !== null) num = String(prev + 1); if (!num) num = String(i + 1); const n = /^\d+$/.test(num) ? Number(num) : null; if (n !== null && prev !== null && n > prev + 1) out.push(n === prev + 2 ? `!${prev + 1}` : `!${prev + 1}-${n - 1}`); if (n !== null) prev = n; out.push(`=${num}`); if (p.error) out.push(`>(Stranu se nepodařilo přepsat: ${p.error})`); else out.push(bodies[i]); }); return out.join("\n") + "\n"; }