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
//! N-gram transitions with pruning and back-off for differentiable training.
//!
//! This module provides efficient n-gram transition structures that scale
//! to large vocabularies through pruning and back-off mechanisms.
//!
//! ## Problem
//!
//! Dense n-gram transition graphs have complexity O(C^n) where:
//! - C = vocabulary size
//! - n = n-gram order
//!
//! For 1000 word pieces with bigrams: 1,000,000 states/transitions!
//!
//! ## Solution
//!
//! 1. **Pruning**: Only keep n-grams observed ≥ k times in training data
//! 2. **Back-off**: Missing n-grams fall back to (n-1)-gram probabilities
//!
//! ## Results
//!
//! Pruning rare n-grams and backing off sharply reduces per-epoch training cost for large
//! word-piece vocabularies, with negligible accuracy loss at a suitable threshold.
//!
//! ## Back-off Structure
//!
//! ```text
//! State for "ab":
//!   - If "abc" seen: direct transition to "bc" state
//!   - If "abc" unseen: ε-transition to "b" back-off state, then to "c"
//! ```
//!
//! ## References
//!
//! - Hannun et al., "Differentiable Weighted Finite-State Transducers" (ICML 2020, arXiv:2010.01003)
//! - Katz, "Estimation of probabilities from sparse data" (1987)

use std::collections::{HashMap, HashSet};

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

/// Token identifier type.
pub type TokenId = u32;

/// N-gram order type.
pub type NgramOrder = usize;

/// Configuration for pruned n-gram construction.
#[derive(Clone, Debug)]
pub struct PrunedNgramConfig {
    /// N-gram order (2 = bigram, 3 = trigram, etc.).
    pub order: NgramOrder,
    /// Minimum count threshold for keeping an n-gram.
    pub min_count: usize,
    /// Whether to use back-off for unseen n-grams.
    pub use_backoff: bool,
    /// Back-off weight (log probability).
    pub backoff_weight: f64,
    /// Whether to smooth probabilities.
    pub smoothing: bool,
    /// Smoothing discount factor (for Kneser-Ney style smoothing).
    pub discount: f64,
}

impl Default for PrunedNgramConfig {
    fn default() -> Self {
        Self {
            order: 2,
            min_count: 1,
            use_backoff: true,
            backoff_weight: 0.0,
            smoothing: false,
            discount: 0.5,
        }
    }
}

/// N-gram counts for pruning decisions.
#[derive(Clone, Debug, Default)]
pub struct NgramCounts {
    /// Unigram counts: token -> count.
    pub unigrams: HashMap<TokenId, usize>,
    /// Bigram counts: (prev, curr) -> count.
    pub bigrams: HashMap<(TokenId, TokenId), usize>,
    /// Trigram counts: (prev2, prev1, curr) -> count.
    pub trigrams: HashMap<(TokenId, TokenId, TokenId), usize>,
    /// Total count of all tokens.
    pub total: usize,
}

impl NgramCounts {
    /// Create empty counts.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add counts from a token sequence.
    pub fn add_sequence(&mut self, tokens: &[TokenId]) {
        for &token in tokens {
            *self.unigrams.entry(token).or_insert(0) += 1;
            self.total += 1;
        }

        for window in tokens.windows(2) {
            let bigram = (window[0], window[1]);
            *self.bigrams.entry(bigram).or_insert(0) += 1;
        }

        for window in tokens.windows(3) {
            let trigram = (window[0], window[1], window[2]);
            *self.trigrams.entry(trigram).or_insert(0) += 1;
        }
    }

    /// Get unigram count.
    pub fn unigram_count(&self, token: TokenId) -> usize {
        self.unigrams.get(&token).copied().unwrap_or(0)
    }

    /// Get bigram count.
    pub fn bigram_count(&self, prev: TokenId, curr: TokenId) -> usize {
        self.bigrams.get(&(prev, curr)).copied().unwrap_or(0)
    }

    /// Get trigram count.
    pub fn trigram_count(&self, prev2: TokenId, prev1: TokenId, curr: TokenId) -> usize {
        self.trigrams
            .get(&(prev2, prev1, curr))
            .copied()
            .unwrap_or(0)
    }

    /// Compute unigram probability.
    pub fn unigram_prob(&self, token: TokenId) -> f64 {
        if self.total == 0 {
            return 0.0;
        }
        self.unigram_count(token) as f64 / self.total as f64
    }

    /// Compute bigram probability.
    pub fn bigram_prob(&self, prev: TokenId, curr: TokenId) -> f64 {
        let prev_count = self.unigram_count(prev);
        if prev_count == 0 {
            return 0.0;
        }
        self.bigram_count(prev, curr) as f64 / prev_count as f64
    }
}

