lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Subword lexicon builder for ASR with BPE tokenization.
//!
//! This module extends the standard lexicon with subword tokenization support,
//! enabling handling of out-of-vocabulary words and morphologically rich languages.
//!
//! # Subword Boundary Markers
//!
//! Three marking styles are supported (based on Smit et al. 2017):
//!
//! - **LeftMarked**: `+word` - subword continues from the left (word fragment)
//! - **RightMarked**: `word+` - subword continues to the right
//! - **BoundaryTag**: `<w>word` - explicit word boundary before word
//!
//! # Example
//!
//! ```ignore
//! use lling_llang::asr::SubwordLexiconBuilder;
//! use lling_llang::semiring::LogWeight;
//!
//! let mut builder = SubwordLexiconBuilder::<LogWeight>::new(MarkingStyle::LeftMarked);
//!
//! // Add whole word entry
//! builder.add_word("hello", &["HH", "AH", "L", "OW"], LogWeight::one());
//!
//! // Add subword entries (e.g., from BPE)
//! builder.add_subword("hel", &["HH", "AH", "L"], SubwordPosition::Initial);
//! builder.add_subword("lo", &["L", "OW"], SubwordPosition::Final);
//!
//! // Build into cascade
//! let cascade = builder.build_cascade(&ngram)?;
//! ```
//!
//! # References
//!
//! - Smit, P., Virpioja, S., & Kurimo, M. (2017). Improved Subword Modeling
//!   for WFST-Based Speech Recognition. Interspeech.

use std::collections::HashMap;
use std::marker::PhantomData;

use crate::semiring::Semiring;
use crate::wfst::{MutableWfst, VectorWfst};

use super::cascade::LexiconEntry;
use super::context::PhoneId;
use super::ngram::WordId;

/// Subword boundary marking style.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MarkingStyle {
    /// Left-marked: `+word` means continuation from left (word fragment).
    /// Used for morpheme-initial subwords.
    LeftMarked,
    /// Right-marked: `word+` means continuation to right.
    /// Used for morpheme-final subwords.
    RightMarked,
    /// Boundary tag: `<w>word` marks word boundaries explicitly.
    /// Most explicit but adds more symbols.
    BoundaryTag,
}

/// Position of subword within a word.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SubwordPosition {
    /// Complete word (not a subword).
    WholeWord,
    /// Initial subword (start of word).
    Initial,
    /// Medial subword (middle of word).
    Medial,
    /// Final subword (end of word).
    Final,
}

/// A subword entry in the lexicon.
#[derive(Clone, Debug)]
pub struct SubwordEntry<W: Semiring> {
    /// The subword text (with boundary markers applied).
    pub subword: String,
    /// Subword ID for FST labels.
    pub subword_id: u32,
    /// Pronunciation as sequence of phones.
    pub phones: Vec<PhoneId>,
    /// Position within word.
    pub position: SubwordPosition,
    /// Weight (log probability or cost).
    pub weight: W,
    /// Original unmarked subword text.
    pub raw_subword: String,
}

/// Builder for subword lexicons.
///
/// Creates FST-based lexicons that map subword sequences to phone sequences,
/// with proper boundary marking for word reconstruction.
pub struct SubwordLexiconBuilder<W: Semiring> {
    /// Boundary marking style.
    marking_style: MarkingStyle,

    /// Subword vocabulary: raw text -> ID.
    subword_vocab: HashMap<String, u32>,

    /// Reverse vocabulary: ID -> marked text.
    reverse_vocab: Vec<String>,

    /// Subword entries.
    entries: Vec<SubwordEntry<W>>,

    /// Word-to-subword mappings for decomposition.
    word_decompositions: HashMap<WordId, Vec<u32>>,

    /// Phone vocabulary: phone string -> ID.
    phone_vocab: HashMap<String, PhoneId>,

    /// Reverse phone vocab: ID -> string.
    reverse_phone_vocab: Vec<String>,

    /// Next available subword ID.
    next_subword_id: u32,

    /// Weight marker.
    _weight: PhantomData<W>,
}

