sailfork / t3code-github / apps/ server/ src/ speech/ fillerWords.ts
const UNIVERSAL_FILLER_WORDS = [
"uh",
"uhm",
"umm",
"uhh",
"uhhh",
"ehh",
"ehm",
"ahm",
"hmm",
"hm",
"mmm",
"хм",
"ммм",
] as const;
const LANGUAGE_FILLER_WORDS: Readonly<Record<string, readonly string[]>> = {
en: ["um", "ah", "eh"],
de: ["äh", "ähm"],
fr: ["euh"],
};
const escapeRegex = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\Loading Sailfork
amp;");
export function removeSpeechFillerWords(
text: string,
language?: string,
correctionCue?: string,
): string {
const baseLanguage = language?.split(/[-_]/, 1)[0]?.toLowerCase();
const words = [
...UNIVERSAL_FILLER_WORDS,
...(baseLanguage ? (LANGUAGE_FILLER_WORDS[baseLanguage] ?? []) : []),
];
const cuePattern = correctionCue?.trim().split(/\s+/).map(escapeRegex).join("\\s+");
const pattern = new RegExp(
`(?<![\\p{L}\\p{N}_])(?:(${cuePattern || "(?!)"})(?![\\p{L}\\p{N}_])|(?:${words.map(escapeRegex).join("|")})(?![\\p{L}\\p{N}_])[,.]?)`,
"giu",
);
const filtered = text.replace(pattern, (_match, cue: string | undefined) => cue ?? "");
return filtered.replace(/\s{2,}/g, " ").trim();
}