shabdakosh 2.0.0

shabdakosh — Pronunciation dictionary with ARPABET/CMUdict support for svara phonemes
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Grapheme-to-phoneme (G2P) model integration.
//!
//! Defines the [`G2PModel`] trait for neural or rule-based G2P engines,
//! [`G2PResult`] for predictions with confidence scores, and [`FallbackDict`]
//! for chaining dictionary lookup with G2P fallback.
//!
//! # Architecture
//!
//! shabdakosh provides the trait; model crates provide implementations.
//! This follows the pattern used by gruut and eSpeak-ng:
//!
//! ```text
//! lookup("word") → user overlay → base dictionary → G2P model
//! ```
//!
//! # Example
//!
//! ```rust
//! use shabdakosh::dictionary::g2p::{G2PModel, G2PResult, FallbackDict, LookupSource};
//! use shabdakosh::PronunciationDict;
//! use svara::phoneme::Phoneme;
//!
//! struct DummyModel;
//!
//! impl G2PModel for DummyModel {
//!     fn predict(&self, word: &str) -> Option<G2PResult> {
//!         Some(G2PResult::new(vec![Phoneme::VowelSchwa], 0.5))
//!     }
//! }
//!
//! let dict = PronunciationDict::english_minimal();
//! let fallback = dict.with_fallback(DummyModel);
//!
//! // Known word — comes from dictionary
//! let (phonemes, source) = fallback.lookup_with_source("hello").unwrap();
//! assert!(matches!(source, LookupSource::BaseDictionary));
//!
//! // Unknown word — falls through to G2P model
//! let (phonemes, source) = fallback.lookup_with_source("xyzzy").unwrap();
//! assert!(matches!(source, LookupSource::G2PModel { .. }));
//! ```

use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use svara::phoneme::Phoneme;

use super::PronunciationDict;
use super::entry::DictEntry;

/// Result of a G2P model prediction.
///
/// Contains the predicted phoneme sequence and a confidence score
/// indicating how reliable the prediction is.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct G2PResult {
    phonemes: Vec<Phoneme>,
    confidence: f32,
}

impl G2PResult {
    /// Creates a new G2P result with the given phonemes and confidence.
    ///
    /// Confidence should be in the range 0.0 (no confidence) to 1.0 (certain).
    #[must_use]
    pub fn new(phonemes: Vec<Phoneme>, confidence: f32) -> Self {
        Self {
            phonemes,
            confidence,
        }
    }

    /// Returns the predicted phoneme sequence.
    #[must_use]
    pub fn phonemes(&self) -> &[Phoneme] {
        &self.phonemes
    }

    /// Returns the confidence score (0.0–1.0).
    #[must_use]
    pub fn confidence(&self) -> f32 {
        self.confidence
    }

    /// Consumes the result and returns the phoneme vector.
    #[must_use]
    pub fn into_phonemes(self) -> Vec<Phoneme> {
        self.phonemes
    }
}

/// Trait for grapheme-to-phoneme models.
///
/// Implementations convert a written word into a predicted phoneme sequence.
/// shabdakosh provides this trait; model crates (neural, FST, rule-based)
/// provide implementations.
///
/// # Implementors
///
/// - Neural models (e.g., DeepPhonemizer, OpenPhonemizer)
/// - WFST models (e.g., Phonetisaurus)
/// - Rule-based engines (e.g., eSpeak letter-to-sound rules)
pub trait G2PModel: Send + Sync {
    /// Predicts the pronunciation of a word.
    ///
    /// Returns `None` if the model cannot produce a prediction for this word.
    fn predict(&self, word: &str) -> Option<G2PResult>;
}

/// Indicates where a lookup result came from.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum LookupSource {
    /// Result came from the user overlay.
    UserOverlay,
    /// Result came from the base dictionary.
    BaseDictionary,
    /// Result came from a G2P model prediction.
    G2PModel {
        /// Confidence score of the prediction (0.0–1.0).
        confidence: f32,
    },
}

/// A pronunciation dictionary with G2P model fallback.
///
/// Wraps a [`PronunciationDict`] and a [`G2PModel`] implementation.
/// Lookups try: user overlay → base dictionary → G2P model.
///
/// The model is runtime-only and not serialized. Use [`dict`](Self::dict)
/// and [`dict_mut`](Self::dict_mut) to access the underlying dictionary
/// for serialization.
pub struct FallbackDict<M: G2PModel> {
    dict: PronunciationDict,
    model: M,
}

impl<M: G2PModel> FallbackDict<M> {
    /// Creates a new fallback dictionary from a dictionary and model.
    #[must_use]
    pub fn new(dict: PronunciationDict, model: M) -> Self {
        Self { dict, model }
    }

