// Spoken numbers as digits (release 30, lane P). An ASR transcript writes a // number the way it was said — "eighty five players left", "three hundred // seventy three thousand", "$2,750 buy in", "233k" — and the frame scan's // spoken signal (`spoken.ts`) reads stats out of that text with plain // patterns. So the text is normalized first: every run of number words (and // every digit group with commas, a decimal point or a `k`/`m` suffix) becomes // one plain number, and the patterns only ever see digits. // // Rule-based and pure. What it does NOT do: ordinals ("third"), fractions // ("a half"), years read as pairs ("twenty twenty five" is 20 then 25), and // "grand". A run that is not a well-formed number splits where the next word // cannot continue it ("five ten" is 5 then 10, as blinds are said). const UNITS: Record = { zero: 0, oh: 0, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10, eleven: 11, twelve: 12, thirteen: 13, fourteen: 14, fifteen: 15, sixteen: 16, seventeen: 17, eighteen: 18, nineteen: 19, }; const TENS: Record = { twenty: 20, thirty: 30, forty: 40, fourty: 40, fifty: 50, sixty: 60, seventy: 70, eighty: 80, ninety: 90, }; const SCALES: Record = { thousand: 1_000, million: 1_000_000, billion: 1_000_000_000 }; // Ordinals, read as their number: a unit ordinal only after a tens word // ("eighty first" → 81; "first place" stays words), a teen or tens ordinal // on its own ("thirteenth", "twentieth"). const UNIT_ORDINALS: Record = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6, seventh: 7, eighth: 8, ninth: 9 }; const OWN_ORDINALS: Record = { tenth: 10, eleventh: 11, twelfth: 12, thirteenth: 13, fourteenth: 14, fifteenth: 15, sixteenth: 16, seventeenth: 17, eighteenth: 18, nineteenth: 19, twentieth: 20, thirtieth: 30, fortieth: 40, fiftieth: 50, sixtieth: 60, seventieth: 70, eightieth: 80, ninetieth: 90, }; // A digit token: "2,750", "1.5", "233k", "$5", "10m". The `$` stays outside. const DIGITS = /^(\d{1,3}(?:,\d{3})+|\d+)(?:\.(\d+))?(k|m|mil|bn)?$/i; type Tok = { word: string; raw: string }; function digitValue(w: string): number | null { const m = DIGITS.exec(w); if (!m) return null; let v = Number(m[1].replace(/,/g, "") + (m[2] ? `.${m[2]}` : "")); const suf = (m[3] ?? "").toLowerCase(); if (suf === "k") v *= 1_000; else if (suf === "m" || suf === "mil") v *= 1_000_000; else if (suf === "bn") v *= 1_000_000_000; return Number.isFinite(v) ? v : null; } // What kind of number word a token is, or null. function kindOf(w: string): "unit" | "tens" | "hundred" | "scale" | "digits" | "ordinal" | null { if (w in UNITS) return "unit"; if (w in OWN_ORDINALS) return "ordinal"; if (w in TENS) return "tens"; if (w === "hundred") return "hundred"; if (w in SCALES) return "scale"; if (digitValue(w) !== null) return "digits"; return null; } // One number from a run of tokens that form it (the caller decides the run). class Acc { total = 0; current = 0; decimal: string | null = null; last: "unit" | "tens" | "hundred" | "scale" | "digits" | "a" | "ordinal" | null = null; any = false; // The count before the last "hundred" ("fifteen hundred" → 15). hundredsCount = 0; value(): number { return this.total + this.current; } // Whether `w` (of kind `k`) continues the number so far. accepts(k: ReturnType | "a" | "point" | "unitOrdinal", w: string): boolean { if (!this.any) return k !== "point" && k !== "unitOrdinal"; // An ordinal ends a number. if (this.last === "ordinal") return false; if (k === "unitOrdinal") return this.last === "tens"; if (k === "ordinal") return false; if (this.decimal !== null) return k === "unit" && UNITS[w] <= 9 ? true : k === "scale"; switch (k) { case "unit": // "twenty five" and "a hundred five" continue; "five ten" does not. if (this.last === "tens") return UNITS[w] <= 9; return this.last === "hundred" || this.last === "scale"; case "tens": return this.last === "hundred" || this.last === "scale"; case "hundred": return (this.last === "unit" || this.last === "a") && this.current < 100 && this.current > 0; case "scale": return this.last !== "scale" && this.value() > 0 && (this.lastScale() === 0 || SCALES[w] < this.lastScale()); case "digits": return false; case "point": return this.last === "unit" || this.last === "tens" || this.last === "digits"; case "a": return false; default: return false; } } private scales: number[] = []; private lastScale(): number { // A scale may follow only a smaller one ("two million three hundred // thousand"); "thousand thousand" is two numbers. return this.scales.length ? Math.min(...this.scales) : 0; } add(k: NonNullable> | "a" | "point" | "unitOrdinal", w: string): void { this.any = true; if (k === "unitOrdinal") { this.current += UNIT_ORDINALS[w]; this.last = "ordinal"; return; } if (k === "ordinal") { this.current += OWN_ORDINALS[w]; this.last = "ordinal"; return; } if (k === "point") { this.decimal = ""; this.last = "unit"; return; } if (this.decimal !== null && k === "unit") { this.decimal += String(UNITS[w]); this.current = Math.trunc(this.current) + Number(`0.${this.decimal}`); return; } switch (k) { case "a": this.current = 1; break; case "unit": this.current += UNITS[w]; break; case "tens": this.current += TENS[w]; break; case "hundred": this.hundredsCount = this.current || 1; this.current = (this.current || 1) * 100; break; case "scale": this.total += (this.current || 1) * SCALES[w]; this.current = 0; this.scales.push(SCALES[w]); this.decimal = null; break; case "digits": this.current = digitValue(w) ?? 0; break; } this.last = k; } } // "no one", "the one", "this one": a pronoun, not a count. const NOT_A_NUMBER_BEFORE_ONE = new Set(["no", "any", "some", "every", "the", "this", "that", "which", "each", "another", "last", "next", "only"]); function tokenize(text: string): Tok[] { // Words, digit groups and the punctuation between them, every piece kept so // the text can be put back together around the numbers. return (text.match(/\$|\d[\d,]*(?:\.\d+)?(?:k|m|mil|bn)?\b|[A-Za-z]+(?:'[A-Za-z]+)?|[^A-Za-z\d$]+/gi) ?? []).map((raw) => ({ raw, word: raw.toLowerCase(), })); } const fmt = (v: number) => (Number.isInteger(v) ? String(v) : String(Math.round(v * 1000) / 1000)); // The words of the number run that would follow token `n` (its own word first). function runAhead(toks: readonly Tok[], n: number): string[] { const out: string[] = [toks[n].word]; for (let k = n + 1; k + 1 < toks.length && /^[\s-]+$/.test(toks[k].raw); k += 2) { const w = toks[k + 1].word; if (!kindOf(w) && w !== "and") break; out.push(w); } return out; } // Where two numbers said back to back would otherwise run together: "two // hundred four hundred" is 200 then 400 (not 204, 100), and "fifteen hundred // three thousand" is 1500 then 3000 (blinds, said the colloquial way: a // hundreds count of ten or more is a whole number, never the start of a // larger one). function splitsBefore(acc: Acc, k: string, toks: readonly Tok[], n: number): boolean { if (acc.last !== "hundred" || (k !== "unit" && k !== "tens")) return false; const ahead = runAhead(toks, n); const next = ahead.findIndex((w, i) => i > 0 && (w === "hundred" || w in SCALES)); if (next < 0) return false; if (ahead[next] === "hundred") return true; return acc.hundredsCount >= 10; } /** * The text with every spoken or written number as plain digits: "eighty five * players left" → "85 players left", "$2,750" → "$2750", "two hundred thirty * three thousand" → "233000", "1.5 million" → "1500000". Hyphens inside a * number ("eighty-five") join it. Everything else is kept as it was. */ export function normalizeNumbers(text: string): string { const toks = tokenize(text); const out: string[] = []; let i = 0; while (i < toks.length) { const t = toks[i]; const k = kindOf(t.word); // "a hundred", "a thousand": "a" starts a number only before a scale. const aStart = t.word === "a" && i + 2 < toks.length && /^\s+$/.test(toks[i + 1].raw) && (toks[i + 2].word === "hundred" || toks[i + 2].word in SCALES); if (!k && !aStart) { out.push(t.raw); i++; continue; } const acc = new Acc(); acc.add(aStart ? "a" : (k as NonNullable), t.word); let j = i + 1; let end = i; // last token index in the number while (j < toks.length) { const sep = toks[j]; // A separator between two number words: spaces, a hyphen, " and ". if (/^[\s-]+$/.test(sep.raw) && j + 1 < toks.length) { let n = j + 1; // "three hundred and five": "and" continues a number after hundred/scale. if (toks[n].word === "and" && (acc.last === "hundred" || acc.last === "scale") && n + 2 < toks.length && /^\s+$/.test(toks[n + 1].raw)) n += 2; const w = toks[n].word; const nk = w === "point" ? "point" : w in UNIT_ORDINALS ? "unitOrdinal" : kindOf(w); if (nk && acc.accepts(nk, w) && !splitsBefore(acc, nk, toks, n)) { if (nk === "point") { // "one point five": the point needs a unit after it. const after = toks[n + 2]?.word; if (!(n + 2 < toks.length && /^\s+$/.test(toks[n + 1].raw) && after && after in UNITS && UNITS[after] <= 9)) break; } acc.add(nk, w); end = n; j = n + 1; continue; } break; } // A digit group followed directly by a scale word with no space is not a thing; stop. break; } // "three hundred eleven K", "50 k": a lone k after a number is thousands. if (end + 2 < toks.length && /^ +$/.test(toks[end + 1].raw) && toks[end + 2].word === "k" && !/^[A-Za-z']/.test(toks[end + 3]?.raw ?? "")) { out.push(fmt(acc.value() * 1000)); i = end + 3; continue; } // A lone "oh" or "one" stays a word only when it is plainly not a number: // "oh" alone, never; "one" alone becomes 1 (the patterns need it). const prev = out.length ? (out[out.length - 1].trim() === "" ? out[out.length - 2] ?? "" : out[out.length - 1]).trim().toLowerCase() : ""; if (end === i && (t.word === "oh" || (t.word === "one" && NOT_A_NUMBER_BEFORE_ONE.has(prev)))) { out.push(t.raw); i++; continue; } out.push(fmt(acc.value())); i = end + 1; } return out.join(""); } /** Every number in a text, in order (after `normalizeNumbers`). */ export function numbersIn(text: string): number[] { return (normalizeNumbers(text).match(/\d+(?:\.\d+)?/g) ?? []).map(Number); }