libgrammstein 0.1.0

Hybrid language model (N-gram + Embeddings) for WFST text 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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
//! WFST export for n-gram language models.
//!
//! This module provides functionality to export libgrammstein's `NgramModel`
//! as a Weighted Finite State Transducer (WFST) compatible with lling-llang.
//!
//! # Overview
//!
//! The export creates a WFST where:
//! - States represent n-gram histories (previous n-1 words)
//! - Transitions represent word emissions with log probability weights
//! - Backoff ε-transitions implement smoothing fallback to lower-order models
//!
//! # Example
//!
//! ```ignore
//! use libgrammstein::ngram::NgramModel;
//! use libgrammstein::integration::wfst_export::{FromLogProb, NgramWfstExporter};
//! use lling_llang::semiring::LogWeight;
//!
//! let model: NgramModel<D> = /* ... */;
//! let wfst = model.to_wfst::<LogWeight>();
//!
//! // Use in lling-llang pipeline
//! let path_weight = compose(&fst1, &wfst);
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use lling_llang::asr::{NgramBuilder, NgramConfig, NgramTransducer};
use lling_llang::semiring::{LogWeight, ProbabilityWeight, Semiring, TropicalWeight};
use lling_llang::wfst::{MutableWfst, StateId, VectorWfst};

#[allow(deprecated)]
use crate::ngram::{IterableDictionary, NgramEntry, NgramModel, NGRAM_SEPARATOR};
use liblevenshtein::dictionary::MutableMappedDictionary;

use super::vocabulary::{WordId, WordVocabulary, EOS_WORD_ID, UNK_WORD_ID};

/// Trait for converting log probabilities to semiring weights.
///
/// This trait bridges libgrammstein's log probability representation
/// (natural log, can be negative) with lling-llang's semiring weights.
///
/// # Log Probability Convention
///
/// libgrammstein stores log probabilities as `ln(p)` where `p ∈ (0, 1]`.
/// Thus log probabilities are typically negative (or zero for p=1).
///
/// # Semiring Conventions
///
/// - **LogWeight**: Stores negative log probability `-ln(p)`, so lower = better
/// - **TropicalWeight**: Same as LogWeight, stores `-ln(p)` as cost
/// - **ProbabilityWeight**: Stores probability directly `p = exp(ln(p))`
pub trait FromLogProb: Semiring {
    /// Convert from log probability (ln(p)) to semiring weight.
    ///
    /// # Arguments
    ///
    /// * `log_prob` - Natural log of probability (typically negative)
    fn from_log_prob(log_prob: f64) -> Self;

    /// Convert from negative log probability (-ln(p)) to semiring weight.
    ///
    /// # Arguments
    ///
    /// * `neg_log_prob` - Negative log probability (typically positive)
    fn from_neg_log_prob(neg_log_prob: f64) -> Self;
}

impl FromLogProb for LogWeight {
    #[inline]
    fn from_log_prob(log_prob: f64) -> Self {
        // LogWeight stores -ln(p), so negate the log probability
        LogWeight::new(-log_prob)
    }

    #[inline]
    fn from_neg_log_prob(neg_log_prob: f64) -> Self {
        LogWeight::new(neg_log_prob)
    }
}

impl FromLogProb for TropicalWeight {
    #[inline]
    fn from_log_prob(log_prob: f64) -> Self {
        // TropicalWeight stores cost = -ln(p)
        TropicalWeight::new(-log_prob)
    }

    #[inline]
    fn from_neg_log_prob(neg_log_prob: f64) -> Self {
        TropicalWeight::new(neg_log_prob)
    }
}

impl FromLogProb for ProbabilityWeight {
    #[inline]
    fn from_log_prob(log_prob: f64) -> Self {
        // ProbabilityWeight stores p = exp(ln(p))
        ProbabilityWeight::new(log_prob.exp())
    }

    #[inline]
    fn from_neg_log_prob(neg_log_prob: f64) -> Self {
        ProbabilityWeight::new((-neg_log_prob).exp())
    }
}

