hama-js
Version:
G2P, phoneme-ASR, and P2G inference for Node, Bun, and browsers, powered by a self-contained WASM engine (no onnxruntime).
158 lines (157 loc) • 5.57 kB
TypeScript
import { G2PResult, PreserveLiteralsMode } from "./tokenizer.js";
export interface PronunciationTerm {
id?: string;
text: string;
canonical?: string;
pronunciations?: Array<string | string[]>;
aliases?: string[];
metadata?: Record<string, unknown>;
}
export interface PronunciationScanOptions {
language?: string;
spanUnit?: "token" | "character";
maxDistanceRatio?: number;
minDistance?: number;
maxDistance?: number | null;
thresholdBasis?: "phonemes" | "characters";
wordBoundaryMode?: "strict" | "flexible";
tokenSlack?: number;
qgramSize?: number;
maxTermPronunciations?: number;
verifier?: "auto" | "ukkonen" | "myers";
scoring?: "phoneme" | "hybrid";
phonemeWeight?: number;
textWeight?: number;
minScore?: number;
resolveOverlaps?: "all" | "best_non_overlapping" | "per_term_best";
allowShortFuzzy?: boolean;
returnPhonemes?: boolean;
debug?: boolean;
}
export interface PronunciationMatch {
termId?: string | null;
termText: string;
canonical: string;
aliasText?: string | null;
matchedText: string;
startChar: number;
endChar: number;
startToken: number;
endToken: number;
score: number;
phonemeDistance: number;
phonemeThreshold: number;
phonemeSimilarity: number;
textDistance?: number | null;
textSimilarity?: number | null;
termPronunciation?: string[] | string | null;
matchedPronunciation?: string[] | string | null;
metadata?: Record<string, unknown> | null;
}
export interface PronunciationScanStats {
tokenCount?: number;
windowCount?: number;
candidateVariantsConsidered?: number;
candidateVariantsVerified?: number;
matchesReturned?: number;
rejectedByLength?: number;
rejectedByInputLimit?: number;
rejectedByQgram?: number;
rejectedByDistance?: number;
}
export interface PronunciationScanResult {
matches: PronunciationMatch[];
stats: PronunciationScanStats;
}
export interface PronunciationReplaceOptions {
language?: string;
spanUnit?: "token" | "character";
maxDistanceRatio?: number;
minDistance?: number;
maxDistance?: number | null;
thresholdBasis?: "phonemes" | "characters";
wordBoundaryMode?: "strict" | "flexible";
tokenSlack?: number;
qgramSize?: number;
maxTermPronunciations?: number;
verifier?: "auto" | "ukkonen" | "myers";
scoring?: "phoneme" | "hybrid";
phonemeWeight?: number;
textWeight?: number;
minScore?: number;
allowShortFuzzy?: boolean;
returnPhonemes?: boolean;
debug?: boolean;
replacementSource?: "canonical" | "term_text" | "alias_text";
caseStrategy?: "canonical" | "match_simple";
conflictPolicy?: "weighted_interval" | "greedy_left_to_right" | "error";
ambiguousPolicy?: "skip" | "keep_best";
ambiguityMargin?: number;
includeUnchanged?: boolean;
includeDiscarded?: boolean;
keepScanMatches?: boolean;
}
export interface PronunciationPatch {
status?: "applied" | "unchanged" | "discarded_overlap" | "discarded_ambiguous" | "discarded_duplicate";
discardReason?: string | null;
termId?: string | null;
termText: string;
canonical: string;
aliasText?: string | null;
matchedText: string;
replacementText: string;
startChar: number;
endChar: number;
outputStartChar?: number | null;
outputEndChar?: number | null;
startToken: number;
endToken: number;
score: number;
phonemeDistance: number;
phonemeThreshold: number;
phonemeSimilarity: number;
textDistance?: number | null;
textSimilarity?: number | null;
changed: boolean;
deltaChars: number;
termPronunciation?: string[] | string | null;
matchedPronunciation?: string[] | string | null;
metadata?: Record<string, unknown> | null;
}
export interface PronunciationReplaceStats {
tokenCount?: number;
windowCount?: number;
candidateVariantsConsidered?: number;
candidateVariantsVerified?: number;
rejectedByLength?: number;
rejectedByQgram?: number;
rejectedByDistance?: number;
rawMatches?: number;
dedupedMatches?: number;
ambiguousDiscarded?: number;
overlapDiscarded?: number;
duplicateDiscarded?: number;
appliedCount?: number;
unchangedCount?: number;
}
export interface PronunciationReplaceResult {
originalText: string;
text: string;
applied: PronunciationPatch[];
discarded: PronunciationPatch[];
patches: PronunciationPatch[];
stats: PronunciationReplaceStats;
rawMatches?: PronunciationMatch[] | null;
}
export interface PronunciationPredictor {
predict(text: string, options?: {
splitDelimiter?: string | RegExp | null;
outputDelimiter?: string;
preserveLiterals?: PreserveLiteralsMode;
}): Promise<G2PResult>;
getMaxInputLen?(): number | null;
}
export declare function pronunciationScanWithModel(model: PronunciationPredictor, text: string, terms: Array<string | PronunciationTerm>, options?: PronunciationScanOptions): Promise<PronunciationScanResult>;
export declare function pronunciationReplaceWithModel(model: PronunciationPredictor, text: string, terms: Array<string | PronunciationTerm>, options?: PronunciationReplaceOptions): Promise<PronunciationReplaceResult>;
export declare const mergeScanOptions: (options?: PronunciationScanOptions) => Required<PronunciationScanOptions>;
export declare const mergeReplaceOptions: (options?: PronunciationReplaceOptions) => Required<PronunciationReplaceOptions>;