From 029b49f21a8d1e18b980e5a6e785dc5b5a478cfc Mon Sep 17 00:00:00 2001 From: elffuss-bot Date: Fri, 2 Oct 2026 11:39:06 +0000 Subject: [PATCH] Apply item 0xe5e8732a for issue #2 Squash of the patch of item 0xe5e8732a047e9b487e856f47bfd4a96074d25264d0ab22897c8d7106fa6316da, final in the elffuss ledger. Solver 0x3c4b6c950e8bf72399a3f54c7141ed8c487a9daf; 2 approvals of round 0. T2T-Item: 0xe5e8732a047e9b487e856f47bfd4a96074d25264d0ab22897c8d7106fa6316da T2T-Issue: 0x09060fd098e76bfb06be9362ac725843d1834d9218762843b41c83830b66a5c3 T2T-Event: 0xa3f482bef7a2d33d74d754067c3a813488b24309cb56d1040c422d7704d25908 --- diarize.mjs | 2 +- tests/acceptance/2/CRITERIA.json | 18 ++++++++++++++++++ tests/acceptance/2/criteria.test.js | 13 +++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/acceptance/2/CRITERIA.json create mode 100644 tests/acceptance/2/criteria.test.js diff --git a/diarize.mjs b/diarize.mjs index 622b526..2aa9fbe 100644 --- a/diarize.mjs +++ b/diarize.mjs @@ -25,7 +25,7 @@ export async function loadDiarizer(TF, { device = 'wasm' } = {}) { return { seg, segP, emb, embP }; } -const cos = (a, b) => { let dt = 0, na = 0, nb = 0; for (let i = 0; i < a.length; i++) { dt += a[i]*b[i]; na += a[i]*a[i]; nb += b[i]*b[i]; } return dt / (Math.sqrt(na)*Math.sqrt(nb) || 1); }; +const cos = (a, b) => { if (a.length !== b.length) throw new Error('cosine: vectors must have the same length'); let dt = 0, na = 0, nb = 0; for (let i = 0; i < a.length; i++) { dt += a[i]*b[i]; na += a[i]*a[i]; nb += b[i]*b[i]; } return dt / (Math.sqrt(na)*Math.sqrt(nb) || 1); }; const rms = (a) => { let s = 0; for (const v of a) s += v*v; return Math.sqrt(s/a.length); }; async function embed(D, slice) { diff --git a/tests/acceptance/2/CRITERIA.json b/tests/acceptance/2/CRITERIA.json new file mode 100644 index 0000000..549b0d2 --- /dev/null +++ b/tests/acceptance/2/CRITERIA.json @@ -0,0 +1,18 @@ +{ + "schema": "elffuss-t2t/criteria@2", + "issue": 2, + "criteria": [ + { + "index": 1, + "tests": [ + "tests/acceptance/2/criteria.test.js::criterion 1" + ] + }, + { + "index": 2, + "tests": [ + "tests/acceptance/2/criteria.test.js::criterion 2" + ] + } + ] +} diff --git a/tests/acceptance/2/criteria.test.js b/tests/acceptance/2/criteria.test.js new file mode 100644 index 0000000..f2a1b8a --- /dev/null +++ b/tests/acceptance/2/criteria.test.js @@ -0,0 +1,13 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +const mod = await import('../../../diarize.mjs'); + +test('criterion 1', () => { + assert.throws(() => mod.cosine([1, 2, 3], [1, 2]), (err) => err instanceof Error && err.message.startsWith('cosine:')); + assert.equal(mod.cosine([1, 0], [1, 0]), 1); +}); + +test('criterion 2', () => { + assert.throws(() => mod.cosine([1, 0], [1, 0, 5]), (err) => err instanceof Error && err.message.startsWith('cosine:')); + assert.equal(mod.cosine([0, 0], [0, 0]), 0); +});