// The phrase finder's numbers (release 34), from one phrase's scan // (common/lib/montage/phraseScan.mjs: phraseScan or phraseScanMany): how many // times it was said once folded, by year, from the preferred (Rumble) copy or // another channel, how many need an ear for who says it, and a handful of // contexts spread over the years to read and play. // // Pure, so the page, its API and `umtool phrases` print the same numbers. /** `YYYYMMDD…` → `YYYY`, or null. */ export const yearOf = (date) => (/^(\d{4})/.exec(String(date ?? ""))?.[1] ?? null); /** `{year: count}` over the instances, in year order, every year between the first and the last present (0 for a gap). */ export function byYear(instances) { const n = new Map(); for (const i of instances) { const y = yearOf(i.date); if (y) n.set(y, (n.get(y) ?? 0) + 1); } const ys = [...n.keys()].map(Number).sort((a, b) => a - b); const out = {}; if (!ys.length) return out; for (let y = ys[0]; y <= ys[ys.length - 1]; y++) out[String(y)] = n.get(String(y)) ?? 0; return out; } const BARS = " ▁▂▃▄▅▆▇█"; /** A one-line bar row of `byYear`: one character per year. */ export function sparkline(years) { const vals = Object.values(years); const max = Math.max(0, ...vals); if (!max) return ""; return vals.map((v) => (v ? BARS[Math.max(1, Math.round((v / max) * 8))] : BARS[0])).join(""); } /** Pick `n` of `list` spread evenly from first to last (all of them when there are fewer). */ export function spread(list, n) { if (list.length <= n) return list.slice(); if (n <= 1) return list.slice(0, Math.max(0, n)); const out = []; for (let k = 0; k < n; k++) out.push(list[Math.round((k * (list.length - 1)) / (n - 1))]); return out; } /** * One phrase's summary. `prefer` is the preferred channel (the Rumble copy): * an instance there counts as `preferred`, every other as `other`; `rumble` * counts by platform, whatever the channel. `samples` contexts (10) are spread * over the chronological instances. */ export function summarisePhrase(result, { prefer = null, samples = 10 } = {}) { const instances = Array.isArray(result?.instances) ? result.instances : []; const bySlug = {}; for (const i of instances) bySlug[i.slug] = (bySlug[i.slug] ?? 0) + 1; const years = byYear(instances); return { instances: instances.length, hits: Array.isArray(result?.hits) ? result.hits.length : 0, dropped: Array.isArray(result?.dropped) ? result.dropped.length : 0, scanned: Number(result?.scanned) || 0, years, spark: sparkline(years), rumble: instances.filter((i) => i.platform === "rumble").length, notRumble: instances.filter((i) => i.platform !== "rumble").length, preferred: prefer ? instances.filter((i) => i.slug === prefer).length : 0, other: prefer ? instances.filter((i) => i.slug !== prefer).length : instances.length, placed: instances.filter((i) => i.via).length, speakerChecks: instances.filter((i) => i.speakerCheck).length, bySlug, first: instances[0]?.date ?? null, last: instances[instances.length - 1]?.date ?? null, samples: spread(instances, samples).map((i) => ({ slug: i.slug, videoId: i.videoId, date: i.date ?? null, title: i.title ?? null, platform: i.platform ?? null, start: i.start, end: i.end, match: i.match, context: String(i.context ?? "").slice(0, 300), speakerCheck: Boolean(i.speakerCheck), ...(i.via ? { via: { slug: i.via.slug, videoId: i.via.videoId } } : {}), })), }; } /** A phrase as a project slug: `slow news day` → `slow-news-day-montage`; a regex keeps its letters. */ export function montageSlug(text) { const base = String(text ?? "") .toLowerCase() .replace(/\\[bswd]/g, " ") .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 48) .replace(/-+$/g, ""); return `${base || "phrase"}-montage`; }