/// Build a pruned bigram transition graph.
///
/// # Arguments
///
/// * `vocab_size` - Number of tokens in vocabulary
/// * `counts` - N-gram counts for pruning
/// * `config` - Configuration options
///
/// # Returns
///
/// A WFST representing the pruned bigram transitions.
pub fn build_pruned_bigram_graph(
    vocab_size: usize,
    counts: &NgramCounts,
    config: &PrunedNgramConfig,
) -> VectorWfst<TokenId, LogWeight> {
    let mut fst = VectorWfst::new();

    // Create start state
    let start = fst.add_state();
    fst.set_start(start);
    fst.set_final(start, LogWeight::one());

    // Create state for each token (represents context)
    let mut token_states: HashMap<TokenId, StateId> = HashMap::new();
    for token in 0..vocab_size as TokenId {
        let state = fst.add_state();
        token_states.insert(token, state);
        fst.set_final(state, LogWeight::one());
    }

    // Back-off state (if using back-off)
    let backoff_state = if config.use_backoff {
        let state = fst.add_state();
        fst.set_final(state, LogWeight::one());
        Some(state)
    } else {
        None
    };

    // Add transitions from start to each token
    for token in 0..vocab_size as TokenId {
        let log_prob = if counts.total > 0 {
            let prob = counts.unigram_prob(token).max(1e-10);
            -prob.ln()
        } else {
            0.0
        };

        let to_state = token_states[&token];
        fst.add_arc(
            start,
            Some(token),
            Some(token),
            to_state,
            LogWeight::new(log_prob),
        );
    }

    // Add bigram transitions
    for prev in 0..vocab_size as TokenId {
        let from_state = token_states[&prev];
        let mut seen_tokens = HashSet::new();

        // Add direct transitions for observed bigrams above threshold
        for curr in 0..vocab_size as TokenId {
            let count = counts.bigram_count(prev, curr);
            if count >= config.min_count {
                let log_prob = if config.smoothing {
                    compute_smoothed_prob(counts, prev, curr, config)
                } else {
                    let prob = counts.bigram_prob(prev, curr).max(1e-10);
                    -prob.ln()
                };

                let to_state = token_states[&curr];
                fst.add_arc(
                    from_state,
                    Some(curr),
                    Some(curr),
                    to_state,
                    LogWeight::new(log_prob),
                );
                seen_tokens.insert(curr);
            }
        }

        // Add back-off transitions for unseen bigrams
        if config.use_backoff {
            if let Some(backoff) = backoff_state {
                // ε-transition to back-off state with back-off weight
                fst.add_arc(
                    from_state,
                    None,
                    None,
                    backoff,
                    LogWeight::new(config.backoff_weight),
                );
            }
        }
    }

    // From back-off state, allow all tokens with unigram probabilities
    if let Some(backoff) = backoff_state {
        for token in 0..vocab_size as TokenId {
            let log_prob = if counts.total > 0 {
                let prob = counts.unigram_prob(token).max(1e-10);
                -prob.ln()
            } else {
                0.0
            };

            let to_state = token_states[&token];
            fst.add_arc(
                backoff,
                Some(token),
                Some(token),
                to_state,
                LogWeight::new(log_prob),
            );
        }
    }

    fst
}

/// Compute smoothed probability using simple discounting.
fn compute_smoothed_prob(
    counts: &NgramCounts,
    prev: TokenId,
    curr: TokenId,
    config: &PrunedNgramConfig,
) -> f64 {
    let bigram_count = counts.bigram_count(prev, curr) as f64;
    let prev_count = counts.unigram_count(prev) as f64;

    if prev_count == 0.0 {
        return 0.0;
    }

    // Simple discounting: (c - d) / total + lambda * P_backoff
    let discounted = (bigram_count - config.discount).max(0.0) / prev_count;
    let unigram_prob = counts.unigram_prob(curr);
    let lambda = config.discount / prev_count; // Normalization factor

    let prob = (discounted + lambda * unigram_prob).max(1e-10);
    -prob.ln()
}

