piper-phoneme-streaming 0.1.0

A high-performance Rust library for streaming Text-to-Phoneme (G2P) conversion.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::collections::VecDeque;

use crate::semantic::Language;
use crate::text_expand::ExpandUnit;

const CONTEXT_WINDOW_SIZE: usize = 5;
/// A single unambiguous word with confidence ≥ this triggers an immediate
/// language switch and resets the context window (fast path).
const SINGLE_WORD_SWITCH_THRESHOLD: f64 = 0.80;
/// The context window must reach this confidence before switching language
/// (slow path).  0.70 = 0.5 base + 0.20 hysteresis.
const CONTEXT_SWITCH_THRESHOLD: f64 = 0.70;

/// Trait for language detection backends.
///
/// Implementations receive a context string and return the most likely language
/// plus its confidence score, or `None` if detection fails.
pub(crate) trait LanguageDetector: Send + Sync {
    fn detect(&self, context: &str) -> Option<(Language, f64)>;
}

/// Streaming language detector that wraps a [`LanguageDetector`] backend.
///
/// Takes [`ExpandUnit`] values one at a time and returns the detected
/// [`Language`] for each:
///
/// - `Word` units trigger detection via a sliding context window with
///   hysteresis — language only switches when confidence exceeds 0.5 + 0.20.
/// - `Number` and `Mark` units inherit the current language without detection.
/// - Sentence boundaries (`.`, `?`, `!`) should be signalled via
///   [`reset_context`](Self::reset_context) to clear the sliding window.
pub struct StreamingLanguageDetector {
    detector: Box<dyn LanguageDetector>,
    current_language: Language,
    context_window: VecDeque<String>,
}

impl StreamingLanguageDetector {
    pub(crate) fn new(default_language: Language, detector: Box<dyn LanguageDetector>) -> Self {
        Self {
            detector,
            current_language: default_language,
            context_window: VecDeque::new(),
        }
    }

    /// Build a detector backed by the `lingua` library.
    ///
    /// `languages` must contain at least two languages; `default_language` is
    /// the starting assumption before any text is seen.
    ///
    pub fn with_lingua(languages: &[Language], default_language: Language) -> Self {
        Self::new(default_language, Box::new(LinguaDetector::new(languages)))
    }

    /// Push one expand unit and get back the language for that unit.
    ///
    /// Words run through the detection algorithm; numbers and marks inherit
    /// the current language without running detection.
    pub fn push(&mut self, unit: &ExpandUnit) -> Language {
        match unit {
            ExpandUnit::Word(word) => self.detect_for_word(word),
            ExpandUnit::Number(_) | ExpandUnit::Mark(_) => self.current_language,
        }
    }

    /// Clear the context window on sentence boundaries (`.`, `?`, `!`).
    pub fn reset_context(&mut self) {
        self.context_window.clear();
    }

    fn detect_for_word(&mut self, word: &str) -> Language {
        // Fast path: a single unambiguous word with high confidence triggers an
        // immediate switch and resets the context window so the new language is
        // not dragged back by old context.
        if let Some((word_lang, word_conf)) = self.detector.detect(word)
            && word_lang != self.current_language
            && word_conf >= SINGLE_WORD_SWITCH_THRESHOLD
        {
            self.current_language = word_lang;
            self.context_window.clear();
            self.context_window.push_back(word.to_string());
            return self.current_language;
        }

        // Slow path: accumulate a sliding context window and switch only when
        // the aggregated confidence exceeds the hysteresis threshold.
        self.context_window.push_back(word.to_string());
        if self.context_window.len() > CONTEXT_WINDOW_SIZE {
            self.context_window.pop_front();
        }

        let context: String = self
            .context_window
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>()
            .join(" ");

        if let Some((ctx_lang, ctx_conf)) = self.detector.detect(&context)
            && ctx_lang != self.current_language
            && ctx_conf >= CONTEXT_SWITCH_THRESHOLD
        {
            self.current_language = ctx_lang;
        }

        self.current_language
    }
}

