/** * Scores an answers array from scoring-key.csv alone. No product code is involved. * * The key lists, for every item of the four instruments, the scale it belongs to and the five weights * (w0..w4) that the answer index 0..4 earns. A scale's score is * round(100 * sum of the weights of the chosen answers / sum of the largest weight of each of its items). * `top` is the first scale, in scale_order, that reaches the highest score (an all-zero profile gives the * first scale). Reverse-keyed items are simply items whose weights run downwards. * * Built on 2026-10-05 as a replacement for the product scorer the analysis used up to then; its output was * compared with that scorer on every stored human and model answer vector (see README). */ import fs from 'fs' export function loadKey(file) { const lines = fs.readFileSync(file, 'utf8').split(/\r?\n/).filter((l) => l.length) const head = lines.shift().split(',') const col = Object.fromEntries(head.map((h, i) => [h, i])) const tests = {} for (const line of lines) { const f = line.split(',') const t = (tests[f[col.test]] ||= { items: [], order: [] }) if (Number(f[col.item]) !== t.items.length + 1) throw new Error(`${f[col.test]}: item numbers must run 1..n in order, got ${f[col.item]}`) const weights = [0, 1, 2, 3, 4].map((k) => Number(f[col[`w${k}`]])) if (weights.some((w) => !Number.isFinite(w))) throw new Error(`${f[col.test]} item ${f[col.item]}: weights must be numbers`) t.items.push({ scale: f[col.scale], weights }) t.order[Number(f[col.scale_order])] = f[col.scale] } for (const [slug, t] of Object.entries(tests)) { if (t.order.some((s) => s == null) || t.items.some((it) => !t.order.includes(it.scale))) throw new Error(`${slug}: scale_order is not a clean 0..k-1 numbering`) } return tests } export function scoreFromKey(key, slug, answers) { const t = key[slug] if (!t) return null const raw = Object.fromEntries(t.order.map((s) => [s, { total: 0, max: 0 }])) answers.forEach((a, i) => { const it = t.items[i] if (!it) return raw[it.scale].total += it.weights[a] ?? 0 raw[it.scale].max += Math.max(...it.weights) }) const scores = {} let top = t.order[0] let topVal = 0 for (const s of t.order) { const { total, max } = raw[s] const p = max > 0 ? Math.round((total / max) * 100) : 0 scores[s] = p if (p > topVal) { topVal = p; top = s } } return { scores, topResult: top } }