/// Build a pruned trigram transition graph.
///
/// Uses a two-level structure: states encode the two-token history.
pub fn build_pruned_trigram_graph(
    vocab_size: usize,
    counts: &NgramCounts,
    config: &PrunedNgramConfig,
) -> VectorWfst<TokenId, LogWeight> {
    let mut fst = VectorWfst::new();

    // Create start state
    let start = fst.add_state();
    fst.set_start(start);
    fst.set_final(start, LogWeight::one());

    // State for single-token history (used after start)
    let mut unigram_states: HashMap<TokenId, StateId> = HashMap::new();
    for token in 0..vocab_size as TokenId {
        let state = fst.add_state();
        unigram_states.insert(token, state);
        fst.set_final(state, LogWeight::one());
    }

    // States for bigram history
    let mut bigram_states: HashMap<(TokenId, TokenId), StateId> = HashMap::new();
    for &(prev, curr) in counts.bigrams.keys() {
        if counts.bigram_count(prev, curr) >= config.min_count {
            let state = fst.add_state();
            bigram_states.insert((prev, curr), state);
            fst.set_final(state, LogWeight::one());
        }
    }

    // Back-off state for bigram level
    let bigram_backoff = if config.use_backoff {
        let state = fst.add_state();
        fst.set_final(state, LogWeight::one());
        Some(state)
    } else {
        None
    };

    // Transitions from start -> unigram states
    for token in 0..vocab_size as TokenId {
        let log_prob = if counts.total > 0 {
            let prob = counts.unigram_prob(token).max(1e-10);
            -prob.ln()
        } else {
            0.0
        };
        let to_state = unigram_states[&token];
        fst.add_arc(
            start,
            Some(token),
            Some(token),
            to_state,
            LogWeight::new(log_prob),
        );
    }

    // Transitions from unigram -> bigram states
    for prev in 0..vocab_size as TokenId {
        let from_state = unigram_states[&prev];

        for curr in 0..vocab_size as TokenId {
            let count = counts.bigram_count(prev, curr);
            if count >= config.min_count {
                if let Some(&to_state) = bigram_states.get(&(prev, curr)) {
                    let prob = counts.bigram_prob(prev, curr).max(1e-10);
                    fst.add_arc(
                        from_state,
                        Some(curr),
                        Some(curr),
                        to_state,
                        LogWeight::new(-prob.ln()),
                    );
                }
            }
        }

        // Back-off to bigram_backoff
        if let Some(backoff) = bigram_backoff {
            fst.add_arc(
                from_state,
                None,
                None,
                backoff,
                LogWeight::new(config.backoff_weight),
            );
        }
    }

    // From bigram_backoff, allow all tokens with unigram probabilities
    if let Some(backoff) = bigram_backoff {
        for token in 0..vocab_size as TokenId {
            let prob = counts.unigram_prob(token).max(1e-10);
            // Go to unigram state after back-off
            let to_state = unigram_states[&token];
            fst.add_arc(
                backoff,
                Some(token),
                Some(token),
                to_state,
                LogWeight::new(-prob.ln()),
            );
        }
    }

    // Transitions from bigram -> bigram states (trigrams)
    for (&(prev1, prev2), &from_state) in &bigram_states {
        for curr in 0..vocab_size as TokenId {
            let count = counts.trigram_count(prev1, prev2, curr);
            if count >= config.min_count {
                // Trigram observed: direct transition
                if let Some(&to_state) = bigram_states.get(&(prev2, curr)) {
                    let denom = counts.bigram_count(prev1, prev2) as f64;
                    let prob = if denom > 0.0 {
                        (count as f64 / denom).max(1e-10)
                    } else {
                        1e-10
                    };
                    fst.add_arc(
                        from_state,
                        Some(curr),
                        Some(curr),
                        to_state,
                        LogWeight::new(-prob.ln()),
                    );
                }
            }
        }

        // Back-off from bigram state
        if let Some(backoff) = bigram_backoff {
            fst.add_arc(
                from_state,
                None,
                None,
                backoff,
                LogWeight::new(config.backoff_weight),
            );
        }
    }

    fst
}

/// Statistics about the pruned n-gram graph.
#[derive(Clone, Debug, Default)]
pub struct PrunedNgramStats {
    /// Number of states in the graph.
    pub num_states: usize,
    /// Number of arcs in the graph.
    pub num_arcs: usize,
    /// Number of unique n-grams kept.
    pub ngrams_kept: usize,
    /// Number of unique n-grams pruned.
    pub ngrams_pruned: usize,
    /// Pruning ratio.
    pub pruning_ratio: f64,
    /// Comparison: dense graph would have this many arcs.
    pub dense_arcs: usize,
    /// Compression ratio.
    pub compression_ratio: f64,
}