impl<W: Semiring + Clone> SubwordLexiconBuilder<W> {
    /// Create a new subword lexicon builder.
    pub fn new(marking_style: MarkingStyle) -> Self {
        Self {
            marking_style,
            subword_vocab: HashMap::new(),
            reverse_vocab: Vec::new(),
            entries: Vec::new(),
            word_decompositions: HashMap::new(),
            phone_vocab: HashMap::new(),
            reverse_phone_vocab: Vec::new(),
            next_subword_id: 0,
            _weight: PhantomData,
        }
    }

    /// Get the marking style.
    pub fn marking_style(&self) -> MarkingStyle {
        self.marking_style
    }

    /// Get subword vocabulary size.
    pub fn vocab_size(&self) -> usize {
        self.subword_vocab.len()
    }

    /// Get phone vocabulary size.
    pub fn phone_vocab_size(&self) -> usize {
        self.phone_vocab.len()
    }

    /// Add or get phone ID.
    fn intern_phone(&mut self, phone: &str) -> PhoneId {
        if let Some(&id) = self.phone_vocab.get(phone) {
            id
        } else {
            let id = self.reverse_phone_vocab.len() as PhoneId;
            self.phone_vocab.insert(phone.to_string(), id);
            self.reverse_phone_vocab.push(phone.to_string());
            id
        }
    }

    /// Apply boundary marking to a subword.
    fn apply_marking(&self, subword: &str, position: SubwordPosition) -> String {
        match (self.marking_style, position) {
            // Whole words get no marking
            (_, SubwordPosition::WholeWord) => subword.to_string(),

            // Left-marked: prefix with + for continuations
            (MarkingStyle::LeftMarked, SubwordPosition::Initial) => subword.to_string(),
            (MarkingStyle::LeftMarked, SubwordPosition::Medial) => format!("+{}", subword),
            (MarkingStyle::LeftMarked, SubwordPosition::Final) => format!("+{}", subword),

            // Right-marked: suffix with + for continuations
            (MarkingStyle::RightMarked, SubwordPosition::Initial) => format!("{}+", subword),
            (MarkingStyle::RightMarked, SubwordPosition::Medial) => format!("{}+", subword),
            (MarkingStyle::RightMarked, SubwordPosition::Final) => subword.to_string(),

            // Boundary tag: add <w> before word-initial subwords
            (MarkingStyle::BoundaryTag, SubwordPosition::Initial) => format!("<w>{}", subword),
            (MarkingStyle::BoundaryTag, SubwordPosition::Medial) => subword.to_string(),
            (MarkingStyle::BoundaryTag, SubwordPosition::Final) => subword.to_string(),
        }
    }

    /// Check if a marked subword indicates word boundary.
    pub fn is_word_boundary(&self, marked_subword: &str) -> bool {
        match self.marking_style {
            MarkingStyle::LeftMarked => !marked_subword.starts_with('+'),
            MarkingStyle::RightMarked => !marked_subword.ends_with('+'),
            MarkingStyle::BoundaryTag => marked_subword.starts_with("<w>"),
        }
    }

    /// Add a complete word entry.
    pub fn add_word(&mut self, word: &str, phones: &[&str], weight: W) -> u32 {
        self.add_subword(word, phones, SubwordPosition::WholeWord, weight)
    }

    /// Add a subword entry with position marking.
    pub fn add_subword(
        &mut self,
        subword: &str,
        phones: &[&str],
        position: SubwordPosition,
        weight: W,
    ) -> u32 {
        let marked = self.apply_marking(subword, position);

        // Check if already exists
        if let Some(&id) = self.subword_vocab.get(&marked) {
            return id;
        }

        // Assign new ID
        let id = self.next_subword_id;
        self.next_subword_id += 1;

        // Intern phones
        let phone_ids: Vec<PhoneId> = phones.iter().map(|p| self.intern_phone(p)).collect();

        // Store in vocabulary
        self.subword_vocab.insert(marked.clone(), id);
        self.reverse_vocab.push(marked.clone());

        // Create entry
        let entry = SubwordEntry {
            subword: marked,
            subword_id: id,
            phones: phone_ids,
            position,
            weight,
            raw_subword: subword.to_string(),
        };
        self.entries.push(entry);

        id
    }

