mirror of
https://github.com/bitbaum/speechkit.git
synced 2026-12-05 03:19:59 +01:00
Extracted from Heidi's speaking practice, where it measures Zurich German learners. Language-agnostic: vowels, fillers, dialect markers and the grammar code are passed in; nothing here names a language. Four layers: signal (pauses and runs from raw samples — works on a dialect nothing transcribes), evidence (a translated transcript is never evidence about form), words (rate, where a long pause fell, did the recogniser answer in the variety spoken), grammar (your own LanguageTool, filtered to what a speaker is answerable for). No pronunciation score, and a test on the built package fails if an export starts to look like one. dist/ committed so `github:bitbaum/speechkit#v1.0.0` installs without a build; CI fails if it drifts from the source. npm publishing is wired (tag-driven, tokenless) but gated off until the one-time passkey bootstrap in the README. 90 unit tests on the source, 4 on dist/. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
84 lines
3.5 KiB
JavaScript
84 lines
3.5 KiB
JavaScript
/**
|
||
* Counting syllables, which is what turns a transcript into a speech RATE.
|
||
*
|
||
* `delivery.ts` deliberately reports no rate: syllables per second is the
|
||
* standard fluency measure and counting syllables needs words, which the
|
||
* signal alone does not have. With a transcript in the variety that was spoken
|
||
* (see `evidence.ts`) the measure becomes available, and it is the most
|
||
* informative number in the whole surface — speech rate and mean length of run
|
||
* are what "fluency" means in the research sense, as distinct from how good
|
||
* somebody sounds.
|
||
*
|
||
* ONE RULE, WITH ONE SWITCH: COUNT VOWEL GROUPS, OR VOWEL LETTERS.
|
||
*
|
||
* A run of adjacent vowel letters is one nucleus. That is the whole algorithm,
|
||
* and it arrived at that size by being written larger first: an earlier draft
|
||
* carried a list of digraphs — `au`, `ei`, `ie`, `äu` — to merge into single
|
||
* nuclei, and the tests showed every one of them was a no-op. German writes
|
||
* its diphthongs as adjacent vowels, so the group scan had already merged
|
||
* them. The list was machinery that looked like knowledge.
|
||
*
|
||
* WHY A RULE AND NOT A DICTIONARY. A hyphenation dictionary would be exact and
|
||
* would also be a per-language download, a licence question, and a word that
|
||
* is missing the one time it matters. A vowel-group rule is a few lines, has
|
||
* no licence, and works on a word it has never seen — including the compounds
|
||
* German invents on the spot, which is exactly where a dictionary fails.
|
||
*
|
||
* The error it makes is stated in the tests and is CONSISTENT: the same word
|
||
* counts the same way every time. That matters because the number is used
|
||
* comparatively — this take against the learner's own last one — where a
|
||
* systematic bias cancels and only the change survives. Comparing against a
|
||
* published native norm would need exactness this does not have, which is one
|
||
* more reason no such comparison exists anywhere in this product.
|
||
*
|
||
* Pure: no I/O, same input -> same output.
|
||
*/
|
||
/**
|
||
* Syllables in one word.
|
||
*
|
||
* Never returns zero for a word containing a letter: a word nobody can
|
||
* pronounce is not a thing a learner said, and a zero would silently deflate
|
||
* every rate it feeds.
|
||
*/
|
||
export function countSyllables(word, rule) {
|
||
const cleaned = word.toLowerCase().replace(/[^\p{L}]/gu, "");
|
||
if (!cleaned)
|
||
return 0;
|
||
const vowels = new Set([...rule.vowels]);
|
||
let count = 0;
|
||
let inNucleus = false;
|
||
for (const ch of cleaned) {
|
||
if (vowels.has(ch)) {
|
||
// Merging counts a RUN of vowels once; not merging counts each letter.
|
||
if (!inNucleus || !rule.adjacentVowelsMerge)
|
||
count++;
|
||
inNucleus = true;
|
||
}
|
||
else {
|
||
inNucleus = false;
|
||
}
|
||
}
|
||
return Math.max(1, count);
|
||
}
|
||
/** Syllables across a list of words. */
|
||
export function countSyllablesIn(words, rule) {
|
||
let total = 0;
|
||
for (const word of words)
|
||
total += countSyllables(word, rule);
|
||
return total;
|
||
}
|
||
/**
|
||
* Split a transcript into words.
|
||
*
|
||
* Unicode-aware and apostrophe-tolerant, because `gaht's` is one word and
|
||
* splitting it into two would inflate every word count and deflate every mean
|
||
* word length. Digits are dropped rather than counted: "2019" is said as a
|
||
* number of syllables nothing here can derive from the characters, and
|
||
* guessing would put an invented figure inside a measured one.
|
||
*/
|
||
export function words(text) {
|
||
return (text.match(/[\p{L}][\p{L}'’‑-]*/gu) ?? [])
|
||
.map((w) => w.replace(/^['’]+|['’]+$/g, ""))
|
||
.filter(Boolean);
|
||
}
|
||
//# sourceMappingURL=syllables.js.map
|