phonemize
Version:
Fast phonemizer with rule-based G2P prediction. Pure JavaScript implementation.
59 lines (58 loc) • 2.4 kB
TypeScript
/**
* English stress assignment FSM (P2.1 of G2P redesign).
*
* Given a syllable count and (optionally) the outer suffix that was peeled
* off, return the index of the primary stress (and secondary if computable).
*
* Two layers:
*
* 1. Suffix-driven (rule from SuffixEntry.stress):
* - "neutral": inherit base stress (caller supplies baseStress)
* - "pre": stress on syllable immediately before suffix
* - "pre2": stress on syllable two before suffix
* - "self": suffix syllable bears stress
*
* 2. Latin default (no suffix or after recursion):
* - 1-2 syllable words: stress initial
* - 3+ syllable words: heavy penult attracts, else antepenult
* - A syllable is "heavy" if it has a long vowel/diphthong or is closed
*
* Syllable representation here is opaque — caller is responsible for
* syllabifying. The FSM works on syllable counts + heaviness predicate.
*/
import type { SuffixEntry } from "./suffixes";
export interface Stress {
/** Index (0-based) of the primary-stressed syllable. */
primary: number;
/** Optional index of secondary stress (often 2 syllables before primary). */
secondary?: number;
}
/**
* Count vowel nuclei in an IPA string. A diphthong like "eɪ" counts as one
* (two consecutive vowel chars = one nucleus run). Stress marks act as
* syllable boundaries (so "vi" + "ˈeɪ" in "abbreviation" counts as two
* separate nuclei, not one hiatus run).
*/
export declare function countIpaSyllables(ipa: string): number;
/**
* Find the index of the primary-stressed syllable from a dict IPA string.
* Returns -1 if no primary stress mark is present.
*/
export declare function dictStressIdx(ipa: string): number;
/**
* Default Latin stress: heavy penult attracts, else antepenult. For very
* short words, stress initial syllable.
*/
export declare function latinStress(syllableCount: number, isHeavy: (i: number) => boolean): Stress;
/**
* Assign stress to a word given total syllable count, an optional outer
* suffix that was peeled off, and (for the neutral case) the base's own
* stress index. Position-from-end indexing matches the underlying Latin
* stress rule and unifies the suffix table.
*/
export declare function assignStress(opts: {
syllableCount: number;
suffix?: SuffixEntry;
baseStress?: number;
isHeavy?: (i: number) => boolean;
}): Stress;