A free, open source, and extensible speech-to-text application that works completely offline.
fix: keep the sentence capital after removing a leading filler (#2157)
Aktan Azateb49dc02acfe
diff --git a/src-tauri/src/audio_toolkit/text.rs b/src-tauri/src/audio_toolkit/text.rsindex 5224814d..40686f03 100644--- a/src-tauri/src/audio_toolkit/text.rs+++ b/src-tauri/src/audio_toolkit/text.rs@@ -361,6 +361,49 @@ fn collapse_stutters(text: &str) -> String {result.join(" ")}+/// Whether a word appended to `kept` would open a sentence: nothing but+/// whitespace so far, or the last visible character ends a sentence.+fn opens_sentence(kept: &str) -> bool {+ kept.trim_end()+ .chars()+ .next_back()+ .is_none_or(|c| matches!(c, '.' | '!' | '?' | '…'))+}++/// Appends `segment` to `kept`. While `capital_owed` is set, the first+/// alphanumeric character of `segment` is uppercased and the debt is settled.+fn push_restoring_capital(kept: &mut String, segment: &str, capital_owed: &mut bool) {+ if *capital_owed {+ if let Some((index, first)) = segment.char_indices().find(|(_, c)| c.is_alphanumeric()) {+ *capital_owed = false;+ kept.push_str(&segment[..index]);+ kept.extend(first.to_uppercase());+ kept.push_str(&segment[index + first.len_utf8()..]);+ return;+ }+ }+ kept.push_str(segment);+}++/// Deletes every match of one filler pattern. A capitalized filler that opened+/// a sentence hands its capital to the word that takes its place, so+/// "Um, so I think" becomes "So I think" rather than "so I think".+fn remove_filler_matches(text: &str, pattern: &Regex) -> String {+ let mut kept = String::with_capacity(text.len());+ let mut resume = 0;+ let mut capital_owed = false;++ for filler in pattern.find_iter(text) {+ push_restoring_capital(&mut kept, &text[resume..filler.start()], &mut capital_owed);+ let capitalized = filler.as_str().starts_with(char::is_uppercase);+ capital_owed |= capitalized && opens_sentence(&kept);+ resume = filler.end();+ }+ push_restoring_capital(&mut kept, &text[resume..], &mut capital_owed);++ kept+}+/// Removes filler words from transcription output when enabled.////// Built-in removal is two-tiered: [`UNIVERSAL_FILLER_WORDS`] apply regardless@@ -411,7 +454,7 @@ pub fn remove_filler_words(// Remove filler wordslet mut filtered = text.to_string();for pattern in &patterns {- filtered = pattern.replace_all(&filtered, "").to_string();+ filtered = remove_filler_matches(&filtered, pattern);}filtered@@ -506,7 +549,7 @@ mod tests {fn test_filter_filler_words_case_insensitive() {let text = "UHM this is UH a test";let result = filter_transcription_output(text, "en", &None);- assert_eq!(result, "this is a test");+ assert_eq!(result, "This is a test");}#[test]@@ -534,7 +577,20 @@ mod tests {fn test_filter_combined() {let text = " Uhm, so I was, uh, thinking about this ";let result = filter_transcription_output(text, "en", &None);- assert_eq!(result, "so I was, thinking about this");+ assert_eq!(result, "So I was, thinking about this");+ }++ #[test]+ fn test_filter_leading_filler_keeps_sentence_capital() {+ let result = filter_transcription_output("Um, so I think we should ship it.", "en", &None);+ assert_eq!(result, "So I think we should ship it.");++ let result = filter_transcription_output("That works. Um, let me check.", "en", &None);+ assert_eq!(result, "That works. Let me check.");++ // Mid-sentence there is no capital to hand over.+ let result = filter_transcription_output("He said, Um, not today.", "en", &None);+ assert_eq!(result, "He said, not today.");}#[test]