phonemize
Version:
Fast phonemizer with rule-based G2P prediction. Pure JavaScript implementation.
89 lines (88 loc) • 3.15 kB
TypeScript
import { LanguageProcessor } from "../g2p";
export type EnglishDialect = "en-US" | "en-GB";
export interface HomographEntry {
pronunciation: string;
pos: string;
}
export interface HomographDict {
[word: string]: HomographEntry[];
}
export interface TraceStep {
grapheme: string;
phoneme: string;
rule: string;
}
export interface TraceResult {
word: string;
ipa: string;
path: "dictionary" | "morphology" | "decomposition" | "rules";
syllables?: string[];
steps: TraceStep[];
}
export declare class EnglishG2P implements LanguageProcessor {
private dictionary;
/**
* Per-instance custom pronunciations. We keep these separate from
* `this.dictionary` (which references the shared module-level JSON
* object) so that `addPronunciation()` on one instance doesn't leak
* to other instances created in the same process.
*/
private customDict;
private homographs;
private disableDict;
private dialect;
/**
* Opt-in: route through the principled pipeline (en-principled) when it
* produces output. Off by default — the existing predictInternal +
* postBase path is the well-tested production code. When this flag is
* on, the principled pipeline runs FIRST for each word; if it returns
* non-null, its output is used (skipping the legacy path entirely).
*/
private enablePrincipled;
readonly id = "en-g2p";
readonly name = "English G2P Processor";
/**
* Accepts the bare `en` tag plus both major dialects. The same
* instance can serve both — see `predict()` for per-call dispatch.
*/
readonly supportedLanguages: string[];
constructor(options?: {
disableDict?: boolean;
dialect?: EnglishDialect;
enablePrincipled?: boolean;
});
/**
* Expand numbers, abbreviations, currency, dates, times, etc. into
* spoken English form before tokenization.
*/
preProcess(text: string): string;
/**
* Per-word POS tagging for homograph disambiguation (read/lead/wind/…).
* Delegates to the rule-based simplePOSTagger in pos-tagger.ts, which
* is English-only by design — other languages plug in their own tagger
* via LanguageProcessor.tagWord if they need POS.
*/
tagWord(word: string, context?: {
prev?: string;
next?: string;
}): {
pos: string;
confidence: number;
};
predict(word: string, language?: string, pos?: string): string | null;
trace(word: string, language?: string, pos?: string): TraceResult;
private predictInternal;
private matchPos;
private wellKnown;
private tryMorphologicalAnalysis;
/**
* Two-part compound split using the mined head/tail tables. Picks
* the split with the most balanced halves (longest minimum part).
* A doubled consonant at the boundary signals suffixing rather than
* compounding (abet|ting) and rejects the split point.
*/
private tryCompoundSplit;
private tryDecomposition;
addPronunciation(word: string, pronunciation: string): void;
}
export default EnglishG2P;