    /// Returns a reference to the underlying dictionary.
    #[must_use]
    pub fn dict(&self) -> &PronunciationDict {
        &self.dict
    }

    /// Returns a mutable reference to the underlying dictionary.
    pub fn dict_mut(&mut self) -> &mut PronunciationDict {
        &mut self.dict
    }

    /// Returns a reference to the G2P model.
    #[must_use]
    pub fn model(&self) -> &M {
        &self.model
    }

    /// Looks up the primary pronunciation of a word.
    ///
    /// Tries: user overlay → base dictionary → G2P model.
    /// For dictionary results, returns the phoneme slice directly.
    /// For G2P results, the phonemes are owned — use [`lookup_with_source`](Self::lookup_with_source)
    /// to get full details.
    #[must_use]
    pub fn lookup(&self, word: &str) -> Option<Vec<Phoneme>> {
        if let Some(phonemes) = self.dict.lookup(word) {
            return Some(phonemes.to_vec());
        }
        self.model
            .predict(&word.to_lowercase())
            .map(|r| r.into_phonemes())
    }

    /// Looks up a word and returns both the phonemes and the source.
    ///
    /// The lookup chain is: user overlay → base dictionary → G2P model.
    #[must_use]
    pub fn lookup_with_source(&self, word: &str) -> Option<(Vec<Phoneme>, LookupSource)> {
        let key = alloc::string::ToString::to_string(&word.to_lowercase());

        // Check user overlay first.
        if let Some(entry) = self.dict.user_entries().get(&key) {
            return Some((entry.primary_phonemes().to_vec(), LookupSource::UserOverlay));
        }

        // Check base dictionary.
        if let Some(entry) = self.dict.entries().get(&key) {
            return Some((
                entry.primary_phonemes().to_vec(),
                LookupSource::BaseDictionary,
            ));
        }

        // Fall through to G2P model.
        let result = self.model.predict(&key)?;
        let confidence = result.confidence();
        Some((
            result.into_phonemes(),
            LookupSource::G2PModel { confidence },
        ))
    }

    /// Looks up the full dictionary entry for a word.
    ///
    /// Only checks the dictionary layers (user overlay + base), not the G2P model.
    /// Use [`lookup_with_source`](Self::lookup_with_source) for G2P fallback.
    #[must_use]
    pub fn lookup_entry(&self, word: &str) -> Option<&DictEntry> {
        self.dict.lookup_entry(word)
    }

    /// Promotes a G2P prediction to the user overlay.
    ///
    /// If the word is already in the dictionary (user or base), this is a no-op.
    /// Returns `true` if a prediction was promoted.
    pub fn promote_prediction(&mut self, word: &str) -> bool {
        // Already in dictionary — nothing to promote.
        if self.dict.lookup_entry(word).is_some() {
            return false;
        }

        let key = alloc::string::ToString::to_string(&word.to_lowercase());
        if let Some(result) = self.model.predict(&key) {
            self.dict.insert_user(word, result.phonemes());
            true
        } else {
            false
        }
    }

    /// Promotes a G2P prediction only if its confidence meets the threshold.
    ///
    /// Returns `true` if a prediction was promoted.
    pub fn promote_if_confident(&mut self, word: &str, threshold: f32) -> bool {
        if self.dict.lookup_entry(word).is_some() {
            return false;
        }

        let key = alloc::string::ToString::to_string(&word.to_lowercase());
        if let Some(result) = self.model.predict(&key)
            && result.confidence() >= threshold
        {
            self.dict.insert_user(word, result.phonemes());
            return true;
        }
        false
    }

    /// Consumes the fallback dictionary and returns the inner dictionary and model.
    #[must_use]
    pub fn into_parts(self) -> (PronunciationDict, M) {
        (self.dict, self.model)
    }
}

impl<M: G2PModel + core::fmt::Debug> core::fmt::Debug for FallbackDict<M> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("FallbackDict")
            .field("dict", &self.dict)
            .field("model", &self.model)
            .finish()
    }
}

impl<M: G2PModel + Clone> Clone for FallbackDict<M> {
    fn clone(&self) -> Self {
        Self {
            dict: self.dict.clone(),
            model: self.model.clone(),
        }
    }
}

// --- Phonetisaurus FST integration ---

