hex/src/dictation/filler_words.rs
Dax 7f544671ac
only remove filler words from english dictation (#1492)
* only remove filler words from english dictation (#1460)

* address review: pure filler gate, language source docs

* keep engine unchanged; document reload gap
2026-11-07 11:26:30 -05:00

236 lines
7.6 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Labs › Remove filler words: the English hesitations a model writes down
//! verbatim ("um", "umm", "uh", "uhh", "erm", "er", "ah") leave an English
//! dictation, with no model involved. A filler matches as a whole word,
//! lowercase or with only its first letter capitalised, so "umbrella",
//! "ahead", "error", "Uhura", "uh-huh", and "ER" stay as they are. The comma
//! or ellipsis that comes with a filler goes with it, the spaces around it
//! close up, and a capitalised filler that started a sentence hands its
//! capital to the next word. Line breaks are kept.
const FILLERS: &[&str] = &["um", "umm", "uh", "uhh", "erm", "er", "ah"];
/// Whether a dictation in `language` loses its fillers. Only English does:
/// "er" and "um" are ordinary German and Portuguese words, so "auto" and
/// every other language keep their transcripts as heard.
pub fn applies_to(language: &str) -> bool {
language == "en"
}
/// `transcript` without its filler words when the Labs switch is `enabled`
/// and the dictation is English; `None` leaves the caller's text untouched.
pub fn strip_if(transcript: &str, language: &str, enabled: bool) -> Option<String> {
(enabled && applies_to(language)).then(|| apply(transcript))
}
/// `transcript` without its filler words.
pub fn apply(transcript: &str) -> String {
let chars: Vec<char> = transcript.chars().collect();
let mut out = String::with_capacity(transcript.len());
// A filler was just removed: the next text decides the spacing.
let mut removed = false;
let mut capitalize = false;
let mut index = 0;
while index < chars.len() {
if let Some(end) = filler_at(&chars, index) {
trim_end(&mut out, &[]);
if !removed {
capitalize = chars[index].is_uppercase() && sentence_start(&out);
}
index = after_filler(&chars, end);
removed = true;
continue;
}
let c = chars[index];
index += 1;
if removed {
if is_space(c) {
continue;
}
if c == '\n' {
removed = false;
capitalize = false;
out.push(c);
continue;
}
if matches!(c, '.' | '!' | '?' | ',') && sentence_start(&out) {
continue;
}
removed = false;
let closing = matches!(
c,
'.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '}' | '”' | '’' | '…'
) || (c == '"'
&& chars.get(index).is_none_or(|next| !next.is_alphanumeric()));
if closing {
trim_end(&mut out, &[',']);
capitalize = false;
} else {
if needs_space(&out) {
out.push(' ');
}
if std::mem::take(&mut capitalize) {
out.extend(c.to_uppercase());
continue;
}
}
}
out.push(c);
}
if removed {
trim_end(&mut out, &[',']);
}
out
}
fn joins_word(c: char) -> bool {
c.is_alphanumeric() || matches!(c, '-' | '\'' | '’')
}
/// Where the filler starting at `start` ends, if one does.
fn filler_at(chars: &[char], start: usize) -> Option<usize> {
if start > 0 && joins_word(chars[start - 1]) {
return None;
}
FILLERS.iter().find_map(|word| {
let end = start + word.len();
let candidate = chars.get(start..end)?;
let matches =
candidate
.iter()
.zip(word.chars())
.enumerate()
.all(|(number, (&c, expected))| {
c == expected || (number == 0 && c == expected.to_ascii_uppercase())
});
let ends = chars.get(end).is_none_or(|&next| !joins_word(next));
(matches && ends).then_some(end)
})
}
/// Where the text continues after a filler ending at `end`, past its own
/// comma or ellipsis.
fn after_filler(chars: &[char], end: usize) -> usize {
let mut index = skip_spaces(chars, end);
match chars.get(index) {
Some(',' | '…') => index += 1,
Some('.') if chars.get(index + 1) == Some(&'.') && chars.get(index + 2) == Some(&'.') => {
index += 3
}
_ => {}
}
skip_spaces(chars, index)
}
fn is_space(c: char) -> bool {
c.is_whitespace() && c != '\n'
}
fn skip_spaces(chars: &[char], mut index: usize) -> usize {
while chars.get(index).is_some_and(|&c| is_space(c)) {
index += 1;
}
index
}
fn trim_end(out: &mut String, also: &[char]) {
while let Some(last) = out.chars().next_back() {
if is_space(last) || also.contains(&last) {
out.pop();
} else {
break;
}
}
}
fn sentence_start(out: &str) -> bool {
out.chars()
.next_back()
.is_none_or(|last| matches!(last, '.' | '!' | '?' | '\n'))
}
fn needs_space(out: &str) -> bool {
let mut reversed = out.chars().rev();
match reversed.next() {
None | Some('\n' | '(' | '[' | '{' | '“' | '‘') => false,
Some('"') => reversed
.next()
.is_some_and(|before| !before.is_whitespace()),
Some(_) => true,
}
}
#[cfg(test)]
mod tests {
use super::{applies_to, apply, strip_if};
#[test]
fn only_english_dictations_apply_the_filter() {
assert!(applies_to("en"));
for language in ["de", "pt", "auto", ""] {
assert!(!applies_to(language), "{language:?}");
}
}
#[test]
fn enabled_english_dictations_strip_their_fillers() {
assert_eq!(
strip_if("Um, please send the draft.", "en", true).as_deref(),
Some("Please send the draft.")
);
}
#[test]
fn every_other_language_or_a_disabled_switch_keeps_the_words() {
for (transcript, language) in [
("Er kommt um acht Uhr.", "de"),
("Eu quero um café.", "pt"),
("Um, please send the draft.", "auto"),
("Um, please send the draft.", ""),
] {
assert_eq!(strip_if(transcript, language, true), None, "{language:?}");
}
assert_eq!(strip_if("Um, please send the draft.", "en", false), None);
}
#[test]
fn fillers_leave_with_their_commas_and_spaces() {
assert_eq!(apply("So, um, we should go."), "So, we should go.");
assert_eq!(apply("I think uh we should go."), "I think we should go.");
assert_eq!(apply("I think, um."), "I think.");
assert_eq!(apply("Well umm... maybe"), "Well maybe");
assert_eq!(apply("It was, er, erm, fine"), "It was, fine");
assert_eq!(apply("Done, uh"), "Done");
assert_eq!(apply("(um, aside)"), "(aside)");
}
#[test]
fn a_filler_that_started_a_sentence_hands_on_its_capital() {
assert_eq!(apply("Um, so I think"), "So I think");
assert_eq!(
apply("Hello. Uh, we left. Ah. Then"),
"Hello. We left. Then"
);
assert_eq!(apply("Uh, um, okay"), "Okay");
}
#[test]
fn line_breaks_are_kept() {
assert_eq!(apply("First line.\nUm, second"), "First line.\nSecond");
assert_eq!(apply("one um\ntwo"), "one\ntwo");
}
#[test]
fn other_words_are_left_alone() {
for text in [
"The umbrella is ahead of the error.",
"Uhura said uh-huh.",
"Take him to the ER.",
"UM is a university.",
"drum, Ahab, ermine",
"Plain text, nothing to do.",
"",
] {
assert_eq!(apply(text), text);
}
}
}