// What the clip says: its words with times, in phrases, numbered for the plan. export interface Word { text: string; start: number; end: number; } const PAUSE_SECONDS = 0.6; const PHRASE_WORDS = 14; export interface Phrase { start: number; end: number; count: number; text: string; } export function phrases(words: Word[]): Phrase[] { const out: Phrase[] = []; let held: Word[] = []; const close = () => { const first = held[0]; const last = held.at(-1); if (first && last) out.push({ start: first.start, end: last.end, count: held.length, text: held.map((w) => w.text).join(" "), }); held = []; }; for (const word of words) { const last = held.at(-1); if (last && (word.start - last.end >= PAUSE_SECONDS || held.length >= PHRASE_WORDS)) close(); held.push(word); if (/[.?!。?!]$/.test(word.text.trimEnd())) close(); } close(); return out; } /** The transcript in phrases, each word with its number. */ export function numbered(words: Word[], parts: Phrase[]): string { let at = 0; return parts .map((p) => { const row = words.slice(at, at + p.count).map((w, k) => `[${at + k}]${w.text}`); at += p.count; return `${p.start.toFixed(1)}-${p.end.toFixed(1)} ${row.join(" ")}`; }) .join("\n"); } /** A word as it is shown: as said, without the comma after it. */ export const shown = (text: string): string => text.trim().replace(/[,;:—-]+$/, "") || text.trim();