Sort pages by their number, drop repeated photos, mark uncertain words

The model now wraps words it isn't sure of in {word} or {word|other}; the
reader shows the first reading. Pages are ordered by the number on each
photo and a page photographed twice is kept once (fewer uncertain words,
then more text). meta.photos records which photo goes with which page.
New POST /images/:n/transcribe re-reads a single photo for the editor.
Seeded plays get an img/ folder on their first photo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
agent 2026-11-04 16:15:27 +00:00
parent 3f2cf95f1d
commit ab842a1d73
8 changed files with 187 additions and 43 deletions

View file

@ -1,5 +1,11 @@
// Stitch per-page transcriptions (see providers.mjs) into one play file.
// Uncertain readings are marked "{word}" or "{word|other reading}". Older
// transcriptions used "word[?]"; convert those so the editor finds them too.
export function migrateUncertain(text) {
return text.replace(/([\p{L}\p{N}'’-]+)([.,;:!?…]*) ?\[\?\]/gu, "{$1}$2").replace(/ ?\[\?\]/g, "");
}
// 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");
@ -11,7 +17,7 @@ export function splitPage(text) {
}
// 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 };
return { num, body: migrateUncertain(body) };
}
const nameKey = (n) => n.normalize("NFD").replace(/\p{M}/gu, "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim();
@ -27,7 +33,8 @@ 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();
// an uncertain name "{Lída}" counts as "Lída"
const n = raw.replace(/\{([^{}|]*)(\|[^{}]*)?\}/g, "$1").trim().replace(/[:.]+$/, "").trim();
if (n.length > 1 && n === n.toLocaleUpperCase("cs") && n !== n.toLocaleLowerCase("cs")) {
fromCaps.add(titleCase(n));
return titleCase(n);
@ -57,23 +64,66 @@ export function normalizeSpeakers(bodies) {
}).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 || ""));
export const countUncertain = (body) => (body.match(/\{[^{}\n]*\}/g) || []).length;
const pageInt = (num) => (/^\d+/.test(num || "") ? parseInt(num, 10) : null);
// Build the play from per-photo results [{ num, body } | { error } | null], in upload order.
//
// Pages are sorted by the number the model read off each photo, so upload order
// doesn't matter. A photo without a readable number stays right after the photo
// uploaded before it. Two photos of the same page (an accidental re-upload or a
// retake) keep only the better transcription: fewer uncertain words, then more text.
// Numeric gaps become "missing page" markers.
//
// Returns { text, photos, duplicates }:
// photos[i] the photo (1-based) shown with the i-th page marker of text, or null
// duplicates [{ image, page, kept, body }] photos left out as repeats of a page
export function assemble(results) {
const bodies = normalizeSpeakers(results.map((r) => r?.body || ""));
let entries = results.map((r, i) => ({ image: i + 1, num: r?.error ? null : r?.num, n: r?.error ? null : pageInt(r?.num), body: bodies[i], error: r?.error }));
// sort key: the page number; unnumbered photos follow their upload predecessor
let anchor = 0;
for (const e of entries) {
if (e.n !== null) anchor = e.n;
e.key = e.n !== null ? e.n : anchor + 0.5;
}
entries.sort((a, b) => a.key - b.key || (a.n === null) - (b.n === null) || a.image - b.image);
// drop repeats of the same page
const duplicates = [];
const byNum = new Map();
for (const e of entries) {
if (e.n === null) continue;
const kept = byNum.get(e.num);
if (!kept) { byNum.set(e.num, e); continue; }
const better = countUncertain(e.body) - countUncertain(kept.body) || kept.body.length - e.body.length;
const [keep, drop] = better < 0 ? [e, kept] : [kept, e];
byNum.set(e.num, keep);
drop.dropped = true;
duplicates.push({ image: drop.image, page: e.num, kept: keep.image, body: drop.body });
}
for (const d of duplicates) d.kept = byNum.get(d.page).image; // a later, better copy may have won
entries = entries.filter((e) => !e.dropped);
const taken = new Set(entries.filter((e) => e.n !== null).map((e) => e.n));
const out = [];
const photos = [];
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}`);
for (const e of entries) {
let num = e.num;
let n = e.n;
// guess the number of an unnumbered page if the next number is free
if (n === null && prev !== null && !taken.has(prev + 1)) { n = prev + 1; num = String(n); taken.add(n); }
if (n === null) num = "?";
if (n !== null && prev !== null && n > prev + 1) {
out.push(n === prev + 2 ? `!${prev + 1}` : `!${prev + 1}-${n - 1}`);
photos.push(null);
}
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";
photos.push(e.image);
out.push(e.error ? `>(Stranu se nepodařilo přepsat: ${e.error})` : e.body);
}
return { text: out.filter((l) => l !== "").join("\n") + "\n", photos, duplicates };
}