// The phrase finder's scan (release 34): several phrases over a set of // channels in ONE pass (phraseScanMany), each typed as plain words or a // regular expression (phraseSource.mjs), through the cue-text cache under // umtool's CACHE_DIR. The page runs it as a job (`umtool phrases --spec … --out // …`, lib/phrase-finder/jobs.ts) and `umtool phrases` runs it in a terminal; // both get this. // // Read-only over the channels directory. Plain ESM, no server. import { phraseScanMany, phraseSourceOf } from "yt-dlp-transcript-common/lib/montage/phraseScan.mjs"; import { CHANNELS_DIR, cacheFile } from "../paths.mjs"; /** The most phrases one scan compares. */ export const MAX_PHRASES = 30; /** A channel directory's name: no separators, no dot first. */ export const CHANNEL_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; export const MODES = ["auto", "words", "regex"]; /** Where the joined cue text is cached (cueTextCache.mjs), under CACHE_DIR. */ export const phraseTextCacheDir = () => cacheFile("phrase-text"); /** * A scan's settings, checked: `{rows: [{id, text, mode, label?}], channels, * prefer, exclude}` → the same with each row's `source` (what is searched) * and `used` (`words` | `regex`); `label` names a phrase given as an * expression (a suggestion's "nobody asked"). Throws a sentence naming what is wrong. */ export function checkSpec(spec) { const rowsIn = Array.isArray(spec?.rows) ? spec.rows : []; const rows = []; const seen = new Set(); for (const [k, r] of rowsIn.entries()) { const text = String(r?.text ?? "").trim(); if (!text) continue; const mode = MODES.includes(r?.mode) ? r.mode : "auto"; let id = String(r?.id ?? `p${k + 1}`).trim() || `p${k + 1}`; if (!/^[A-Za-z0-9_-]{1,40}$/.test(id) || seen.has(id)) id = `p${k + 1}`; if (seen.has(id)) throw new Error(`two phrases share the id "${id}"`); seen.add(id); let src; try { src = phraseSourceOf(text, mode); } catch (e) { throw new Error(`"${text}": ${e instanceof Error ? e.message : String(e)}`); } const label = String(r?.label ?? "").trim().slice(0, 80); rows.push({ id, text, ...(label ? { label } : {}), mode, source: src.source, used: src.mode }); } if (!rows.length) throw new Error("type a phrase to search for"); if (rows.length > MAX_PHRASES) throw new Error(`at most ${MAX_PHRASES} phrases in one search`); const list = (v) => (Array.isArray(v) ? v : String(v ?? "").split(",")).map((c) => String(c).trim()).filter(Boolean); const channels = [...new Set(list(spec?.channels))]; const prefer = String(spec?.prefer ?? "").trim() || null; for (const c of [...channels, ...(prefer ? [prefer] : [])]) if (!CHANNEL_RE.test(c)) throw new Error(`"${c}" is not a channel`); if (!channels.length && !prefer) throw new Error("pick at least one channel"); const exclude = String(spec?.exclude ?? "").trim() || null; if (exclude) { try { new RegExp(exclude, "i"); } catch (e) { throw new Error(`exclude is not a regular expression: ${e instanceof Error ? e.message : String(e)}`); } } return { rows, channels, prefer, exclude }; } /** * Run a checked spec: `{version, spec, startedAt, seconds, scanned, rows: * [{id, text, mode, used, source, result}]}`, `result` being phraseScan's * answer for that phrase alone. `cacheDir: null` reads every cue file. */ export async function runPhraseScan(spec, { channelsDir = CHANNELS_DIR, cacheDir = phraseTextCacheDir(), onProgress } = {}) { const s = checkSpec(spec); const t0 = Date.now(); const results = await phraseScanMany({ channelsDir, channels: s.channels, prefer: s.prefer, exclude: s.exclude, phrases: s.rows.map((r) => ({ id: r.id, phrase: r.source })), cacheDir, onProgress, }); const byId = new Map(results.map((r) => [r.id, r])); return { version: 1, spec: s, startedAt: new Date(t0).toISOString(), seconds: Math.round((Date.now() - t0) / 100) / 10, scanned: results[0]?.scanned ?? 0, rows: s.rows.map((r) => { const { id: _id, phrase: _phrase, ...result } = byId.get(r.id); return { ...r, result }; }), }; }