    /// Register a word decomposition into subwords.
    ///
    /// This is used when the language model operates on subwords but you want
    /// to maintain word-level alignment for evaluation.
    pub fn register_decomposition(&mut self, word_id: WordId, subword_ids: Vec<u32>) {
        self.word_decompositions.insert(word_id, subword_ids);
    }

    /// Get subword ID by marked text.
    pub fn get_subword_id(&self, marked_subword: &str) -> Option<u32> {
        self.subword_vocab.get(marked_subword).copied()
    }

    /// Get marked text by subword ID.
    pub fn get_subword_text(&self, id: u32) -> Option<&str> {
        self.reverse_vocab.get(id as usize).map(|s| s.as_str())
    }

    /// Get phone name by ID.
    pub fn get_phone_name(&self, id: PhoneId) -> Option<&str> {
        self.reverse_phone_vocab
            .get(id as usize)
            .map(|s| s.as_str())
    }

    /// Build the subword lexicon transducer (L).
    ///
    /// Creates an FST mapping subword sequences to phone sequences.
    ///
    /// Input labels: phones
    /// Output labels: subword IDs
    pub fn build_lexicon_fst(&self) -> VectorWfst<PhoneId, W> {
        let mut fst: VectorWfst<PhoneId, W> = VectorWfst::new();

        // Create initial state (also accepting - allows empty input)
        let start = fst.add_state();
        fst.set_start(start);
        fst.set_final(start, W::one());

        // Add each subword entry
        for entry in &self.entries {
            if entry.phones.is_empty() {
                continue;
            }

            let mut current = start;

            // First phone: output the subword label
            // (In a proper L transducer, output would be on first arc)
            let next = fst.add_state();
            fst.add_arc(
                current,
                Some(entry.phones[0]),
                Some(entry.phones[0]),
                next,
                entry.weight.clone(),
            );
            current = next;

            // Middle phones (if any)
            for &phone in entry
                .phones
                .iter()
                .skip(1)
                .take(entry.phones.len().saturating_sub(2))
            {
                let next = fst.add_state();
                fst.add_arc(current, Some(phone), Some(phone), next, W::one());
                current = next;
            }

            // Last phone (if more than one phone): return to start
            if entry.phones.len() > 1 {
                let last_phone = entry.phones[entry.phones.len() - 1];
                fst.add_arc(current, Some(last_phone), Some(last_phone), start, W::one());
            } else {
                // Single-phone subword: create self-loop back to start
                // The arc we added already goes from start to next
                // Add epsilon transition from next back to start
                fst.add_arc(current, None, None, start, W::one());
            }
        }

        fst
    }

    /// Convert entries to standard LexiconEntry format for CascadeBuilder.
    pub fn to_lexicon_entries(&self) -> Vec<LexiconEntry<W>> {
        self.entries
            .iter()
            .map(|e| LexiconEntry {
                word: e.subword_id as WordId,
                phones: e.phones.clone(),
                weight: e.weight.clone(),
                auxiliaries: Vec::new(),
            })
            .collect()
    }

    /// Get all entries.
    pub fn entries(&self) -> &[SubwordEntry<W>] {
        &self.entries
    }

    /// Get word decomposition by word ID.
    pub fn get_decomposition(&self, word_id: WordId) -> Option<&[u32]> {
        self.word_decompositions.get(&word_id).map(|v| v.as_slice())
    }

