diff --git a/app/src/Editor.tsx b/app/src/Editor.tsx index a6ab4f6..e2ca3a4 100644 --- a/app/src/Editor.tsx +++ b/app/src/Editor.tsx @@ -14,6 +14,7 @@ import { reassignSentenceSpeaker, groupParagraphs, replaceAll, + countReplacements, } from "@core/editor"; import type { EditorState, EditorSentence } from "@core/editor"; import { UNKNOWN_SPEAKER } from "@core/types"; @@ -118,13 +119,14 @@ export default function Editor({ ); const starts = useMemo(() => flatWords(state).map((w) => w.start), [state]); - // Dry-run replaceAll for the live match count; the real replace runs on "Replace all". + // Live match count without building the replaced state; the real replace runs on + // "Replace all". const findOpts = useMemo( () => ({ caseSensitive: findCase, wholeWord: findWhole }), [findCase, findWhole], ); const findCount = useMemo( - () => replaceAll(state, findText, replaceText, findOpts).count, + () => countReplacements(state, findText, replaceText, findOpts), [state, findText, replaceText, findOpts], ); diff --git a/core/src/editor.ts b/core/src/editor.ts index 94345d4..c8969a5 100644 --- a/core/src/editor.ts +++ b/core/src/editor.ts @@ -79,6 +79,25 @@ function tokenize(text: string): string[] { .filter(Boolean); } +/** Spread `span` seconds from `start` across `tokens` by character length. */ +function spread( + start: number, + span: number, + tokens: string[], + speaker: SpeakerId, +): EditorWord[] { + const total = tokens.join("").length || 1; + let acc = 0; + const rebuilt: EditorWord[] = []; + for (const tok of tokens) { + const st = start + span * (acc / total); + acc += tok.length; + const en = start + span * (acc / total); + rebuilt.push({ text: tok, start: st, end: en, speaker }); + } + return rebuilt; +} + /** * Spread `[spanStart, spanEnd]` across `tokens` by character length, all assigned * `speaker`. A degenerate span (end <= start) becomes 0.3s per token so timings stay @@ -94,43 +113,115 @@ function respread( spanEnd - spanStart > 0 ? spanEnd - spanStart : Math.max(0.3 * tokens.length, 0.3); - const total = tokens.join("").length || 1; - let acc = 0; - const rebuilt: EditorWord[] = []; - for (const tok of tokens) { - const st = spanStart + span * (acc / total); - acc += tok.length; - const en = spanStart + span * (acc / total); - rebuilt.push({ text: tok, start: st, end: en, speaker }); + return spread(spanStart, span, tokens, speaker); +} + +/** + * `[oldIndex, newIndex]` pairs for the words an edit kept: a longest common + * subsequence of the two word lists, compared by exact text. + */ +function keptPairs(oldTexts: string[], newTexts: string[]): Array<[number, number]> { + // Match the shared prefix and suffix first. Edits usually touch a few words in the + // middle, so this keeps the table small; and where a repeated word could match in + // more than one place, it keeps the words at both ends matched. + let head = 0; + while ( + head < oldTexts.length && + head < newTexts.length && + oldTexts[head] === newTexts[head] + ) { + head++; } - return rebuilt; + let tail = 0; + while ( + tail < oldTexts.length - head && + tail < newTexts.length - head && + oldTexts[oldTexts.length - 1 - tail] === newTexts[newTexts.length - 1 - tail] + ) { + tail++; + } + + const a = oldTexts.slice(head, oldTexts.length - tail); + const b = newTexts.slice(head, newTexts.length - tail); + // lcs[i][j]: length of the longest common subsequence of a[i..] and b[j..]. + const lcs = Array.from({ length: a.length + 1 }, () => new Int32Array(b.length + 1)); + for (let i = a.length - 1; i >= 0; i--) { + for (let j = b.length - 1; j >= 0; j--) { + lcs[i]![j] = + a[i] === b[j] ? lcs[i + 1]![j + 1]! + 1 : Math.max(lcs[i + 1]![j]!, lcs[i]![j + 1]!); + } + } + + const pairs: Array<[number, number]> = []; + for (let k = 0; k < head; k++) pairs.push([k, k]); + let i = 0; + let j = 0; + while (i < a.length && j < b.length) { + if (a[i] === b[j]) { + pairs.push([head + i, head + j]); + i++; + j++; + } else if (lcs[i + 1]![j]! >= lcs[i]![j + 1]!) { + i++; + } else { + j++; + } + } + for (let k = tail; k > 0; k--) { + pairs.push([oldTexts.length - k, newTexts.length - k]); + } + return pairs; +} + +/** + * The words for edited text `tokens` that replaces `old`, all given the speaker of + * `old`'s first word. Words the edit kept stay as they were: their timing and + * `original`. A run of new words that replaces old words takes those words' time; a + * run inserted between kept words takes the gap between them (no duration if there + * is none). With nothing kept, the new words spread across `old`'s whole span. + */ +function retime(old: EditorWord[], tokens: string[]): EditorWord[] { + const spanStart = old[0]?.start ?? 0; + const spanEnd = old.at(-1)?.end ?? 0; + const speaker = old[0]?.speaker ?? UNKNOWN_SPEAKER; + const pairs = keptPairs( + old.map((w) => w.text), + tokens, + ); + if (pairs.length === 0) return respread(spanStart, spanEnd, tokens, speaker); + + // A sentinel pair past both ends flushes the run after the last kept word. + pairs.push([old.length, tokens.length]); + const words: EditorWord[] = []; + let oldAt = 0; + let newAt = 0; + for (const [keptOld, keptNew] of pairs) { + if (keptNew > newAt) { + // The replaced words' own time, else the gap between the kept neighbours. + const replaced = keptOld > oldAt; + const from = replaced ? old[oldAt]!.start : (old[oldAt - 1]?.end ?? spanStart); + const to = replaced ? old[keptOld - 1]!.end : (old[keptOld]?.start ?? spanEnd); + const added = tokens.slice(newAt, keptNew); + words.push(...spread(from, Math.max(to - from, 0), added, speaker)); + } + if (keptOld < old.length) words.push({ ...old[keptOld]!, speaker }); + oldAt = keptOld + 1; + newAt = keptNew + 1; + } + return words; } -/** Replace a sentence's text, keeping its original time span. Blank text deletes the sentence. */ +/** + * Replace a sentence's text: `editParagraph` on that one sentence. Words the edit + * didn't touch keep their timing and `original` (see `retime`). Blank text deletes + * the sentence; an unknown id returns the state unchanged. + */ export function editSentence( state: EditorState, sentenceId: string, newText: string, ): EditorState { - const next = cloneState(state); - const idx = next.sentences.findIndex((s) => s.id === sentenceId); - if (idx === -1) return next; - - const sentence = next.sentences[idx]!; - const orig = sentence.words; - const s = orig.length > 0 ? orig[0]!.start : 0; - const e = orig.length > 0 ? orig[orig.length - 1]!.end : 0; - const speaker = orig.length > 0 ? orig[0]!.speaker : UNKNOWN_SPEAKER; - - const toks = tokenize(newText); - - if (toks.length === 0) { - next.sentences.splice(idx, 1); - return next; - } - - sentence.words = respread(s, e, toks, speaker); - return next; + return editParagraph(state, [sentenceId], newText); } /** Indexes of `sentenceIds` if they name consecutive sentences in document order, else null. */ @@ -152,11 +243,11 @@ function contiguousRun( } /** - * Paragraph-scope edit. Re-tokenize `newText` and re-spread it across the run's ANCHOR - * span — the first listed sentence's first-word `start` to the last listed sentence's - * last-word `end` — then collapse the whole contiguous run to ONE sentence whose id is - * the FIRST sentence's id (so highlight/edit ids stay stable) and whose speaker is the - * first word's speaker. Blank/whitespace `newText` deletes the entire run. + * Paragraph-scope edit. Re-tokenize `newText` and time it against the run's words + * (see `retime`), so untouched words keep their timing and `original`. The whole + * contiguous run collapses to ONE sentence whose id is the FIRST sentence's id (so + * highlight/edit ids stay stable) and whose speaker is the first word's speaker. + * Blank/whitespace `newText` deletes the entire run. * * Non-contiguous, unknown or empty ids return the state unchanged. */ @@ -179,27 +270,12 @@ export function editParagraph( return next; } - let spanStart = 0; - let spanEnd = 0; - let speaker: SpeakerId = UNKNOWN_SPEAKER; - for (const s of runSentences) { - if (s.words.length > 0) { - spanStart = s.words[0]!.start; - speaker = s.words[0]!.speaker; - break; - } - } - for (let i = runSentences.length - 1; i >= 0; i--) { - const w = runSentences[i]!.words; - if (w.length > 0) { - spanEnd = w[w.length - 1]!.end; - break; - } - } - const collapsed: EditorSentence = { id: runSentences[0]!.id, - words: respread(spanStart, spanEnd, toks, speaker), + words: retime( + runSentences.flatMap((s) => s.words), + toks, + ), }; next.sentences.splice(startIdx, run.length, collapsed); return next; @@ -218,18 +294,16 @@ function escapeRegExp(s: string): string { } /** - * Literal, per-sentence find/replace (a multi-word `find` matches across word gaps). - * `replace` is inserted verbatim (no `$`-substitution). A changed sentence keeps its - * time span; a replacement that would empty a sentence is skipped. Empty `find` is a - * no-op. Returns the new state and the number of matches replaced. + * The sentences a replace-all changes: each one's index, its number of matches and + * its words after replacing. A replacement that would empty a sentence is skipped. */ -export function replaceAll( - state: EditorState, +function replacements( + sentences: EditorSentence[], find: string, replace: string, - opts: ReplaceOptions = {}, -): { state: EditorState; count: number } { - if (find === "") return { state, count: 0 }; + opts: ReplaceOptions, +): Array<{ index: number; matches: number; toks: string[] }> { + if (find === "") return []; const escaped = escapeRegExp(find); const pattern = opts.wholeWord ? `\\b${escaped}\\b` : escaped; @@ -237,34 +311,62 @@ export function replaceAll( const regex = new RegExp(pattern, flags); const replacer = (): string => replace; - const next = cloneState(state); - let count = 0; - - for (const sentence of next.sentences) { - const orig = sentence.words; - const joined = orig.map((w) => w.text).join(" "); + const changed: Array<{ index: number; matches: number; toks: string[] }> = []; + sentences.forEach((sentence, index) => { + const joined = sentence.words.map((w) => w.text).join(" "); regex.lastIndex = 0; let matches = 0; while (regex.exec(joined) !== null) { matches++; } - if (matches === 0) continue; + if (matches === 0) return; + + const toks = tokenize(joined.replace(regex, replacer)); + if (toks.length === 0) return; // no-delete rule + changed.push({ index, matches, toks }); + }); + return changed; +} - const newText = joined.replace(regex, replacer); - const toks = tokenize(newText); - if (toks.length === 0) continue; // no-delete rule +/** + * Literal, per-sentence find/replace (a multi-word `find` matches across word gaps). + * `replace` is inserted verbatim (no `$`-substitution). In a changed sentence only the + * replaced words are re-timed (see `retime`); a replacement that would empty a sentence + * is skipped. Empty `find` is a no-op. Returns the new state and the number of matches + * replaced. + */ +export function replaceAll( + state: EditorState, + find: string, + replace: string, + opts: ReplaceOptions = {}, +): { state: EditorState; count: number } { + if (find === "") return { state, count: 0 }; - const s = orig.length > 0 ? orig[0]!.start : 0; - const e = orig.length > 0 ? orig[orig.length - 1]!.end : 0; - const speaker = orig.length > 0 ? orig[0]!.speaker : UNKNOWN_SPEAKER; - sentence.words = respread(s, e, toks, speaker); + const next = cloneState(state); + let count = 0; + for (const { index, matches, toks } of replacements(state.sentences, find, replace, opts)) { + const sentence = next.sentences[index]!; + sentence.words = retime(sentence.words, toks); count += matches; } - return { state: next, count }; } +/** The `count` that `replaceAll` would return, without building the new state. */ +export function countReplacements( + state: EditorState, + find: string, + replace: string, + opts: ReplaceOptions = {}, +): number { + return replacements(state.sentences, find, replace, opts).reduce( + (sum, r) => sum + r.matches, + 0, + ); +} + /** * Concatenate a CONTIGUOUS run of sentences into ONE sentence (id = the first sentence's * id), preserving every word's timing in document order. No re-spread. Used to join diff --git a/core/test/edit-keeps-words.test.ts b/core/test/edit-keeps-words.test.ts new file mode 100644 index 0000000..ebac49d --- /dev/null +++ b/core/test/edit-keeps-words.test.ts @@ -0,0 +1,86 @@ +// Issue #28: an edit re-times only the words it changes; untouched words keep their +// native timing and the record of what the model wrote. +import { describe, it, expect } from "vitest"; + +import { loadTranscript, editSentence, replaceAll } from "../src/editor.js"; +import type { Transcript } from "../src/types.js"; + +// "I met Shivaun on Tuesday." where the vocabulary file turned "Siobhan" into +// "Shivaun", and a long pause comes before "Tuesday.". +function makeTranscript(): Transcript { + return { + segments: [ + { + start: 0.0, + end: 4.6, + text: "I met Shivaun on Tuesday.", + speaker: "SPEAKER_00", + words: [ + { word: "I", start: 0.0, end: 0.2, speaker: "SPEAKER_00" }, + { word: "met", start: 0.3, end: 0.6, speaker: "SPEAKER_00" }, + { + word: "Shivaun", + start: 0.7, + end: 1.4, + speaker: "SPEAKER_00", + original: "Siobhan", + }, + { word: "on", start: 1.5, end: 1.6, speaker: "SPEAKER_00" }, + { word: "Tuesday.", start: 4.0, end: 4.6, speaker: "SPEAKER_00" }, + ], + }, + ], + }; +} + +const I = { text: "I", start: 0.0, end: 0.2, speaker: "SPEAKER_00" }; +const MET = { text: "met", start: 0.3, end: 0.6, speaker: "SPEAKER_00" }; +const SHIVAUN = { + text: "Shivaun", + start: 0.7, + end: 1.4, + speaker: "SPEAKER_00", + original: "Siobhan", +}; +const ON = { text: "on", start: 1.5, end: 1.6, speaker: "SPEAKER_00" }; +const TUESDAY = { text: "Tuesday.", start: 4.0, end: 4.6, speaker: "SPEAKER_00" }; + +describe("edits keep untouched words", () => { + it("Find & Replace of one word leaves every other word's timing and original", () => { + const state = loadTranscript(makeTranscript()); + const { state: next, count } = replaceAll(state, "Tuesday", "Wednesday"); + + expect(count).toBe(1); + const words = next.sentences[0]!.words; + expect(words.map((w) => w.text)).toEqual(["I", "met", "Shivaun", "on", "Wednesday."]); + expect(words.slice(0, 4)).toEqual([I, MET, SHIVAUN, ON]); + }); + + it("editSentence that changes one word re-times only that word, within its neighbours' gap", () => { + const state = loadTranscript(makeTranscript()); + const sid = state.sentences[0]!.id; + const words = editSentence(state, sid, "I met Siobhán on Tuesday.").sentences[0]!.words; + + expect(words.map((w) => w.text)).toEqual(["I", "met", "Siobhán", "on", "Tuesday."]); + expect([words[0], words[1], words[3], words[4]]).toEqual([I, MET, ON, TUESDAY]); + // "met" ends at 0.6 and "on" starts at 1.5. + expect(words[2]!.start).toBeGreaterThanOrEqual(0.6); + expect(words[2]!.end).toBeLessThanOrEqual(1.5); + expect(words[2]!.end).toBeGreaterThanOrEqual(words[2]!.start); + }); + + it("an edit that inserts a word places it between its neighbours' times", () => { + const state = loadTranscript(makeTranscript()); + const sid = state.sentences[0]!.id; + const words = editSentence(state, sid, "I met Shivaun again on Tuesday.").sentences[0]! + .words; + + expect(words.map((w) => w.text)).toEqual(["I", "met", "Shivaun", "again", "on", "Tuesday."]); + expect(words[2]).toEqual(SHIVAUN); + expect(words[4]).toEqual(ON); + // "Shivaun" ends at 1.4 and "on" starts at 1.5. + expect(words[3]!.start).toBeGreaterThanOrEqual(1.4); + expect(words[3]!.end).toBeLessThanOrEqual(1.5); + expect(words[3]!.end).toBeGreaterThanOrEqual(words[3]!.start); + }); +});