// ---------------------------------------------------------------------------
// lingua backend
// ---------------------------------------------------------------------------

struct LinguaDetector {
    detector: lingua::LanguageDetector,
}

impl LinguaDetector {
    fn new(languages: &[Language]) -> Self {
        let lingua_langs: Vec<lingua::Language> = languages
            .iter()
            .map(|l| match l {
                Language::English => lingua::Language::English,
                Language::Vietnamese => lingua::Language::Vietnamese,
            })
            .collect();
        let detector = lingua::LanguageDetectorBuilder::from_languages(&lingua_langs)
            .with_minimum_relative_distance(0.25)
            .build();
        Self { detector }
    }
}

impl LanguageDetector for LinguaDetector {
    fn detect(&self, context: &str) -> Option<(Language, f64)> {
        let confidences = self.detector.compute_language_confidence_values(context);
        confidences.first().map(|(lingua_lang, confidence)| {
            let lang = match lingua_lang {
                lingua::Language::English => Language::English,
                lingua::Language::Vietnamese => Language::Vietnamese,
            };
            (lang, *confidence)
        })
    }
}

// ---------------------------------------------------------------------------
// Tests
//
// Each test case is: (preferred_language, &[(word, expected_language)])
// Every word is pushed as ExpandUnit::Word; the output language is asserted
// per word so failures point to the exact token where behavior diverges.
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::text_expand::ExpandUnit;
    use Language::{English as EN, Vietnamese as VI};

    fn det(default: Language) -> StreamingLanguageDetector {
        StreamingLanguageDetector::with_lingua(&[EN, VI], default)
    }

    /// Core helper: push each `(word, expected_language)` pair and assert per step.
    fn check(preferred: Language, steps: &[(&str, Language)]) {
        let mut d = det(preferred);
        for (word, expected) in steps {
            let got = d.push(&ExpandUnit::Word(word.to_string()));
            assert_eq!(
                got, *expected,
                "preferred={preferred:?}  word={word:?}  expected={expected:?}  got={got:?}"
            );
        }
    }

    // -------------------------------------------------------------------------
    // Pure-language — window fills with one language, never switches
    // -------------------------------------------------------------------------

    #[test]
    fn case_pure_english() {
        check(
            EN,
            &[
                ("the", EN),   // window: [the]
                ("quick", EN), // window: [the, quick]
                ("brown", EN), // window: [the, quick, brown]
                ("fox", EN),   // window: [the, quick, brown, fox]
                ("jumps", EN), // window: [the, quick, brown, fox, jumps]
            ],
        );
    }

    #[test]
    fn case_pure_vietnamese() {
        check(
            VI,
            &[
                ("xin", VI),  // window: [xin]
                ("chào", VI), // window: [xin, chào]
                ("bạn", VI),  // window: [xin, chào, bạn]
                ("tên", VI),  // window: [xin, chào, bạn, tên]
                ("", VI),   // window: [xin, chào, bạn, tên, là]
            ],
        );
    }

    // -------------------------------------------------------------------------
    // EN → VI transition
    //
    // "chào" has single-word VI confidence = 1.0 ≥ 0.80 → fast switch at
    // first clear Vietnamese word.  Context resets; VI stable thereafter.
    // -------------------------------------------------------------------------

    #[test]
    fn case_en_to_vi_transition() {
        check(
            EN,
            &[
                // ── build EN context ──
                ("the", EN),   // window: [the]
                ("quick", EN), // window: [the, quick]
                ("brown", EN), // window: [the, quick, brown]
                ("fox", EN),   // window: [the, quick, brown, fox]
                ("jumps", EN), // window: [the, quick, brown, fox, jumps]
                // ── first VI word: high single-word confidence → fast switch ──
                ("chào", VI), // fast path: VI 1.0 ≥ 0.80 → switch; context reset to [chào]
                // ── settled in VI ──
                ("bạn", VI),
                ("tôi", VI),
                ("muốn", VI),
                ("học", VI),
            ],
        );
    }

    // -------------------------------------------------------------------------
    // VI → EN transition
    //
    // "the" has single-word EN confidence ≥ 0.80 → fast switch immediately.
    // Context resets to ["the"]; EN stable for all following EN words.
    // -------------------------------------------------------------------------

    #[test]
    fn case_vi_to_en_transition() {
        check(
            VI,
            &[
                // ── build VI context ──
                ("xin", VI),
                ("chào", VI),
                ("bạn", VI),
                ("tên", VI),
                ("", VI),
                // ── first EN word: fast switch ──
                ("the", EN), // fast path: EN ≥ 0.80 → switch; context reset to [the]
                // ── settled in EN ──
                ("quick", EN),
                ("brown", EN),
                ("fox", EN),
                ("jumps", EN),
            ],
        );
    }

    // -------------------------------------------------------------------------
    // Reset clears context — next language dominates immediately
    //
    // With an empty window after reset, even a single unambiguous word exceeds
    // the 0.70 threshold and switches language.
    // -------------------------------------------------------------------------

    #[test]
    fn case_reset_vi_context_then_en() {
        let mut d = det(VI);
        for w in &["xin", "chào", "bạn"] {
            d.push(&ExpandUnit::Word(w.to_string()));
        }
        d.reset_context();
        // Empty window → single EN word immediately switches
        for (word, expected) in &[
            ("the", EN), // window: [the]   — triggers switch on first word
            ("quick", EN),
            ("brown", EN),
            ("fox", EN),
            ("jumps", EN),
        ] {
            let got = d.push(&ExpandUnit::Word(word.to_string()));
            assert_eq!(
                got, *expected,
                "after reset  word={word:?}  expected={expected:?}  got={got:?}"
            );
        }
    }

    // -------------------------------------------------------------------------
    // Numbers and marks — always inherit current language, never enter window
    // -------------------------------------------------------------------------

    #[test]
    fn case_number_inherits_en() {
        let mut d = det(EN);
        assert_eq!(d.push(&ExpandUnit::Number("42".into())), EN);
        assert_eq!(d.push(&ExpandUnit::Number("0".into())), EN);
    }

    #[test]
    fn case_number_inherits_vi() {
        let mut d = det(VI);
        assert_eq!(d.push(&ExpandUnit::Number("100".into())), VI);
        assert_eq!(d.push(&ExpandUnit::Number("1000".into())), VI);
    }

    #[test]
    fn case_mark_inherits_en() {
        let mut d = det(EN);
        assert_eq!(d.push(&ExpandUnit::Mark(' ')), EN);
        assert_eq!(d.push(&ExpandUnit::Mark(',')), EN);
        assert_eq!(d.push(&ExpandUnit::Mark('.')), EN);
    }

    #[test]
    fn case_mark_inherits_vi() {
        let mut d = det(VI);
        assert_eq!(d.push(&ExpandUnit::Mark(' ')), VI);
        assert_eq!(d.push(&ExpandUnit::Mark(',')), VI);
    }

    /// Numbers do not enter the context window; they inherit the current language.
    /// With fast-path switching, "chào" switches to VI immediately.
    #[test]
    fn case_number_does_not_affect_transition_timing() {
        let mut d = det(EN);
        for w in &["the", "quick", "brown", "fox", "jumps"] {
            d.push(&ExpandUnit::Word(w.to_string()));
        }
        assert_eq!(d.push(&ExpandUnit::Word("chào".into())), VI); // fast path: VI 1.0 → switch
        assert_eq!(d.push(&ExpandUnit::Number("100".into())), VI); // inherits VI
        assert_eq!(d.push(&ExpandUnit::Word("bạn".into())), VI); // still VI
        assert_eq!(d.push(&ExpandUnit::Number("7".into())), VI); // inherits VI
    }

    /// Marks behave identically — no window effect.
    #[test]
    fn case_mark_does_not_affect_transition_timing() {
        let mut d = det(EN);
        for w in &["the", "quick", "brown", "fox", "jumps"] {
            d.push(&ExpandUnit::Word(w.to_string()));
        }
        assert_eq!(d.push(&ExpandUnit::Word("chào".into())), VI); // fast path: VI 1.0 → switch
        assert_eq!(d.push(&ExpandUnit::Mark(',')), VI); // inherits VI
        assert_eq!(d.push(&ExpandUnit::Word("bạn".into())), VI); // still VI
    }

    // -------------------------------------------------------------------------
    // Mixed sentences — fast path switches on clear single-word signals
    //
    // "trong tiếng anh hello world có nghĩa là xin chào"
    // = "in English, 'hello world' means 'xin chào'"
    //
    // "hello" (EN 0.886) and "world" (EN 0.851) both exceed the fast-path
    // threshold → immediate EN switch.  "có" exceeds the VI threshold →
    // fast switch back to VI.
    // -------------------------------------------------------------------------

    #[test]
    fn case_vi_sentence_with_embedded_en_words() {
        check(
            VI,
            &[
                // ── pure VI context ──
                ("trong", VI),
                ("tiếng", VI),
                ("anh", VI),
                // ── embedded English words — fast-path switches to EN ──
                ("hello", EN), // fast path: EN 0.886 ≥ 0.80 → switch; context reset to [hello]
                ("world", EN), // stays EN (EN 0.851 ≥ 0.80 but already EN)
                // ── back to Vietnamese — fast-path switches back ──
                ("", VI), // fast path: VI ≥ 0.80 → switch back
                ("nghĩa", VI),
                ("", VI),
                ("xin", VI),
                ("chào", VI),
            ],
        );
    }

    /// A single Vietnamese word with high single-word confidence fast-switches
    /// out of EN, and the next clear English word fast-switches back.
    #[test]
    fn case_en_sentence_with_single_embedded_vi_word() {
        check(
            EN,
            &[
                ("the", EN),
                ("quick", EN),
                ("brown", EN),
                ("fox", EN),
                // ── single VI word: VI 1.0 ≥ 0.80 → fast switch ──
                ("chào", VI), // fast path: VI 1.0 → switch; context reset to [chào]
                // ── next clear EN word fast-switches back ──
                ("jumps", EN), // fast path: EN ≥ 0.80 → switch back
                ("over", EN),
                ("lazy", EN),
                ("dog", EN),
                ("today", EN),
            ],
        );
    }

    // -------------------------------------------------------------------------
    // Numbers in mixed-language context
    // -------------------------------------------------------------------------

    /// "giá 100 đồng" — number throughout inherits VI; reset then switches EN.
    #[test]
    fn case_number_in_vi_then_reset_to_en() {
        let mut d = det(VI);
        for w in &["giá", "tiền", ""] {
            d.push(&ExpandUnit::Word(w.to_string()));
        }
        assert_eq!(d.push(&ExpandUnit::Number("100".into())), VI);
        d.reset_context();
        assert_eq!(d.push(&ExpandUnit::Word("the".into())), EN); // immediate switch after reset
        assert_eq!(d.push(&ExpandUnit::Word("price".into())), EN);
    }

    /// Number during the EN→VI transition: with fast-path, "chào" switches
    /// immediately and the number inherits VI right away.
    #[test]
    fn case_number_tracks_language_through_en_to_vi_transition() {
        let mut d = det(EN);
        for w in &["the", "quick", "brown", "fox", "jumps"] {
            d.push(&ExpandUnit::Word(w.to_string()));
        }
        assert_eq!(d.push(&ExpandUnit::Word("chào".into())), VI); // fast path: VI 1.0 → switch
        assert_eq!(d.push(&ExpandUnit::Number("42".into())), VI); // inherits VI immediately
        assert_eq!(d.push(&ExpandUnit::Word("bạn".into())), VI); // still VI
        assert_eq!(d.push(&ExpandUnit::Number("7".into())), VI); // inherits VI
    }
}