impl PrunedNgramStats {
    /// Compute statistics for a pruned n-gram graph.
    pub fn from_bigram_graph<L: Clone + Send + Sync>(
        fst: &VectorWfst<L, LogWeight>,
        vocab_size: usize,
    ) -> Self {
        let num_states = fst.num_states();
        let num_arcs: usize = (0..num_states as StateId)
            .map(|s| fst.transitions(s).len())
            .sum();

        // Dense bigram graph would have vocab_size^2 arcs
        let dense_arcs = vocab_size * vocab_size;

        Self {
            num_states,
            num_arcs,
            ngrams_kept: 0, // Would need to track during construction
            ngrams_pruned: 0,
            pruning_ratio: 0.0,
            dense_arcs,
            compression_ratio: if num_arcs > 0 {
                dense_arcs as f64 / num_arcs as f64
            } else {
                0.0
            },
        }
    }
}

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

    #[test]
    fn test_pruned_ngram_config_default() {
        let config = PrunedNgramConfig::default();
        assert_eq!(config.order, 2);
        assert_eq!(config.min_count, 1);
        assert!(config.use_backoff);
    }

    #[test]
    fn test_ngram_counts_empty() {
        let counts = NgramCounts::new();
        assert_eq!(counts.total, 0);
        assert_eq!(counts.unigram_count(0), 0);
    }

    #[test]
    fn test_ngram_counts_add_sequence() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[1, 2, 3, 1, 2]);

        assert_eq!(counts.unigram_count(1), 2);
        assert_eq!(counts.unigram_count(2), 2);
        assert_eq!(counts.unigram_count(3), 1);
        assert_eq!(counts.total, 5);

        assert_eq!(counts.bigram_count(1, 2), 2);
        assert_eq!(counts.bigram_count(2, 3), 1);
        assert_eq!(counts.bigram_count(3, 1), 1);
    }

    #[test]
    fn test_ngram_counts_probabilities() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 0, 1]);

        // Unigram: 0 appears 2 times, 1 appears 2 times, total 4
        assert!((counts.unigram_prob(0) - 0.5).abs() < 1e-6);
        assert!((counts.unigram_prob(1) - 0.5).abs() < 1e-6);

        // Bigram: P(1|0) = 2/2 = 1.0 (0->1 appears 2 times, 0 appears 2 times)
        assert!((counts.bigram_prob(0, 1) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_build_pruned_bigram_graph() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 2, 0, 1]);

        let config = PrunedNgramConfig::default();
        let fst = build_pruned_bigram_graph(3, &counts, &config);

        assert!(fst.start() != NO_STATE);
        assert!(fst.num_states() > 0);
    }

    #[test]
    fn test_pruned_bigram_with_threshold() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 0, 1, 0, 1]); // 0->1 appears 3 times

        let config = PrunedNgramConfig {
            min_count: 2,
            ..Default::default()
        };
        let fst = build_pruned_bigram_graph(3, &counts, &config);

        // Should prune infrequent bigrams
        assert!(fst.num_states() > 0);
    }

    #[test]
    fn test_build_pruned_trigram_graph() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 2, 0, 1, 2]);

        let config = PrunedNgramConfig {
            order: 3,
            ..Default::default()
        };
        let fst = build_pruned_trigram_graph(3, &counts, &config);

        assert!(fst.start() != NO_STATE);
        assert!(fst.num_states() > 0);
    }

    #[test]
    fn test_pruned_ngram_stats() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 2]);

        let config = PrunedNgramConfig::default();
        let fst = build_pruned_bigram_graph(10, &counts, &config);

        let stats = PrunedNgramStats::from_bigram_graph(&fst, 10);
        assert!(stats.num_states > 0);
        assert!(stats.num_arcs > 0);
        assert_eq!(stats.dense_arcs, 100); // 10^2
    }

    #[test]
    fn test_backoff_disabled() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1]);

        let config = PrunedNgramConfig {
            use_backoff: false,
            ..Default::default()
        };
        let fst = build_pruned_bigram_graph(3, &counts, &config);

        // Should still work without back-off
        assert!(fst.start() != NO_STATE);
    }

    #[test]
    fn test_smoothing_enabled() {
        let mut counts = NgramCounts::new();
        counts.add_sequence(&[0, 1, 2]);

        let config = PrunedNgramConfig {
            smoothing: true,
            discount: 0.5,
            ..Default::default()
        };
        let fst = build_pruned_bigram_graph(3, &counts, &config);

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

    #[test]
    fn test_compression_ratio() {
        let mut counts = NgramCounts::new();
        // Only observe a few bigrams
        counts.add_sequence(&[0, 1, 0, 1]);

        let config = PrunedNgramConfig {
            min_count: 2, // Only keep bigram (0,1)
            ..Default::default()
        };
        let fst = build_pruned_bigram_graph(100, &counts, &config);

        let stats = PrunedNgramStats::from_bigram_graph(&fst, 100);

        // Dense would have 10000 arcs, pruned should have far fewer
        assert!(stats.num_arcs < stats.dense_arcs);
        assert!(stats.compression_ratio > 1.0);
    }
}