    /// Reconstruct word from subword sequence.
    ///
    /// Uses boundary markers to determine word boundaries.
    pub fn reconstruct_words(&self, subword_ids: &[u32]) -> Vec<String> {
        let mut words = Vec::new();
        let mut current_word = String::new();

        for &id in subword_ids {
            let Some(marked) = self.get_subword_text(id) else {
                continue;
            };

            // Get the raw subword without markers
            let raw = match self.marking_style {
                MarkingStyle::LeftMarked => {
                    if marked.starts_with('+') {
                        &marked[1..]
                    } else {
                        marked
                    }
                }
                MarkingStyle::RightMarked => {
                    if marked.ends_with('+') {
                        &marked[..marked.len() - 1]
                    } else {
                        marked
                    }
                }
                MarkingStyle::BoundaryTag => {
                    if marked.starts_with("<w>") {
                        &marked[3..]
                    } else {
                        marked
                    }
                }
            };

            // Check for word boundary
            let is_boundary = self.is_word_boundary(marked);

            // For left-marked and boundary-tag: boundary is at the START
            // (finalize previous word before starting new one)
            // For right-marked: boundary is at the END
            // (finalize current word after appending)
            match self.marking_style {
                MarkingStyle::LeftMarked | MarkingStyle::BoundaryTag => {
                    // Boundary at start: finalize before appending
                    if is_boundary && !current_word.is_empty() {
                        words.push(current_word);
                        current_word = String::new();
                    }
                    current_word.push_str(raw);
                }
                MarkingStyle::RightMarked => {
                    // Boundary at end: append then finalize
                    current_word.push_str(raw);
                    if is_boundary && !current_word.is_empty() {
                        words.push(current_word);
                        current_word = String::new();
                    }
                }
            }
        }

        // Push final word if non-empty
        if !current_word.is_empty() {
            words.push(current_word);
        }

        words
    }
}

impl<W: Semiring + Clone> Default for SubwordLexiconBuilder<W> {
    fn default() -> Self {
        Self::new(MarkingStyle::LeftMarked)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semiring::TropicalWeight;
    use crate::wfst::Wfst;

    #[test]
    fn test_marking_style_left() {
        let builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        assert_eq!(
            builder.apply_marking("word", SubwordPosition::WholeWord),
            "word"
        );
        assert_eq!(
            builder.apply_marking("hel", SubwordPosition::Initial),
            "hel"
        );
        assert_eq!(builder.apply_marking("lo", SubwordPosition::Medial), "+lo");
        assert_eq!(builder.apply_marking("ing", SubwordPosition::Final), "+ing");
    }

    #[test]
    fn test_marking_style_right() {
        let builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::RightMarked);

        assert_eq!(
            builder.apply_marking("word", SubwordPosition::WholeWord),
            "word"
        );
        assert_eq!(
            builder.apply_marking("hel", SubwordPosition::Initial),
            "hel+"
        );
        assert_eq!(builder.apply_marking("lo", SubwordPosition::Medial), "lo+");
        assert_eq!(builder.apply_marking("ing", SubwordPosition::Final), "ing");
    }

    #[test]
    fn test_marking_style_boundary() {
        let builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::BoundaryTag);

