// quote-in-audio.mjs (lane one-clock): numbers however written, fragments in // order, and the offsets the rev9-a3 pass measured by ear and by hand — from // the parakeet words of the windows that were fetched on 6 Nov and the cues // they were cut by (fixtures/rev9-a3-rumble-words.json). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; import { cueWords, findQuote, foldNumbers, quoteOffset, quoteStatus, quoteTokens, rawTokens, sameToken, } from "./quote-in-audio.mjs"; const FIX = JSON.parse( readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures", "rev9-a3-rumble-words.json"), "utf8"), ); test("numbers are one token however they are written", () => { assert.deepEqual(quoteTokens("thirty eight point five pounds"), ["38.5", "pounds"]); assert.deepEqual(quoteTokens("I'm down to 310.5"), ["im", "down", "to", "310.5"]); assert.deepEqual(quoteTokens("three hundred ten point five"), ["310.5"]); assert.deepEqual(quoteTokens("a hundred and ten"), ["110"]); assert.deepEqual(quoteTokens("lose about one hundred pounds"), ["lose", "about", "100", "pounds"]); assert.deepEqual(quoteTokens("1,000 [sic] people, 17%"), ["1000", "people", "17", "percent"]); assert.deepEqual(quoteTokens("fifty point one pounds in eleven weeks"), ["50.1", "pounds", "in", "11", "weeks"]); // "twenty twenty one" is two numbers, not 41. assert.deepEqual(quoteTokens("twenty twenty one"), ["20", "21"]); assert.deepEqual(foldNumbers(rawTokens("down twenty now")), [ { t: "down", from: 0, to: 0 }, { t: "20", from: 1, to: 1 }, { t: "now", from: 2, to: 2 }, ]); assert.equal(sameToken("40", "40.0"), true); assert.equal(sameToken("weighed", "weighd"), true); assert.equal(sameToken("weighed", "waited"), false); }); const words = (list, t0 = 0) => list.split(" ").map((w, i) => ({ w, start: t0 + i * 0.4, end: t0 + i * 0.4 + 0.3 })); test("a quote with its numbers mangled both ways is found where it was said", () => { const heard = words("so total loss now is thirty eight point five pounds I started at three hundred forty nine I'm down to three hundred ten point five my goal", 100); const r = findQuote("total loss now is 38.5 pounds, started at 349, I'm down to 310.5", heard, { expectedStart: 100.4 }); assert.equal(r.found, true); assert.equal(r.match >= 0.9, true, String(r.match)); assert.equal(r.from, 100.4); assert.equal(r.offset, 0); assert.equal(quoteStatus(r), "ok"); }); test("an ellipsis: fragments found in order; a quote that is not there is not found", () => { const heard = words("I have begun what is commonly referred to as dry January which only about 17 percent of people can complete I've also begun intermittent fasting today", 10); const r = findQuote("I have begun what is commonly referred to as dry January... I've also begun intermittent fasting today.", heard); assert.equal(r.found, true); assert.equal(r.fragments.length, 2); assert.ok(r.fragments.every((f) => f.match === 1)); const miss = findQuote("I promise you, by this time next year I will be down 100 pounds", heard); assert.equal(miss.found, false); assert.equal(quoteStatus(miss), "not-found"); // Short quotes match whole or not at all. assert.equal(findQuote("dry February", heard).found, false); }); test("cue words: a cue's words spread over it, its first word exact", () => { const w = cueWords([{ start: 10, end: 12, text: "a bb ccc" }, { start: 20, end: 21, text: "x" }], { from: 0, to: 15 }); assert.deepEqual(w.map((x) => [x.w, x.start, x.anchor ?? false]), [["a", 10, true], ["bb", 10.44, false], ["ccc", 11.11, false]]); }); // THE 6 NOV WINDOWS. Heard = the window's words on the cue clock (window start // + the file's seconds); cued = the same quote found in the cues. The editing // agent's offsets: c43 +13.1, c45 +13.6, c20 +5.2, k46 -0.7, c44 0. const EXPECTED = { c43: [13.1, 0.15], c45: [13.6, 0.15], c20: [5.2, 0.5], k46: [-0.7, 0.3], c44: [0, 0.15] }; for (const clip of FIX.clips) { test(`rev9-a3 ${clip.id} (${clip.channel}/${clip.video}): the quote is heard ${EXPECTED[clip.id][0]} s from its cues`, () => { const [from, to] = clip.window; const heard = findQuote(clip.quote, clip.words.map((w) => ({ ...w, start: w.start + from, end: w.end + from }))); const cued = findQuote(clip.quote, cueWords(clip.cues, { from: from - 30, to: to + 30 })); assert.equal(heard.found, true, `heard ${heard.match}`); assert.equal(cued.found, true, `cued ${cued.match}`); const off = quoteOffset(heard, cued); const [want, tol] = EXPECTED[clip.id]; assert.ok(Math.abs(off - want) <= tol, `${clip.id}: ${off} vs ${want}`); }); }