diff --git a/crates/core/src/call_view.rs b/crates/core/src/call_view.rs index b2b4382..3bcf321 100644 --- a/crates/core/src/call_view.rs +++ b/crates/core/src/call_view.rs @@ -179,14 +179,24 @@ pub struct CallDetail { /// Whether retention deleted the call's audio (as opposed to the files /// being missing for another reason). pub audio_deleted: bool, + /// The call's language, or the last-used one for a call without its + /// own yet ([`preselect_language`]): what the language picker next to + /// "Transkribieren"/"Erneut versuchen" starts on. + pub language: String, +} + +/// The language the picker starts on for `call`: the one it was +/// transcribed in, else `last_used` (the settings' `language`). +pub fn preselect_language(call: &Call, last_used: &str) -> String { + call.language.clone().unwrap_or_else(|| last_used.to_string()) } impl CallDetail { /// `segments` are the stored ones; `merges` the stored speaker merges /// (source key → target key); `backend_url` is this build's backend - /// (see [`CallListItem::of`]); `language` is the call-language setting - /// in effect now (not necessarily what the call was transcribed with), - /// used to apply [`correct_segment_text`] to each segment's text — + /// (see [`CallListItem::of`]); `last_used_language` is the settings' + /// `language` (see [`preselect_language`]). The call's language picks + /// the [`correct_segment_text`] applied to each segment's text — /// non-destructively, so it also corrects transcripts from before the /// correction existed. pub fn of( @@ -195,8 +205,11 @@ impl CallDetail { speaker_names: BTreeMap, merges: &BTreeMap, backend_url: &str, - language: &str, + last_used_language: &str, ) -> Self { + // A call with segments always has its language (see the store's + // schema v5); the fallback only covers a call not transcribed yet. + let language = preselect_language(call, last_used_language); let segments = merge::apply_merges(segments, merges) .iter() .map(|segment| SegmentView { @@ -204,7 +217,7 @@ impl CallDetail { label: merge::label(&segment.speaker, &speaker_names), start: segment.start, end: segment.end, - text: correct_segment_text(&segment.text, language), + text: correct_segment_text(&segment.text, &language), }) .collect(); Self { @@ -219,6 +232,7 @@ impl CallDetail { audio_available: false, audio_deleted: call.audio_deleted, speaker_names, + language, } } } @@ -658,19 +672,22 @@ mod tests { "uploading": false, "audio_available": false, "audio_deleted": false, + "language": "de", }) ); } + /// With German as the last-used language (what matters for a call + /// without its own, see `CallDetail::of`). fn detail_with(c: &Call, segments: &[Segment], names: &[(&str, &str)]) -> CallDetail { - detail_with_language(c, segments, names, "de") + detail_last_used(c, segments, names, "de") } - fn detail_with_language( + fn detail_last_used( c: &Call, segments: &[Segment], names: &[(&str, &str)], - language: &str, + last_used_language: &str, ) -> CallDetail { CallDetail::of( c, @@ -678,7 +695,7 @@ mod tests { keys(names), &keys(&[("remote:2", "remote:1")]), BACKEND, - language, + last_used_language, ) } @@ -758,14 +775,38 @@ mod tests { #[test] fn corrections_are_not_applied_outside_german_calls() { - let c = call(Some(at(10, 15, 0))); + let mut c = call(Some(at(10, 15, 0))); + c.language = Some("en".into()); let segments = [segment(Speaker::Me, 0.0, "Cheers!")]; - let detail = detail_with_language(&c, &segments, &[], "en"); + // German was used last, but this call is English. + let detail = detail_last_used(&c, &segments, &[], "de"); assert_eq!(detail.segments[0].text, "Cheers!"); } + #[test] + fn corrections_follow_the_calls_language_not_the_last_used_one() { + let mut c = call(Some(at(10, 15, 0))); + c.language = Some("de".into()); + let segments = [segment(Speaker::Me, 0.0, "Cheers!")]; + + let detail = detail_last_used(&c, &segments, &[], "en"); + + assert_eq!(detail.segments[0].text, "Tschüss!"); + assert_eq!(detail.language, "de"); + } + + #[test] + fn the_preselected_language_is_the_calls_own_else_the_last_used() { + let mut c = call(None); + assert_eq!(preselect_language(&c, "fr"), "fr"); + + c.language = Some("it".into()); + assert_eq!(preselect_language(&c, "fr"), "it"); + assert_eq!(detail_last_used(&c, &[], &[], "fr").language, "it"); + } + #[test] fn the_upload_text_uses_the_corrected_segment_text() { let c = call(Some(at(10, 46, 0))); diff --git a/crates/core/src/pipeline.rs b/crates/core/src/pipeline.rs index 7d5765f..cf14b9c 100644 --- a/crates/core/src/pipeline.rs +++ b/crates/core/src/pipeline.rs @@ -22,7 +22,7 @@ use crate::transcribe::{TranscribeError, TranscribeOptions, Transcriber}; /// Settings a pipeline run uses. #[derive(Debug, Clone, PartialEq)] pub struct PipelineOptions { - /// ISO 639-1 code, or `"auto"`. + /// The call's language, one of [`crate::settings::CALL_LANGUAGES`]. pub language: String, pub deepscript_model: String, /// Custom words/names submitted with both tracks (DeepScript's diff --git a/crates/core/src/settings.rs b/crates/core/src/settings.rs index 575fee4..7470eaf 100644 --- a/crates/core/src/settings.rs +++ b/crates/core/src/settings.rs @@ -35,6 +35,20 @@ pub const RETENTION_DAYS: RangeInclusive = 1..=3650; /// default on load (see [`Settings::load`]) rather than failing. pub const DEEPSCRIPT_MODELS: [&str; 2] = ["premium", "premium_jade"]; +/// The languages (ISO 639-1) a call can be transcribed in, chosen per call +/// when its transcription is started. All are in DeepScript's +/// `GET /v1/languages`; its `"auto"` is not offered. +pub const CALL_LANGUAGES: [&str; 5] = ["de", "en", "es", "fr", "it"]; + +/// Checks a call language chosen in the UI; the error is shown as is. +pub fn check_call_language(language: &str) -> Result<(), String> { + if CALL_LANGUAGES.contains(&language) { + Ok(()) + } else { + Err(format!("Unbekannte Sprache: {language}")) + } +} + /// Default custom-vocabulary terms: short standalone German words/names /// DeepScript otherwise tends to mistranscribe or drop. const DEFAULT_VOCABULARY: [&str; 5] = ["Tschüss", "Perfekt", "Servus", "Moin", "Euvino"]; @@ -56,6 +70,9 @@ pub struct Settings { pub allowlist: Vec, pub mic_device: Option, pub output_device: Option, + /// The language of the transcription started last (one of + /// [`CALL_LANGUAGES`]), preselected for the next call. Set by the app + /// when a transcription is started, not by the settings page. pub language: String, pub deepscript_model: String, /// Custom words/names submitted with every transcription (DeepScript's @@ -146,12 +163,14 @@ impl Settings { } /// The settings after the user saved `edited` over these: normalized - /// (see [`normalize_vocabulary`]), checked, with the backend login - /// (which only the app changes) kept, so a settings page loaded before - /// a login cannot undo it. + /// (see [`normalize_vocabulary`]), checked, with the backend login and + /// the last-used language (which only the app changes) kept, so a + /// settings page loaded before a login or a transcription cannot undo + /// them. pub fn edited(&self, mut edited: Settings) -> Result { edited.vocabulary = normalize_vocabulary(edited.vocabulary); edited.validate()?; + edited.language = self.language.clone(); edited.backend_username = self.backend_username.clone(); edited.backend_key_expires_at = self.backend_key_expires_at; edited.backend_key_base_url = self.backend_key_base_url.clone(); @@ -162,8 +181,9 @@ impl Settings { /// defaults; fields missing from the file take their default value. A /// `deepscript_model` outside [`DEEPSCRIPT_MODELS`] (e.g. an older /// file's `"standard"`) falls back to the default instead of failing, and - /// so does a `retention_days` outside [`RETENTION_DAYS`]; `vocabulary` - /// is normalized (see [`normalize_vocabulary`]). + /// so does a `retention_days` outside [`RETENTION_DAYS`] and a + /// `language` outside [`CALL_LANGUAGES`]; `vocabulary` is normalized + /// (see [`normalize_vocabulary`]). pub fn load(path: &Path) -> Settings { let mut settings: Settings = fs::read_to_string(path) .ok() @@ -172,6 +192,9 @@ impl Settings { if !DEEPSCRIPT_MODELS.contains(&settings.deepscript_model.as_str()) { settings.deepscript_model = Settings::default().deepscript_model; } + if check_call_language(&settings.language).is_err() { + settings.language = Settings::default().language; + } // 0 would delete all audio on the next pass. if !RETENTION_DAYS.contains(&settings.retention_days) { settings.retention_days = Settings::default().retention_days; @@ -289,6 +312,51 @@ mod tests { ); } + #[test] + fn the_five_call_languages_are_accepted() { + for language in CALL_LANGUAGES { + assert_eq!(check_call_language(language), Ok(()), "{language}"); + } + assert_eq!(CALL_LANGUAGES, ["de", "en", "es", "fr", "it"]); + } + + #[test] + fn another_call_language_is_refused() { + assert_eq!( + check_call_language("auto"), + Err("Unbekannte Sprache: auto".to_string()) + ); + assert_eq!( + check_call_language("pt"), + Err("Unbekannte Sprache: pt".to_string()) + ); + } + + #[test] + fn an_unknown_stored_language_falls_back_to_the_default() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.json"); + fs::write(&path, r#"{"language": "auto"}"#).unwrap(); + assert_eq!(Settings::load(&path).language, "de"); + + fs::write(&path, r#"{"language": "it"}"#).unwrap(); + assert_eq!(Settings::load(&path).language, "it"); + } + + #[test] + fn an_edit_keeps_the_last_used_language_which_only_the_app_changes() { + let current = Settings { + language: "fr".into(), + ..Settings::default() + }; + let edited = Settings { + language: "de".into(), + ..Settings::default() + }; + + assert_eq!(current.edited(edited).unwrap().language, "fr"); + } + fn logged_in() -> Settings { Settings { backend_username: Some("hinrichs".into()),