Skip to content

Explainer videos and product demos made by your AI agent. Free and open source: a local voice (Kokoro), word timing (Whisper) and a canvas renderer turn a script into a narrated MP4.

Speech check: read years and ordinals the way the voice says them (#24)

Vincent Schmalbach230b613be960
diff --git a/src/align.js b/src/align.js
index 70c589a..52a87be 100644
--- a/src/align.js
+++ b/src/align.js
@@ -31,6 +31,28 @@ export function intWords(n) {
return [String(n)];
}
+// Kokoro reads a lone four-digit number like a year: 1582 "fifteen eighty
+// two", 1900 "nineteen hundred", 1905 "nineteen oh five". Numbers below 1100
+// or with fewer than ten in the last three digits (2000, 2005) stay cardinal.
+export function yearWords(n) {
+ if (n < 1100 || n > 9999 || n % 1000 < 10) return null;
+ const hi = Math.floor(n / 100);
+ const lo = n % 100;
+ if (n % 1000 >= 100 && lo === 0) return [...intWords(hi), 'hundred'];
+ if (n % 1000 >= 100 && lo < 10) return [...intWords(hi), 'oh', ...intWords(lo)];
+ return [...intWords(hi), ...intWords(lo)];
+}
+
+const ORDINALS = { one: 'first', two: 'second', three: 'third', five: 'fifth', eight: 'eighth', nine: 'ninth', twelve: 'twelfth' };
+
+// 29th -> twenty ninth, 101st -> one hundred first.
+function ordinalWords(n) {
+ const words = intWords(n);
+ const last = words.pop();
+ const ord = ORDINALS[last] ?? (last.endsWith('y') ? last.slice(0, -1) + 'ieth' : last + 'th');
+ return [...words.map(canon), ord];
+}
+
// Turns one written word into the tokens a voice would say, in canonical form.
// Whisper writes "$1,050" and "5%" where the voice said "one thousand and
// fifty dollars" and "five percent", and "example.com" or "1.1.1.1" where it
@@ -45,9 +67,13 @@ export function expand(word, language = 'en') {
// Whisper splits "1.1.1.1" into "1", ".1", ".1", ".1": a leading dot is a spoken "dot".
const lead = /^\.[a-z0-9]/i.test(raw) ? ['dot'] : [];
const n = canon(word);
+ const ordinal = /^(\d+)(st|nd|rd|th)$/.exec(n);
+ if (ordinal && !/\d,\d/.test(raw)) return [...lead, ...ordinalWords(Number(ordinal[1]))];
const money = /^([$€£])?(\d+)(?:\.(\d+))?(%)?$/.exec(n);
if (money && /\d/.test(raw) && !/^\d+\.\d+\.\d+/.test(n)) {
const [, cur, int, dec, pct] = money;
+ const year = !cur && dec === undefined && !pct && /^\d{4}$/.test(int) && !/\d,\d/.test(raw) ? yearWords(Number(int)) : null;
+ if (year) return [...lead, ...year].map(canon);
const out = [...lead, ...intWords(Number(int))];
if (dec !== undefined) {
if (cur) out.push(CURRENCY[cur], ...intWords(Number(dec)), 'cents');
diff --git a/test/align.test.js b/test/align.test.js
index cbe3a1b..b7c8793 100644
--- a/test/align.test.js
+++ b/test/align.test.js
@@ -78,3 +78,31 @@ test('a word counts as heard when Whisper writes its shown form', () => {
const wrong = alignWords([{ spoken: 'sequel', display: 'SQL' }], heard([['table', 0, 0.5]]), 0.6);
assert.ok(!wrong.words[0].matched);
});
+
+test('four-digit numbers are read like years, the way the voice reads them', async () => {
+ const { yearWords, expand } = await import('../src/align.js');
+ assert.deepEqual(yearWords(1582), ['fifteen', 'eighty', 'two']);
+ assert.deepEqual(yearWords(1900), ['nineteen', 'hundred']);
+ assert.deepEqual(yearWords(1905), ['nineteen', 'oh', 'five']);
+ assert.deepEqual(yearWords(2015), ['twenty', 'fifteen']);
+ assert.equal(yearWords(2000), null);
+ assert.equal(yearWords(2005), null);
+ assert.equal(yearWords(1066), null);
+ // A comma makes it an ordinary number again: one thousand five hundred eighty two.
+ assert.equal(expand('1,582').length, 6);
+ const year = alignWords([{ spoken: 'In' }, { spoken: 'fifteen eighty two', display: '1582' }, { spoken: 'it' }, { spoken: 'changed.' }], heard([['In', 0, 0.2], ['1582', 0.2, 0.9], ['it', 0.9, 1], ['changed.', 1, 1.4]]), 1.5);
+ assert.equal(year.matchRate, 1);
+ const plain = alignWords([{ spoken: '1900' }, { spoken: 'was' }, { spoken: 'not.' }], heard([['1900', 0, 0.6], ['was', 0.6, 0.8], ['not.', 0.8, 1]]), 1.1);
+ assert.equal(plain.matchRate, 1);
+ const hundred = alignWords([{ spoken: 'nineteen hundred,', display: '1900,' }], heard([['1900,', 0, 0.6]]), 0.7);
+ assert.equal(hundred.matchRate, 1);
+ const cardinal = alignWords([{ spoken: 'two thousand', display: '2000' }], heard([['2000', 0, 0.6]]), 0.7);
+ assert.equal(cardinal.matchRate, 1);
+});
+
+test('ordinals match the words the voice says', () => {
+ const r = alignWords([{ spoken: 'February' }, { spoken: 'twenty ninth', display: '29' }], heard([['February', 0, 0.5], ['29th', 0.5, 1]]), 1.1);
+ assert.equal(r.matchRate, 1);
+ const first = alignWords([{ spoken: 'the' }, { spoken: '1st' }], heard([['the', 0, 0.2], ['first', 0.2, 0.5]]), 0.6);
+ assert.equal(first.matchRate, 1);
+});