        assert_eq!(
            builder.apply_marking("word", SubwordPosition::WholeWord),
            "word"
        );
        assert_eq!(
            builder.apply_marking("hel", SubwordPosition::Initial),
            "<w>hel"
        );
        assert_eq!(builder.apply_marking("lo", SubwordPosition::Medial), "lo");
        assert_eq!(builder.apply_marking("ing", SubwordPosition::Final), "ing");
    }

    #[test]
    fn test_add_word() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        let id = builder.add_word("hello", &["HH", "AH", "L", "OW"], TropicalWeight::one());
        assert_eq!(id, 0);
        assert_eq!(builder.vocab_size(), 1);
        assert_eq!(builder.phone_vocab_size(), 4);

        // Adding same word should return same ID
        let id2 = builder.add_word("hello", &["HH", "AH", "L", "OW"], TropicalWeight::one());
        assert_eq!(id, id2);
    }

    #[test]
    fn test_add_subwords() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        let id1 = builder.add_subword(
            "hel",
            &["HH", "AH", "L"],
            SubwordPosition::Initial,
            TropicalWeight::one(),
        );
        let id2 = builder.add_subword(
            "lo",
            &["L", "OW"],
            SubwordPosition::Final,
            TropicalWeight::one(),
        );

        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(builder.vocab_size(), 2);

        // Check marked forms
        assert_eq!(builder.get_subword_text(id1), Some("hel"));
        assert_eq!(builder.get_subword_text(id2), Some("+lo"));
    }

    #[test]
    fn test_is_word_boundary() {
        let left_builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);
        assert!(left_builder.is_word_boundary("hello"));
        assert!(!left_builder.is_word_boundary("+ing"));

        let right_builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::RightMarked);
        assert!(right_builder.is_word_boundary("hello"));
        assert!(!right_builder.is_word_boundary("hel+"));

        let boundary_builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::BoundaryTag);
        assert!(boundary_builder.is_word_boundary("<w>hello"));
        assert!(!boundary_builder.is_word_boundary("ing"));
    }

    #[test]
    fn test_reconstruct_words_left_marked() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        // Add subwords: "hel", "+lo", "world"
        let id1 = builder.add_subword(
            "hel",
            &["HH", "AH", "L"],
            SubwordPosition::Initial,
            TropicalWeight::one(),
        );
        let id2 = builder.add_subword(
            "lo",
            &["L", "OW"],
            SubwordPosition::Final,
            TropicalWeight::one(),
        );
        let id3 = builder.add_word("world", &["W", "ER", "L", "D"], TropicalWeight::one());

        let words = builder.reconstruct_words(&[id1, id2, id3]);
        assert_eq!(words, vec!["hello", "world"]);
    }

    #[test]
    fn test_reconstruct_words_right_marked() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::RightMarked);

        // Add subwords: "hel+", "lo", "world"
        let id1 = builder.add_subword(
            "hel",
            &["HH", "AH", "L"],
            SubwordPosition::Initial,
            TropicalWeight::one(),
        );
        let id2 = builder.add_subword(
            "lo",
            &["L", "OW"],
            SubwordPosition::Final,
            TropicalWeight::one(),
        );
        let id3 = builder.add_word("world", &["W", "ER", "L", "D"], TropicalWeight::one());

        let words = builder.reconstruct_words(&[id1, id2, id3]);
        assert_eq!(words, vec!["hello", "world"]);
    }

    #[test]
    fn test_build_lexicon_fst() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        builder.add_word("hi", &["HH", "AY"], TropicalWeight::one());
        builder.add_word("bye", &["B", "AY"], TropicalWeight::one());

        let fst = builder.build_lexicon_fst();

        // Should have states: start + 2 states per word = 5 states
        assert!(fst.num_states() >= 3);
        // Verify start state is valid
        assert!(fst.is_valid_state(fst.start()));
    }

    #[test]
    fn test_to_lexicon_entries() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        builder.add_word("hello", &["HH", "AH", "L", "OW"], TropicalWeight::new(1.5));

        let entries = builder.to_lexicon_entries();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].word, 0);
        assert_eq!(entries[0].phones.len(), 4);
        assert!((entries[0].weight.value() - 1.5).abs() < 0.001);
    }

    #[test]
    fn test_register_decomposition() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        let id1 = builder.add_subword(
            "un",
            &["AH", "N"],
            SubwordPosition::Initial,
            TropicalWeight::one(),
        );
        let id2 = builder.add_subword(
            "break",
            &["B", "R", "EY", "K"],
            SubwordPosition::Medial,
            TropicalWeight::one(),
        );
        let id3 = builder.add_subword(
            "able",
            &["AH", "B", "AH", "L"],
            SubwordPosition::Final,
            TropicalWeight::one(),
        );

        // Register word decomposition
        let word_id: WordId = 42;
        builder.register_decomposition(word_id, vec![id1, id2, id3]);

        let decomp = builder.get_decomposition(word_id);
        assert_eq!(decomp, Some(&[id1, id2, id3][..]));
    }

    #[test]
    fn test_phone_interning() {
        let mut builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        builder.add_word("aaa", &["AH", "AH", "AH"], TropicalWeight::one());
        builder.add_word("bbb", &["B", "B", "B"], TropicalWeight::one());

        // Should only have 2 unique phones
        assert_eq!(builder.phone_vocab_size(), 2);
        assert_eq!(builder.get_phone_name(0), Some("AH"));
        assert_eq!(builder.get_phone_name(1), Some("B"));
    }

    #[test]
    fn test_empty_builder() {
        let builder: SubwordLexiconBuilder<TropicalWeight> =
            SubwordLexiconBuilder::new(MarkingStyle::LeftMarked);

        assert_eq!(builder.vocab_size(), 0);
        assert_eq!(builder.phone_vocab_size(), 0);
        assert!(builder.entries().is_empty());

        let fst = builder.build_lexicon_fst();
        assert_eq!(fst.num_states(), 1); // Just start state
    }
}