/// A Weighted Finite-State Transducer (WFST) based G2P model wrapper.
///
/// This struct provides the integration point for Phonetisaurus-style
/// WFST models. It is lighter than neural models and offers good accuracy
/// for many languages.
///
/// # Usage
///
/// Phonetisaurus models produce ARPABET or IPA output which must be
/// converted to svara phonemes. Configure the notation via [`FstNotation`].
///
/// ```rust,no_run
/// use shabdakosh::dictionary::g2p::{FstModel, FstNotation, G2PModel};
///
/// // Load a pre-trained FST model file.
/// let model = FstModel::new("/path/to/model.fst", FstNotation::Arpabet);
///
/// // Use with a fallback dictionary:
/// // let fallback = dict.with_fallback(model);
/// ```
///
/// # Implementation
///
/// `FstModel` implements [`G2PModel`] by:
/// 1. Passing the input word through the WFST
/// 2. Converting the output symbols to svara phonemes (ARPABET or IPA)
/// 3. Deriving a confidence score from the path weight
///
/// The actual FST engine is **not bundled** — this wrapper calls out to
/// an external library or binary. See the `phonetisaurus-g2p-rs` crate
/// (when available) for a pure-Rust implementation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FstModel {
    model_path: alloc::string::String,
    notation: FstNotation,
}

/// Output notation of an FST model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum FstNotation {
    /// Model outputs ARPABET symbols (e.g., "K AE1 T").
    Arpabet,
    /// Model outputs IPA symbols (e.g., "kæt").
    Ipa,
}

impl FstModel {
    /// Creates a new FST model wrapper pointing to a model file.
    ///
    /// The model file is not loaded until [`predict`](G2PModel::predict) is called.
    #[must_use]
    pub fn new(model_path: &str, notation: FstNotation) -> Self {
        Self {
            model_path: alloc::string::ToString::to_string(model_path),
            notation,
        }
    }

    /// Returns the path to the model file.
    #[must_use]
    pub fn model_path(&self) -> &str {
        &self.model_path
    }

    /// Returns the output notation of this model.
    #[must_use]
    pub fn notation(&self) -> FstNotation {
        self.notation
    }
}

impl G2PModel for FstModel {
    /// Predicts pronunciation using the FST model.
    ///
    /// **Current status:** This is a stub that always returns `None`.
    /// Wire in an actual FST engine (e.g., `phonetisaurus-g2p-rs`) to
    /// provide real predictions.
    fn predict(&self, _word: &str) -> Option<G2PResult> {
        // Stub: when a real FST engine is available, this would:
        // 1. Run the word through the WFST at self.model_path
        // 2. Parse output symbols according to self.notation
        // 3. Convert to svara phonemes via arpabet:: or ipa:: module
        // 4. Derive confidence from path weight (e.g., exp(-cost))
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A mock G2P model for testing.
    #[derive(Debug, Clone)]
    struct MockG2PModel {
        /// Fixed confidence for all predictions.
        confidence: f32,
    }

    impl MockG2PModel {
        fn new(confidence: f32) -> Self {
            Self { confidence }
        }
    }

    impl G2PModel for MockG2PModel {
        fn predict(&self, _word: &str) -> Option<G2PResult> {
            Some(G2PResult::new(
                alloc::vec![Phoneme::VowelSchwa],
                self.confidence,
            ))
        }
    }

    /// A model that always returns None.
    #[derive(Debug, Clone)]
    struct NoopModel;

    impl G2PModel for NoopModel {
        fn predict(&self, _word: &str) -> Option<G2PResult> {
            None
        }
    }

    #[test]
    fn test_g2p_result_new() {
        let result = G2PResult::new(alloc::vec![Phoneme::PlosiveK], 0.85);
        assert_eq!(result.phonemes(), &[Phoneme::PlosiveK]);
        assert_eq!(result.confidence(), 0.85);
    }

    #[test]
    fn test_g2p_result_into_phonemes() {
        let result = G2PResult::new(alloc::vec![Phoneme::PlosiveK, Phoneme::VowelAsh], 0.9);
        let phonemes = result.into_phonemes();
        assert_eq!(phonemes, alloc::vec![Phoneme::PlosiveK, Phoneme::VowelAsh]);
    }

    #[test]
    fn test_g2p_result_serde_roundtrip() {
        let result = G2PResult::new(alloc::vec![Phoneme::PlosiveK, Phoneme::VowelAsh], 0.75);
        let json = serde_json::to_string(&result).unwrap();
        let result2: G2PResult = serde_json::from_str(&json).unwrap();
        assert_eq!(result, result2);
    }

    #[test]
    fn test_lookup_source_serde_roundtrip() {
        let sources = [
            LookupSource::UserOverlay,
            LookupSource::BaseDictionary,
            LookupSource::G2PModel { confidence: 0.8 },
        ];
        for source in &sources {
            let json = serde_json::to_string(source).unwrap();
            let source2: LookupSource = serde_json::from_str(&json).unwrap();
            assert_eq!(source, &source2);
        }
    }

    #[test]
    fn test_fallback_dict_lookup_from_dict() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        // "hello" is in the minimal dictionary.
        let phonemes = fallback.lookup("hello");
        assert!(phonemes.is_some());
    }

