fix(dictation): take the filler language from what the engine is told, and drop the comma before a final filler

This commit is contained in:
Kelvin Amoaba
2026-11-09 04:54:26 +00:00
parent 318d526b2e
commit 52e4e599b2
3 changed files with 35 additions and 15 deletions
+12 -2
View File
@@ -9,6 +9,7 @@ describe('removeFillerWords', () => {
['en', 'Yes. Uh. No.', 'Yes. No.'],
['en', 'We should ship it, hmm', 'We should ship it'],
['en', 'we should um.', 'we should.'],
['en', 'We should ship, um.', 'We should ship.'],
['en', 'um so we ship', 'so we ship'],
['en', 'Um, iPhone builds first.', 'iPhone builds first.'],
[
@@ -30,6 +31,7 @@ describe('removeFillerWords', () => {
['en', 'The umbrella is humming, uh-huh.'],
['en', 'Hmm? What did you say?'],
['en', 'Ask UM and HMM about it.'],
['en', 'Open um.txt and set var_um to 3.'],
['ja', 'ええ、そうです。'],
['ko', '음이 너무 높아요.'],
['zh', '金额不对,那个文件也要改。']
@@ -50,14 +52,22 @@ describe('createDictationTextPipeline', () => {
expect(clean?.('Äh, um, ship it.')).toBe('Äh, ship it.')
})
it('reads the language from the setting for a multilingual model', () => {
it('reads the language from the setting for a multilingual model that takes it', () => {
const clean = createDictationTextPipeline(
{ language: 'multilingual' },
{ language: 'multilingual', dictationLanguages: ['de', 'en'] },
{ dictationLanguage: 'de' }
)
expect(clean?.('Äh, um fünf Uhr.')).toBe('Um fünf Uhr.')
})
it('ignores a language setting the model ignores', () => {
const clean = createDictationTextPipeline(
{ language: 'multilingual' },
{ dictationLanguage: 'en' }
)
expect(clean?.('Er hat um zehn Uhr angerufen.')).toBe('Er hat um zehn Uhr angerufen.')
})
it('leaves text untouched when filler removal is off', () => {
expect(createDictationTextPipeline({ language: 'en' }, { removeFillerWords: false })).toBeNull()
})
+9 -6
View File
@@ -17,13 +17,16 @@ FILLERS_BY_LANGUAGE.yue = FILLERS_BY_LANGUAGE.zh
// For an unknown language: English fillers that are no word elsewhere ("um" is German and Portuguese, "er" German).
const LANGUAGE_NEUTRAL_FILLERS = 'u+h+m*|u+m{2,}|e+r+m+|h+m+|m{3,}'
const WORD_CHAR = String.raw`[\p{L}\p{N}\p{M}'’\-]`
// A filler stands alone: no word character touches it, and "hm?" or "eh!" is a question or a call, so it stays.
const WORD_CHAR = String.raw`[\p{L}\p{N}\p{M}_/'’\-]`
// A filler stands alone: nothing joins it to a word (a dot only does inside a name like "um.txt"),
// and "hm?" or "eh!" is a question or a call, so it stays.
const STANDALONE = (fillers: string): string =>
String.raw`(?<!${WORD_CHAR})(${fillers})(?!${WORD_CHAR})(?!\s*[??!!])`
String.raw`(?<!${WORD_CHAR}|${WORD_CHAR}\.)(${fillers})(?!${WORD_CHAR}|\.${WORD_CHAR})(?!\s*[??!!])`
// The comma or ellipsis after a filler goes with it.
const TRAILING_PAUSE = String.raw`(?:\s*(?:[,,、…]|\.{3}))?\s*`
const SENTENCE_END_RE = /^[.!?。!?]+\s*/u
// A comma left in front of a removed filler that ended the sentence or the text.
const TRAILING_SOFT_PUNCTUATION_RE = /[\s,,、]+$/u
const SENTENCE_START_RE = /(^|[.!?。!?])\s*$/u
const NON_LATIN_LETTER_RE = /(?=\p{L})\P{Script=Latin}/u
@@ -75,11 +78,11 @@ export function removeFillerWords(text: string, languages: readonly string[] | u
cursor = match.index + match[0].length
const atSentenceStart = SENTENCE_START_RE.test(out)
const sentenceEnd = SENTENCE_END_RE.exec(text.slice(cursor))
// "we should um." keeps its full stop; "Um." on its own goes entirely.
// "we should, um." keeps its full stop; "Um." on its own goes entirely.
if (sentenceEnd && atSentenceStart) {
cursor += sentenceEnd[0].length
} else if (sentenceEnd) {
out = out.trimEnd()
out = out.replace(TRAILING_SOFT_PUNCTUATION_RE, '')
}
// Only a capitalised "Um" began a sentence the next word now begins.
capitalizeNext ||= atSentenceStart && filler.charAt(0) !== filler.charAt(0).toLowerCase()
@@ -90,5 +93,5 @@ export function removeFillerWords(text: string, languages: readonly string[] | u
// A filler at the very end leaves the comma before it dangling.
const endsOnFiller = cursor === text.length
keep(text.slice(cursor))
return (endsOnFiller ? out.replace(/[\s,,、]+$/u, '') : out).trim()
return (endsOnFiller ? out.replace(TRAILING_SOFT_PUNCTUATION_RE, '') : out).trim()
}
+14 -7
View File
@@ -1,24 +1,31 @@
import { DICTATION_LANGUAGE_AUTO } from './dictation-language'
import { engineDictationLanguage } from './dictation-language'
import { removeFillerWords } from './dictation-filler-words'
import type { SpeechModelManifest, VoiceSettings } from './speech-types'
export type DictationTextSettings = Pick<VoiceSettings, 'dictationLanguage' | 'removeFillerWords'>
/** The languages the dictated text is in: a single-language model's own, else the user's setting; undefined when unknown. */
export function dictatedTextLanguages(
model: Pick<SpeechModelManifest, 'language'> | undefined,
type DictationTextModel = Pick<SpeechModelManifest, 'language' | 'dictationLanguages'>
/** The languages the dictated text is in: a single-language model's own, else the one the engine is told; undefined when unknown. */
function dictatedTextLanguages(
model: DictationTextModel | undefined,
setting: string | undefined
): string[] | undefined {
if (model && model.language !== 'multilingual') {
if (!model) {
return undefined
}
if (model.language !== 'multilingual') {
// Bilingual models name both, as in 'zh-en'.
return model.language.split('-')
}
return setting && setting !== DICTATION_LANGUAGE_AUTO ? [setting] : undefined
// The Voice pane disables a setting the model ignores, so a stale one says nothing about what was spoken.
const language = engineDictationLanguage(model, setting)
return language ? [language] : undefined
}
/** The passes dictated text goes through before anyone sees it, or null when every pass is off. */
export function createDictationTextPipeline(
model: Pick<SpeechModelManifest, 'language'> | undefined,
model: DictationTextModel | undefined,
settings: Partial<DictationTextSettings> | undefined
): ((text: string) => string) | null {
if (settings?.removeFillerWords === false) {