/// Builder for creating WFST from n-gram model.
///
/// This struct encapsulates the state needed during WFST construction.
pub struct NgramWfstBuilder<D, W>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
    W: Semiring + FromLogProb,
{
    /// The n-gram model being exported.
    model: Arc<NgramModel<D>>,
    /// Vocabulary mapping words to IDs.
    vocabulary: WordVocabulary,
    /// The WFST being constructed.
    wfst: VectorWfst<WordId, W>,
    /// Mapping from n-gram history (as word IDs) to WFST state ID.
    history_to_state: HashMap<Vec<WordId>, StateId>,
    /// Start state ID.
    start_state: StateId,
    /// Backoff state ID (for unigram backoff).
    backoff_state: StateId,
}

impl<D, W> NgramWfstBuilder<D, W>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
    W: Semiring + FromLogProb,
{
    /// Create a new WFST builder from an n-gram model.
    pub fn new(model: Arc<NgramModel<D>>) -> Self {
        let vocab_size = model.vocab_size();
        let vocabulary = Self::build_vocabulary(&model);

        // Estimate number of states (rough: vocab + vocab^2 for bigrams + ...)
        let estimated_states = vocab_size.saturating_mul(2);
        let mut wfst = VectorWfst::with_capacity(estimated_states);

        // Create start and backoff states
        let start_state = wfst.add_state();
        let backoff_state = wfst.add_state();

        wfst.set_start(start_state);

        // Start state is final (allows empty input)
        wfst.set_final(start_state, W::one());
        // Backoff state is final
        wfst.set_final(backoff_state, W::one());

        let mut history_to_state = HashMap::with_capacity(estimated_states);
        history_to_state.insert(vec![], backoff_state);

        Self {
            model,
            vocabulary,
            wfst,
            history_to_state,
            start_state,
            backoff_state,
        }
    }

    /// Build vocabulary from the n-gram model.
    #[allow(deprecated)]
    fn build_vocabulary(model: &NgramModel<D>) -> WordVocabulary {
        let mut vocab = WordVocabulary::with_capacity(model.vocab_size());

        // Iterate over all entries in the trie to collect unigrams
        for (key, _entry) in model.trie().iter_entries() {
            // Unigrams have no separator
            if !key.contains(NGRAM_SEPARATOR) {
                vocab.add_word(&key);
            }
        }

        vocab
    }

    /// Get or create a state for a given history.
    fn get_or_create_state(&mut self, history: &[WordId]) -> StateId {
        if let Some(&state) = self.history_to_state.get(history) {
            return state;
        }

        let state = self.wfst.add_state();
        self.wfst.set_final(state, W::one());
        self.history_to_state.insert(history.to_vec(), state);
        state
    }

    /// Get the backoff state for a given history.
    ///
    /// For history [w1, w2, w3], the backoff is [w2, w3].
    /// For empty history, there is no further backoff.
    fn get_backoff_history(history: &[WordId]) -> Option<Vec<WordId>> {
        if history.is_empty() {
            None
        } else {
            Some(history[1..].to_vec())
        }
    }

    /// Build the WFST from the n-gram model.
    pub fn build(mut self) -> (VectorWfst<WordId, W>, WordVocabulary) {
        let order = self.model.order();

        // Add epsilon transition from start to backoff
        self.wfst
            .add_epsilon(self.start_state, self.backoff_state, W::one());

        // First pass: add unigram transitions from backoff state
        self.add_unigrams();

        // Second pass: add higher-order n-grams
        if order > 1 {
            self.add_higher_order_ngrams();
        }

        // Third pass: add backoff transitions
        self.add_backoff_transitions();

        (self.wfst, self.vocabulary)
    }

    /// Add unigram transitions from the backoff state.
    fn add_unigrams(&mut self) {
        let backoff_state = self.backoff_state;

        // Collect unigram words first to avoid borrowing issues
        let unigram_words: Vec<String> = self
            .vocabulary
            .iter()
            .skip(2) // Skip EOS and UNK special tokens
            .map(|(word, _)| word.to_string())
            .collect();

        for word in unigram_words {
            let word_id = self
                .vocabulary
                .get_id(&word)
                .expect("Word must be in vocabulary");

            // Get log probability from model
            let log_prob = self.model.log_prob(&word, &[]);
            let weight = W::from_log_prob(log_prob);

            // Get or create state for this unigram history
            let history = vec![word_id];
            let target_state = self.get_or_create_state(&history);

            // Add transition: backoff -> unigram_state
            self.wfst.add_arc(
                backoff_state,
                Some(word_id),
                Some(word_id),
                target_state,
                weight,
            );
        }
    }

    /// Add higher-order n-gram transitions.
    #[allow(deprecated)]
    fn add_higher_order_ngrams(&mut self) {
        let order = self.model.order();

        // Collect all n-grams from the trie
        let ngrams: Vec<(Vec<String>, String)> = self
            .model
            .trie()
            .iter_entries()
            .filter_map(|(key, _entry)| {
                let tokens: Vec<&str> = key.split(NGRAM_SEPARATOR).collect();
                if tokens.len() >= 2 && tokens.len() <= order {
                    let history: Vec<String> = tokens[..tokens.len() - 1]
                        .iter()
                        .map(|s| s.to_string())
                        .collect();
                    let word = tokens.last().unwrap().to_string();
                    Some((history, word))
                } else {
                    None
                }
            })
            .collect();

        // Add transitions for each n-gram
        for (history_words, word) in ngrams {
            // Convert history to word IDs
            let history_ids: Vec<WordId> = history_words
                .iter()
                .filter_map(|w| self.vocabulary.get_id(w))
                .collect();

            // Skip if any history word is unknown
            if history_ids.len() != history_words.len() {
                continue;
            }

            let word_id = match self.vocabulary.get_id(&word) {
                Some(id) => id,
                None => continue, // Skip unknown words
            };

            // Get source state for this history
            let source_state = self.get_or_create_state(&history_ids);

            // Compute target history: history + word, truncated to order-1
            let mut target_history = history_ids.clone();
            target_history.push(word_id);
            if target_history.len() >= order {
                target_history = target_history[target_history.len() - (order - 1)..].to_vec();
            }
            let target_state = self.get_or_create_state(&target_history);

            // Get log probability
            let history_strs: Vec<&str> = history_words.iter().map(|s| s.as_str()).collect();
            let log_prob = self.model.log_prob(&word, &history_strs);
            let weight = W::from_log_prob(log_prob);

            // Add transition
            self.wfst.add_arc(
                source_state,
                Some(word_id),
                Some(word_id),
                target_state,
                weight,
            );
        }
    }

    /// Add epsilon backoff transitions between states.
    fn add_backoff_transitions(&mut self) {
        // Collect all histories and their states
        let histories: Vec<(Vec<WordId>, StateId)> = self
            .history_to_state
            .iter()
            .map(|(h, &s)| (h.clone(), s))
            .collect();

        for (history, state) in histories {
            // Skip the backoff state itself (empty history)
            if history.is_empty() {
                continue;
            }

            // Get backoff history
            if let Some(backoff_history) = Self::get_backoff_history(&history) {
                let backoff_state = self
                    .history_to_state
                    .get(&backoff_history)
                    .copied()
                    .unwrap_or(self.backoff_state);

                // Compute backoff weight
                // For simplicity, use unit weight; the model's smoothing handles the math
                let backoff_weight = W::one();

                // Add epsilon transition to backoff state
                self.wfst.add_epsilon(state, backoff_state, backoff_weight);
            }
        }
    }
}