    #[test]
    fn test_fallback_dict_lookup_from_g2p() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        // "xyzzy" is not in any dictionary.
        let phonemes = fallback.lookup("xyzzy");
        assert!(phonemes.is_some());
        assert_eq!(phonemes.unwrap(), alloc::vec![Phoneme::VowelSchwa]);
    }

    #[test]
    fn test_fallback_dict_lookup_none_when_no_model_result() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, NoopModel);

        assert!(fallback.lookup("xyzzy").is_none());
    }

    #[test]
    fn test_lookup_with_source_user_overlay() {
        let mut dict = PronunciationDict::english_minimal();
        dict.insert_user("custom", &[Phoneme::VowelA]);
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        let (_, source) = fallback.lookup_with_source("custom").unwrap();
        assert_eq!(source, LookupSource::UserOverlay);
    }

    #[test]
    fn test_lookup_with_source_base_dict() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        let (_, source) = fallback.lookup_with_source("hello").unwrap();
        assert_eq!(source, LookupSource::BaseDictionary);
    }

    #[test]
    fn test_lookup_with_source_g2p() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.75));

        let (phonemes, source) = fallback.lookup_with_source("xyzzy").unwrap();
        assert_eq!(phonemes, alloc::vec![Phoneme::VowelSchwa]);
        assert_eq!(source, LookupSource::G2PModel { confidence: 0.75 });
    }

    #[test]
    fn test_promote_prediction() {
        let dict = PronunciationDict::english_minimal();
        let mut fallback = FallbackDict::new(dict, MockG2PModel::new(0.8));

        assert!(fallback.promote_prediction("newword"));
        // Now it's in user overlay — second promote is a no-op.
        assert!(!fallback.promote_prediction("newword"));
        assert_eq!(fallback.dict().user_len(), 1);
    }

    #[test]
    fn test_promote_prediction_skips_existing() {
        let dict = PronunciationDict::english_minimal();
        let mut fallback = FallbackDict::new(dict, MockG2PModel::new(0.8));

        // "hello" is already in base dict — should not promote.
        assert!(!fallback.promote_prediction("hello"));
        assert_eq!(fallback.dict().user_len(), 0);
    }

    #[test]
    fn test_promote_if_confident_above_threshold() {
        let dict = PronunciationDict::english_minimal();
        let mut fallback = FallbackDict::new(dict, MockG2PModel::new(0.8));

        assert!(fallback.promote_if_confident("newword", 0.7));
        assert_eq!(fallback.dict().user_len(), 1);
    }

    #[test]
    fn test_promote_if_confident_below_threshold() {
        let dict = PronunciationDict::english_minimal();
        let mut fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        assert!(!fallback.promote_if_confident("newword", 0.7));
        assert_eq!(fallback.dict().user_len(), 0);
    }

    #[test]
    fn test_into_parts() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict.clone(), MockG2PModel::new(0.5));
        let (recovered_dict, _model) = fallback.into_parts();
        assert_eq!(recovered_dict.len(), dict.len());
    }

    #[test]
    fn test_fallback_dict_lookup_entry() {
        let dict = PronunciationDict::english_minimal();
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.5));

        assert!(fallback.lookup_entry("hello").is_some());
        assert!(fallback.lookup_entry("xyzzy").is_none());
    }

    #[test]
    fn test_user_overlay_takes_precedence_over_g2p() {
        let mut dict = PronunciationDict::new();
        dict.insert_user("test", &[Phoneme::PlosiveT]);
        let fallback = FallbackDict::new(dict, MockG2PModel::new(0.9));

        let (phonemes, source) = fallback.lookup_with_source("test").unwrap();
        assert_eq!(phonemes, alloc::vec![Phoneme::PlosiveT]);
        assert_eq!(source, LookupSource::UserOverlay);
    }

    #[test]
    fn test_fst_model_serde_roundtrip() {
        let model = FstModel::new("/path/to/model.fst", FstNotation::Arpabet);
        let json = serde_json::to_string(&model).unwrap();
        let model2: FstModel = serde_json::from_str(&json).unwrap();
        assert_eq!(model.model_path(), model2.model_path());
        assert_eq!(model.notation(), model2.notation());
    }

    #[test]
    fn test_fst_notation_serde_roundtrip() {
        for notation in [FstNotation::Arpabet, FstNotation::Ipa] {
            let json = serde_json::to_string(&notation).unwrap();
            let notation2: FstNotation = serde_json::from_str(&json).unwrap();
            assert_eq!(notation, notation2);
        }
    }
}