// SPDX-License-Identifier: Apache-2.0 // Copyright 2026 Kale // number-normalised scoring: Whisper's read-back goes through the SAME front end (tech terms + dizione rules) as the input, then word // error rate and exact match against the front end's normalised text. usage: node score_fe.mjs import { readFileSync, writeFileSync } from "node:fs"; import { termsReplacer } from "../../frontend/terms.ts"; import { normalizer, readTable } from "../../frontend/words/kaleTts.js"; const [, , sc, idsf, outf] = process.argv; const here = new URL("../../frontend/words/", import.meta.url).pathname; const lex = readTable(readFileSync(here + "lexicon.tsv", "utf8")); readTable(readFileSync(here + "override.public.tsv", "utf8"), lex); const n0 = normalizer(lex); const applyTerms = termsReplacer(readFileSync(here + "terms.tsv", "utf8")); // terms.ts as is: dictionary, camelCase split, dictionary const norm = (t) => n0(applyTerms(t)); const words = (t) => t.toLowerCase().replace(/[-–—]/g, " ").replace(/[^a-z' ]/g, " ").split(/\s+/).filter(Boolean).map((w) => w.replace(/'/g, "")); const ed = (a, b) => { const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]); for (let j = 1; j <= b.length; j++) d[0][j] = j; for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1)); return d[a.length][b.length]; }; const S = JSON.parse(readFileSync(sc, "utf8")); const ref = Object.fromEntries(JSON.parse(readFileSync(idsf, "utf8")).map((r) => [r.id, r.normalized])); const out = {}; for (const [tag, rows] of Object.entries(S)) { let e = 0, w = 0, ex = 0; const miss = []; for (const r of rows) { const a = words(ref[r.id]), b = words(norm(r.heard || "")); const d = ed(a, b); e += d; w += a.length; if (d === 0) ex++; else miss.push({ id: r.id, ref: a.join(" "), heard: b.join(" ") }); } out[tag] = { wer: +(100 * e / w).toFixed(1), exact: `${ex}/${rows.length}`, miss }; console.log(tag, "word errors (number-normalised)", out[tag].wer, "% exact", out[tag].exact); } writeFileSync(outf, JSON.stringify(out, null, 1));