/// Extension trait for NgramModel to provide WFST export.
pub trait NgramWfstExport<D>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
{
    /// Export the n-gram model as a VectorWfst.
    ///
    /// The resulting WFST can be used in lling-llang's WFST composition
    /// pipeline, for example as the "G" (grammar) transducer in ASR.
    ///
    /// # Type Parameters
    ///
    /// * `W` - The semiring weight type (must implement `FromLogProb`)
    ///
    /// # Returns
    ///
    /// A tuple of (WFST, vocabulary) where the vocabulary provides the
    /// mapping between word strings and the integer labels used in the WFST.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use libgrammstein::ngram::NgramModel;
    /// use libgrammstein::integration::wfst_export::NgramWfstExport;
    /// use lling_llang::semiring::LogWeight;
    ///
    /// let model: NgramModel<D> = /* ... */;
    /// let (wfst, vocab) = model.to_wfst::<LogWeight>();
    /// ```
    fn to_wfst<W>(&self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb;

    /// Export the n-gram model as a VectorWfst (consumes self for Arc optimization).
    fn into_wfst<W>(self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb;

    /// Export the n-gram model as an NgramTransducer.
    ///
    /// This creates a transducer compatible with lling-llang's n-gram
    /// transducer format, using `NgramBuilder` to construct the WFST
    /// with proper backoff structure.
    ///
    /// The resulting transducer can be used directly with lling-llang's
    /// ASR cascade composition pipeline.
    ///
    /// # Type Parameters
    ///
    /// * `W` - The semiring weight type (must implement `FromLogProb`)
    ///
    /// # Returns
    ///
    /// A tuple of (NgramTransducer, vocabulary) where the vocabulary provides
    /// the mapping between word strings and the integer labels used in the transducer.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use libgrammstein::ngram::NgramModel;
    /// use libgrammstein::integration::wfst_export::NgramWfstExport;
    /// use lling_llang::semiring::LogWeight;
    /// use lling_llang::asr::CascadeBuilder;
    ///
    /// let model: NgramModel<D> = /* ... */;
    /// let (transducer, vocab) = model.to_ngram_transducer::<LogWeight>();
    ///
    /// // Use in ASR cascade
    /// let cascade = CascadeBuilder::new()
    ///     .grammar_from_ngram(transducer)
    ///     .build();
    /// ```
    fn to_ngram_transducer<W>(&self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone;

    /// Export the n-gram model as an NgramTransducer (consumes self).
    fn into_ngram_transducer<W>(self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone;
}

/// Builder for creating NgramTransducer from NgramModel.
///
/// This uses lling-llang's NgramBuilder to construct the transducer
/// with proper backoff structure matching the n-gram model's smoothing.
pub struct NgramTransducerBuilder<D, W>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
    W: Semiring + FromLogProb + Clone,
{
    /// The n-gram model being exported.
    model: Arc<NgramModel<D>>,
    /// Vocabulary mapping words to IDs.
    vocabulary: WordVocabulary,
    /// The NgramBuilder for constructing the transducer.
    builder: NgramBuilder<W>,
}

impl<D, W> NgramTransducerBuilder<D, W>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
    W: Semiring + FromLogProb + Clone,
{
    /// Create a new NgramTransducer builder from an n-gram model.
    pub fn new(model: Arc<NgramModel<D>>) -> Self {
        let vocabulary = NgramWfstBuilder::<D, W>::build_vocabulary(&model);
        let mut builder = NgramBuilder::new(model.order());

        // Configure with EOS and UNK IDs from vocabulary
        let config = NgramConfig {
            order: model.order(),
            add_sentence_markers: true,
            sos_id: None, // libgrammstein doesn't use SOS in the same way
            eos_id: Some(EOS_WORD_ID),
            unk_id: Some(UNK_WORD_ID),
        };
        builder = builder.config(config);

        Self {
            model,
            vocabulary,
            builder,
        }
    }

    /// Build the NgramTransducer.
    pub fn build(mut self) -> (NgramTransducer<W>, WordVocabulary) {
        // Set vocabulary size
        self.builder = self.builder.vocab_size(self.vocabulary.len());

        // Add unigrams
        self.add_unigrams();

        // Add higher-order n-grams and backoff weights
        self.add_higher_order_ngrams();

        // Build and return
        let transducer = self.builder.build();
        (transducer, self.vocabulary)
    }

    /// Add unigram probabilities to the builder.
    fn add_unigrams(&mut self) {
        // Collect words to avoid borrowing issues
        let words: Vec<(String, WordId)> = self
            .vocabulary
            .iter()
            .skip(2) // Skip EOS and UNK special tokens
            .map(|(word, id)| (word.to_string(), id))
            .collect();

        for (word, word_id) in words {
            let log_prob = self.model.log_prob(&word, &[]);
            let weight = W::from_log_prob(log_prob);
            self.builder.add_unigram(word_id, weight);
        }
    }

    /// Add higher-order n-grams and backoff weights.
    #[allow(deprecated)]
    fn add_higher_order_ngrams(&mut self) {
        let order = self.model.order();

        // Collect all n-grams from the trie
        let ngrams: Vec<(Vec<String>, String)> = self
            .model
            .trie()
            .iter_entries()
            .filter_map(|(key, _entry)| {
                let tokens: Vec<&str> = key.split(NGRAM_SEPARATOR).collect();
                if tokens.len() >= 2 && tokens.len() <= order {
                    let history: Vec<String> = tokens[..tokens.len() - 1]
                        .iter()
                        .map(|s| s.to_string())
                        .collect();
                    let word = tokens.last().expect("tokens not empty").to_string();
                    Some((history, word))
                } else {
                    None
                }
            })
            .collect();

        // Track unique histories for backoff weight computation
        let mut histories_seen: std::collections::HashSet<Vec<WordId>> =
            std::collections::HashSet::new();

        // Add n-grams
        for (history_words, word) in ngrams {
            // Convert history to word IDs
            let history_ids: Vec<WordId> = history_words
                .iter()
                .filter_map(|w| self.vocabulary.get_id(w))
                .collect();

            // Skip if any history word is unknown
            if history_ids.len() != history_words.len() {
                continue;
            }

            let word_id = match self.vocabulary.get_id(&word) {
                Some(id) => id,
                None => continue, // Skip unknown words
            };

            // Get log probability
            let history_strs: Vec<&str> = history_words.iter().map(|s| s.as_str()).collect();
            let log_prob = self.model.log_prob(&word, &history_strs);
            let weight = W::from_log_prob(log_prob);

            // Add n-gram to builder
            self.builder.add_ngram(&history_ids, word_id, weight);

            // Track this history for backoff
            histories_seen.insert(history_ids);
        }

        // Add backoff weights for all seen histories
        // The backoff weight accounts for probability mass reserved for unseen n-grams
        for history_ids in histories_seen {
            // Compute backoff weight
            // In Modified Kneser-Ney, β(h) = D(h) * N1+(h•) / C(h)
            // For simplicity, we use unit weight since log_prob already includes smoothing
            let backoff_weight = W::one();

            self.builder.set_backoff(&history_ids, backoff_weight);
        }
    }
}

impl<D> NgramWfstExport<D> for NgramModel<D>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
{
    fn to_wfst<W>(&self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb,
    {
        // Clone the model into an Arc for the builder
        let model = Arc::new(self.clone());
        let builder = NgramWfstBuilder::new(model);
        builder.build()
    }

    fn into_wfst<W>(self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb,
    {
        let model = Arc::new(self);
        let builder = NgramWfstBuilder::new(model);
        builder.build()
    }

    fn to_ngram_transducer<W>(&self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone,
    {
        let model = Arc::new(self.clone());
        let builder = NgramTransducerBuilder::new(model);
        builder.build()
    }

    fn into_ngram_transducer<W>(self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone,
    {
        let model = Arc::new(self);
        let builder = NgramTransducerBuilder::new(model);
        builder.build()
    }
}

impl<D> NgramWfstExport<D> for Arc<NgramModel<D>>
where
    D: MutableMappedDictionary<Value = NgramEntry> + IterableDictionary,
{
    fn to_wfst<W>(&self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb,
    {
        let builder = NgramWfstBuilder::new(Arc::clone(self));
        builder.build()
    }

    fn into_wfst<W>(self) -> (VectorWfst<WordId, W>, WordVocabulary)
    where
        W: Semiring + FromLogProb,
    {
        let builder = NgramWfstBuilder::new(self);
        builder.build()
    }

    fn to_ngram_transducer<W>(&self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone,
    {
        let builder = NgramTransducerBuilder::new(Arc::clone(self));
        builder.build()
    }

    fn into_ngram_transducer<W>(self) -> (NgramTransducer<W>, WordVocabulary)
    where
        W: Semiring + FromLogProb + Clone,
    {
        let builder = NgramTransducerBuilder::new(self);
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::corpus::PlaintextReader;
    use crate::ngram::TrainerBuilder;
    use liblevenshtein::dictionary::pathmap::PathMapDictionary;
    use lling_llang::wfst::Wfst;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_test_model() -> NgramModel<PathMapDictionary<NgramEntry>> {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let content = "the quick brown fox the quick brown dog the lazy fox \
                       the quick brown fox the quick brown dog the lazy fox";
        let path = dir.path().join("test.txt");
        let mut file = std::fs::File::create(&path).expect("Failed to create test file");
        write!(file, "{}", content).expect("Failed to write test file");

        let reader = PlaintextReader::from_file(&path).expect("Failed to create reader");
        let dictionary = PathMapDictionary::<NgramEntry>::new();
        TrainerBuilder::new(dictionary)
            .order(3)
            .train(reader)
            .expect("Training failed")
    }

    #[test]
    fn test_from_log_prob_log_weight() {
        let log_prob = -1.0_f64; // ln(0.368)
        let weight = LogWeight::from_log_prob(log_prob);
        assert!((weight.value() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_from_log_prob_tropical_weight() {
        let log_prob = -2.0_f64;
        let weight = TropicalWeight::from_log_prob(log_prob);
        assert!((weight.value() - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_from_log_prob_probability_weight() {
        let log_prob = 0.0_f64; // ln(1.0)
        let weight = ProbabilityWeight::from_log_prob(log_prob);
        assert!((weight.value() - 1.0).abs() < 1e-10);

        let log_prob = -1.0_f64; // ln(0.368)
        let weight = ProbabilityWeight::from_log_prob(log_prob);
        assert!((weight.value() - (-1.0_f64).exp()).abs() < 1e-10);
    }

    #[test]
    fn test_build_vocabulary() {
        let model = create_test_model();
        let model_arc = Arc::new(model);
        let vocab = NgramWfstBuilder::<_, LogWeight>::build_vocabulary(&model_arc);

        // Should have special tokens + words from corpus
        assert!(vocab.len() >= 2);
        assert!(vocab.contains("the"));
        assert!(vocab.contains("quick"));
        assert!(vocab.contains("brown"));
        assert!(vocab.contains("fox"));
    }

    #[test]
    fn test_to_wfst_basic() {
        let model = create_test_model();
        let (wfst, vocab): (VectorWfst<WordId, LogWeight>, _) = model.to_wfst();

        // WFST should have states
        assert!(wfst.num_states() > 0);

        // Start state should be set
        assert!(wfst.start() != u32::MAX);

        // Vocabulary should have words
        assert!(vocab.len() > 2);
    }

    #[test]
    fn test_to_wfst_has_transitions() {
        let model = create_test_model();
        let (wfst, _vocab): (VectorWfst<WordId, LogWeight>, _) = model.to_wfst();

        // Start state should have transitions
        let start = wfst.start();
        let transitions = wfst.transitions(start);

        // Should have at least epsilon to backoff
        assert!(!transitions.is_empty());
    }

    #[test]
    fn test_to_wfst_final_states() {
        let model = create_test_model();
        let (wfst, _vocab): (VectorWfst<WordId, LogWeight>, _) = model.to_wfst();

        // All states should be final in an n-gram WFST
        for state_id in 0..wfst.num_states() as StateId {
            assert!(
                wfst.is_final(state_id),
                "State {} should be final",
                state_id
            );
        }
    }

    #[test]
    fn test_to_wfst_weights_finite() {
        let model = create_test_model();
        let (wfst, _vocab): (VectorWfst<WordId, LogWeight>, _) = model.to_wfst();

        // All transition weights should be finite
        for state_id in 0..wfst.num_states() as StateId {
            for transition in wfst.transitions(state_id) {
                assert!(
                    transition.weight.value().is_finite(),
                    "Transition weight should be finite"
                );
            }
        }
    }

    #[test]
    fn test_tropical_weight_wfst() {
        let model = create_test_model();
        let (wfst, _vocab): (VectorWfst<WordId, TropicalWeight>, _) = model.to_wfst();

        assert!(wfst.num_states() > 0);
    }

    #[test]
    fn test_probability_weight_wfst() {
        let model = create_test_model();
        let (wfst, _vocab): (VectorWfst<WordId, ProbabilityWeight>, _) = model.to_wfst();

        assert!(wfst.num_states() > 0);
    }

    // NgramTransducer tests

    #[test]
    fn test_to_ngram_transducer_basic() {
        let model = create_test_model();
        let (transducer, vocab) = model.to_ngram_transducer::<LogWeight>();

        // Transducer should have states
        assert!(transducer.fst.num_states() > 0);

        // Start state should be set
        assert!(transducer.fst.start() != u32::MAX);

        // Vocabulary should have words
        assert!(vocab.len() > 2);

        // Order should match model
        assert_eq!(transducer.order(), 3);
    }

    #[test]
    fn test_to_ngram_transducer_has_unigram_transitions() {
        let model = create_test_model();
        let (transducer, vocab) = model.to_ngram_transducer::<LogWeight>();

        // Count non-epsilon transitions from backoff state
        // Backoff state is state 1 in NgramBuilder
        let mut word_transitions = 0;
        for state_id in 0..transducer.fst.num_states() as StateId {
            for trans in transducer.fst.transitions(state_id) {
                if trans.input.is_some() {
                    word_transitions += 1;
                }
            }
        }

        // Should have at least as many transitions as vocab words (excluding special tokens)
        assert!(
            word_transitions >= vocab.len() - 2,
            "Expected at least {} word transitions, got {}",
            vocab.len() - 2,
            word_transitions
        );
    }

    #[test]
    fn test_to_ngram_transducer_final_states() {
        let model = create_test_model();
        let (transducer, _vocab) = model.to_ngram_transducer::<LogWeight>();

        // All states should be final in an n-gram transducer
        for state_id in 0..transducer.fst.num_states() as StateId {
            assert!(
                transducer.fst.is_final(state_id),
                "State {} should be final",
                state_id
            );
        }
    }

    #[test]
    fn test_to_ngram_transducer_weights_finite() {
        let model = create_test_model();
        let (transducer, _vocab) = model.to_ngram_transducer::<LogWeight>();

        // All transition weights should be finite
        for state_id in 0..transducer.fst.num_states() as StateId {
            for transition in transducer.fst.transitions(state_id) {
                assert!(
                    transition.weight.value().is_finite(),
                    "Transition weight should be finite at state {}",
                    state_id
                );
            }
        }
    }

    #[test]
    fn test_to_ngram_transducer_tropical_weight() {
        let model = create_test_model();
        let (transducer, _vocab) = model.to_ngram_transducer::<TropicalWeight>();

        assert!(transducer.fst.num_states() > 0);
        assert_eq!(transducer.order(), 3);
    }

    #[test]
    fn test_to_ngram_transducer_vocabulary_size() {
        let model = create_test_model();
        let (transducer, vocab) = model.to_ngram_transducer::<LogWeight>();

        // Transducer vocabulary size should match WordVocabulary
        assert_eq!(transducer.vocabulary_size(), vocab.len());
    }

    #[test]
    fn test_to_ngram_transducer_has_backoff_arcs() {
        let model = create_test_model();
        let (transducer, _vocab) = model.to_ngram_transducer::<LogWeight>();

        // Should have epsilon transitions (backoff arcs)
        let mut has_epsilon = false;
        for state_id in 0..transducer.fst.num_states() as StateId {
            for trans in transducer.fst.transitions(state_id) {
                if trans.input.is_none() {
                    has_epsilon = true;
                    break;
                }
            }
            if has_epsilon {
                break;
            }
        }

        assert!(
            has_epsilon,
            "Transducer should have backoff epsilon transitions"
        );
    }

    #[test]
    fn test_into_ngram_transducer() {
        let model = create_test_model();
        let (transducer, vocab) = model.into_ngram_transducer::<LogWeight>();

        assert!(transducer.fst.num_states() > 0);
        assert!(vocab.len() > 2);
    }

    #[test]
    fn test_arc_ngram_transducer() {
        let model = Arc::new(create_test_model());
        let (transducer, vocab) = model.to_ngram_transducer::<LogWeight>();

        assert!(transducer.fst.num_states() > 0);
        assert!(vocab.len() > 2);
    }
}