// THE SENTENCE AS THE UNIT OF AN EDGE (the phone walk's edge strip). // // The operator's standing complaint is cuts that land mid-sentence, and on a // phone a 10 px handle on a 300 px waveform cannot fix that. So an edge moves // by SENTENCE: `‹ sentence` / `sentence ›` step it to the previous or next // boundary, and a drag snaps to the nearest one within SNAP_RADIUS. // // THE RULE IS THE PRE-CUT'S (the job dir's precut.py, which re-cut the Huck // walk's unruled clips), ported rather than re-imagined -- its one addition is // the curly closing quotes a transcript sometimes carries -- so the // strip and the pass that already re-cut a whole cut agree on where a sentence // ends: // // a cue ENDS a sentence when its text ends in . ? ! (closing quotes or // brackets allowed after it), or the next cue starts // SENTENCE_GAP seconds or more after it ends; // a cue STARTS a sentence when it is the first, or the cue before it ends one // by the same two tests. // // A start edge lands on a sentence START (a cue's start), an end edge on a // sentence END (a cue's end). On a contiguous transcript the two are the same // second; across a pause they are not, and an end placed at the next start // would carry the silence into the clip. // // Pure: no DOM, no React. Absolute source seconds throughout, like the bench. import { endsSentence as textEndsSentence } from "umtool-report-to-video/pauses"; /** A pause this long between two cues is a sentence boundary, punctuation or not. */ export const SENTENCE_GAP = 1.5; /** A dragged edge snaps to a boundary this close (seconds). */ export const SNAP_RADIUS = 0.6; /** Two boundaries closer than this are one (a cue's end and the next one's start). */ const SAME = 0.05; /** Does this text close a sentence by its punctuation? (report-to-video/pauses.mjs endsSentence: one rule.) */ export function endsWithTerminal(text) { return textEndsSentence(text); } const fin = (v) => typeof v === "number" && Number.isFinite(v); /** * @typedef {{ start: number, end: number, text: string }} Cue * @param {Cue | undefined} c * @param {Cue | undefined | null} next */ export function endsSentence(c, next) { if (!c) return false; return endsWithTerminal(c.text) || (!!next && next.start - c.end >= SENTENCE_GAP); } /** * @param {Cue | undefined} c * @param {Cue | undefined | null} prev */ export function startsSentence(c, prev) { if (!c) return false; return !prev || endsWithTerminal(prev.text) || c.start - prev.end >= SENTENCE_GAP; } /** * Every sentence boundary in a run of cues, as source seconds, ascending. * * `starts` is where a start edge may land, `ends` where an end edge may; * `all` is both, merged where they coincide -- what the strip draws as ticks. * * @param {Cue[]} cues * @returns {{ starts: number[], ends: number[], all: number[] }} */ export function sentenceBoundaries(cues) { const cs = (cues ?? []).filter((c) => c && fin(c.start) && fin(c.end)).slice().sort((a, b) => a.start - b.start); const starts = []; const ends = []; for (let i = 0; i < cs.length; i += 1) { if (startsSentence(cs[i], cs[i - 1] ?? null)) starts.push(cs[i].start); if (endsSentence(cs[i], cs[i + 1] ?? null)) ends.push(cs[i].end); } const all = []; for (const t of [...starts, ...ends].sort((a, b) => a - b)) { if (!all.length || t - all[all.length - 1] > SAME) all.push(t); } return { starts: dedupe(starts), ends: dedupe(ends), all }; } function dedupe(list) { const out = []; for (const t of list) if (!out.length || t - out[out.length - 1] > SAME) out.push(t); return out; } /** * The next boundary strictly past `t` in direction `dir` (+1 later, -1 * earlier), or null when there is none. "Strictly": an edge already ON a * boundary steps to the next one, not to itself. * * @param {number[]} list ascending * @param {number} t * @param {1 | -1} dir */ export function stepBoundary(list, t, dir) { if (!fin(t)) return null; if (dir > 0) return list.find((b) => b > t + SAME) ?? null; for (let i = list.length - 1; i >= 0; i -= 1) if (list[i] < t - SAME) return list[i]; return null; } /** * Snap `t` to the nearest boundary within `radius`. Returns the second it * lands on and which boundary that is (null when nothing is close enough, and * `t` stands). * * @param {number[]} list * @param {number} t * @param {number} [radius] * @returns {{ t: number, boundary: number | null }} */ export function snapToBoundary(list, t, radius = SNAP_RADIUS) { let best = null; for (const b of list) if (Math.abs(b - t) <= radius && (best === null || Math.abs(b - t) < Math.abs(best - t))) best = b; return best === null ? { t, boundary: null } : { t: best, boundary: best }; } /** * The words either side of an edge, for the line under the strip: the end of * what plays before an end edge (`…can live on no matter what.|`), the start * of what plays after a start edge (`|And this one runs on…`). The cue the * edge is in, or the nearest one on the playing side. * * @param {Cue[]} cues * @param {number} t * @param {"start" | "end"} edge * @param {number} [words] how many words to show */ export function wordsAtEdge(cues, t, edge, words = 7) { const cs = (cues ?? []).filter((c) => c && fin(c.start) && fin(c.end)).slice().sort((a, b) => a.start - b.start); if (!cs.length || !fin(t)) return ""; if (edge === "end") { // The last cue that starts before the edge: what you hear last. const c = [...cs].reverse().find((x) => x.start < t - SAME / 2); if (!c) return ""; const w = String(c.text).trim().split(/\s+/); return `${w.length > words ? "…" : ""}${w.slice(-words).join(" ")}|`; } // The first cue that ends after the edge: what you hear first. const c = cs.find((x) => x.end > t + SAME / 2); if (!c) return ""; const w = String(c.text).trim().split(/\s+/); return `|${w.slice(0, words).join(" ")}${w.length > words ? "…" : ""}`; }