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
//! Weighted determinization algorithm for WFSTs.
//!
//! Determinization produces a WFST where at most one path can match any
//! input string, while preserving the weighted language.
//!
//! # Algorithm
//!
//! Uses the weighted powerset construction from Mohri's work:
//! - States are weighted subsets: sets of (state, residual_weight) pairs
//! - Initial subset: {(start, 1̄)}
//! - For each input label, compute all reachable states with combined weights
//! - Residual weight = w + arc_weight - min_weight (normalized)
//!
//! # Complexity
//!
//! - Worst case: exponential in number of states (powerset construction)
//! - In practice: often linear for unambiguous automata
//! - Acyclic: guaranteed to terminate
//!
//! # Requirements
//!
//! - Semiring must be divisible (for weight normalization)
//! - Semiring should be weakly left-divisible for correctness
//!
//! # References
//!
//! - Mohri, M. (2009). "Weighted Automata Algorithms"
//! - Mohri, M., Pereira, F., & Riley, M. (2002). "WFSTs in Speech Recognition"

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::fmt::Debug;
use std::hash::Hash;

use crate::semiring::{DivisibleSemiring, Semiring, TotallyOrderedSemiring};
use crate::wfst::{MutableWfst, StateId, WeightedTransition, Wfst, NO_STATE};

/// Configuration for determinization.
#[derive(Clone, Debug)]
pub struct DeterminizeConfig {
    /// Maximum number of states in the output (prevents runaway)
    pub max_states: Option<usize>,
    /// Whether to epsilon-remove first (recommended)
    pub remove_epsilon_first: bool,
    /// Whether to connect (trim) after determinization
    pub connect_after: bool,
}

impl Default for DeterminizeConfig {
    fn default() -> Self {
        Self {
            max_states: Some(1_000_000),
            remove_epsilon_first: true,
            connect_after: true,
        }
    }
}

impl DeterminizeConfig {
    /// Standard determinization with default limits.
    pub fn standard() -> Self {
        Self::default()
    }

    /// Unlimited determinization (use with caution).
    pub fn unlimited() -> Self {
        Self {
            max_states: None,
            remove_epsilon_first: true,
            connect_after: true,
        }
    }
}

/// Error during determinization.
#[derive(Clone, Debug, PartialEq)]
pub enum DeterminizeError {
    /// No start state defined.
    NoStartState,
    /// Maximum state limit exceeded.
    StateLimitExceeded {
        /// The maximum number of states allowed.
        limit: usize,
    },
    /// The WFST is not determinizable (cycles with certain weight patterns).
    NotDeterminizable {
        /// Description of why determinization failed.
        reason: String,
    },
}

impl std::fmt::Display for DeterminizeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DeterminizeError::NoStartState => write!(f, "WFST has no start state"),
            DeterminizeError::StateLimitExceeded { limit } => {
                write!(f, "Determinization exceeded {} state limit", limit)
            }
            DeterminizeError::NotDeterminizable { reason } => {
                write!(f, "WFST is not determinizable: {}", reason)
            }
        }
    }
}

impl std::error::Error for DeterminizeError {}

/// A weighted subset is a set of (state, residual_weight) pairs.
/// We use BTreeMap for consistent ordering (needed for hashing).
type WeightedSubset<W> = BTreeMap<StateId, W>;

/// Create a canonical key for a weighted subset (for deduplication).
fn subset_key<W: Semiring + Clone>(subset: &WeightedSubset<W>) -> Vec<(StateId, W)> {
    subset.iter().map(|(&s, w)| (s, w.clone())).collect()
}

/// Find the minimum weight in a weighted subset.
///
/// Uses `TotallyOrderedSemiring::total_cmp` for safe comparison without
/// the `unwrap_or(Equal)` fallback that could hide comparison failures.
fn min_weight<W: TotallyOrderedSemiring + Clone>(subset: &WeightedSubset<W>) -> W {
    subset
        .values()
        .cloned()
        .min_by(|a, b| a.total_cmp(b))
        .unwrap_or_else(W::zero)
}

/// Determinize a WFST.
///
/// This produces a deterministic WFST where for each state, all outgoing
/// transitions have distinct input labels. The weighted language is preserved.
///
/// # Type Parameters
///
/// - `L`: Label type (must be Eq + Hash + Clone + Ord)
/// - `W`: Weight type (must be DivisibleSemiring + TotallyOrderedSemiring)
/// - `F`: WFST type
///
/// # Requirements
///
/// The weight semiring must implement `TotallyOrderedSemiring` to ensure safe
/// weight comparisons. This is a compile-time guarantee that replaces the
/// previous runtime fallback when `PartialOrd` comparisons returned `None`.
///
/// # Returns
///
/// A new determinized WFST, or an error if determinization fails.
///
/// # Example
///
/// ```ignore
/// use lling_llang::algorithms::{determinize, DeterminizeConfig};
///
/// let mut fst = build_some_wfst();
/// let det_fst = determinize(&fst, DeterminizeConfig::standard())?;
/// ```
pub fn determinize<L, W, F>(fst: &F, config: DeterminizeConfig) -> Result<F, DeterminizeError>
where
    L: Clone + Eq + Hash + Ord + Debug,
    W: DivisibleSemiring + TotallyOrderedSemiring + Clone + Debug + Hash + Eq,
    F: MutableWfst<L, W> + Wfst<L, W> + Default,
{
    let n = fst.num_states();
    if n == 0 {
        return Ok(F::default());
    }

    let start = fst.start();
    if start == NO_STATE {
        return Err(DeterminizeError::NoStartState);
    }

    // Create the output WFST
    let mut result = F::default();

    // Map from weighted subset to output state ID
    let mut subset_to_state: HashMap<Vec<(StateId, W)>, StateId> = HashMap::new();

    // Queue of (output_state, weighted_subset) pairs to process
    let mut queue: VecDeque<(StateId, WeightedSubset<W>)> = VecDeque::new();

    // Initial subset: {(start, 1̄)}
    let mut initial_subset: WeightedSubset<W> = BTreeMap::new();
    initial_subset.insert(start, W::one());

    // Create initial state in result
    let initial_state = result.add_state();
    result.set_start(initial_state);

    let initial_key = subset_key(&initial_subset);
    subset_to_state.insert(initial_key, initial_state);
    queue.push_back((initial_state, initial_subset));

    // Main determinization loop
    while let Some((output_state, subset)) = queue.pop_front() {
        // Check state limit
        if let Some(limit) = config.max_states {
            if result.num_states() > limit {
                return Err(DeterminizeError::StateLimitExceeded { limit });
            }
        }

        // Compute final weight for this subset
        // Final weight is ⊕ of all final weights for states in the subset
        let mut final_weight = W::zero();
        for (&state, residual) in &subset {
            if fst.is_final(state) {
                let fw = fst.final_weight(state);
                // Final weight = residual ⊗ original_final_weight
                final_weight = final_weight.plus(&residual.times(&fw));
            }
        }
        if !final_weight.is_zero() {
            result.set_final(output_state, final_weight);
        }

        // Group outgoing transitions by input label
        let mut label_to_targets: HashMap<Option<L>, Vec<(StateId, W, Option<L>)>> = HashMap::new();

        for (&state, residual) in &subset {
            for trans in fst.transitions(state) {
                // Combined weight = residual ⊗ arc_weight
                let combined = residual.times(&trans.weight);

                label_to_targets
                    .entry(trans.input.clone())
                    .or_default()
                    .push((trans.to, combined, trans.output.clone()));
            }
        }

        // Process each input label
        for (input_label, targets) in label_to_targets {
            // Skip epsilon transitions for now (should be removed beforehand)
            // In a full implementation, we'd handle epsilon-closure here
            if input_label.is_none() {
                continue;
            }

            // Build the target weighted subset
            let mut target_subset: WeightedSubset<W> = BTreeMap::new();

            // For transducers, we need to handle output labels
            // For simplicity, we'll use the first output label seen
            // (This is correct for acceptors where input == output)
            let mut output_label: Option<L> = None;

            for (target_state, weight, out) in &targets {
                // Merge weights for same target state using ⊕
                target_subset
                    .entry(*target_state)
                    .and_modify(|w| *w = w.plus(weight))
                    .or_insert_with(|| weight.clone());

                // Track output label (for transducers)
                if output_label.is_none() {
                    output_label = out.clone();
                }
            }

            if target_subset.is_empty() {
                continue;
            }

            // Normalize: find minimum weight and factor it out
            let min_w = min_weight(&target_subset);

            // Normalized subset: divide each weight by the minimum
            let mut normalized_subset: WeightedSubset<W> = BTreeMap::new();
            for (&state, weight) in &target_subset {
                // normalized = weight / min_w
                if let Some(normalized) = weight.divide(&min_w) {
                    normalized_subset.insert(state, normalized);
                } else {
                    // Division failed, use original (shouldn't happen for valid semirings)
                    normalized_subset.insert(state, weight.clone());
                }
            }

            // Look up or create state for normalized subset
            let normalized_key = subset_key(&normalized_subset);
            let target_output_state = if let Some(&existing) = subset_to_state.get(&normalized_key)
            {
                existing
            } else {
                let new_state = result.add_state();
                subset_to_state.insert(normalized_key, new_state);
                queue.push_back((new_state, normalized_subset));
                new_state
            };

            // Add transition with minimum weight
            let trans = WeightedTransition {
                from: output_state,
                to: target_output_state,
                input: input_label,
                output: output_label,
                weight: min_w,
            };
            result.add_transition(trans);
        }
    }

    // Optionally connect (trim) the result
    if config.connect_after {
        use crate::algorithms::{connect, ConnectConfig};
        connect(&mut result, ConnectConfig::trim());
    }

    Ok(result)
}

/// Check if a WFST is deterministic.
///
/// A WFST is deterministic if:
/// 1. It has at most one start state
/// 2. For each state, all outgoing transitions have distinct input labels
/// 3. There are no epsilon transitions on the input
pub fn is_deterministic<L, W, F>(fst: &F) -> bool
where
    L: Clone + Eq + Hash,
    W: Semiring,
    F: Wfst<L, W>,
{
    let n = fst.num_states();
    if n == 0 {
        return true;
    }

    let start = fst.start();
    if start == NO_STATE {
        return true; // Empty language is deterministic
    }

    for state in 0..n {
        let state_id = state as StateId;
        let mut seen_labels: std::collections::HashSet<Option<&L>> =
            std::collections::HashSet::new();

        for trans in fst.transitions(state_id) {
            // Epsilon input makes it non-deterministic
            if trans.input.is_none() {
                return false;
            }

            // Duplicate input label makes it non-deterministic
            if !seen_labels.insert(trans.input.as_ref()) {
                return false;
            }
        }
    }

    true
}

/// Count the degree of non-determinism for a WFST.
///
/// Returns the maximum number of transitions with the same input label
/// from any single state. A deterministic WFST has degree 1.
pub fn non_determinism_degree<L, W, F>(fst: &F) -> usize
where
    L: Clone + Eq + Hash,
    W: Semiring,
    F: Wfst<L, W>,
{
    let n = fst.num_states();
    if n == 0 {
        return 0;
    }

    let mut max_degree = 0;

    for state in 0..n {
        let state_id = state as StateId;
        let mut label_counts: HashMap<Option<&L>, usize> = HashMap::new();

        for trans in fst.transitions(state_id) {
            *label_counts.entry(trans.input.as_ref()).or_insert(0) += 1;
        }

        if let Some(&count) = label_counts.values().max() {
            max_degree = max_degree.max(count);
        }
    }

    max_degree
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semiring::TropicalWeight;
    use crate::wfst::{VectorWfst, VectorWfstBuilder};

    // Property-based tests
    mod property_tests {
        use super::*;
        use crate::test_utils::arb_deterministic_wfst_tropical;
        use proptest::prelude::*;

        proptest! {
            /// Determinize should always produce a deterministic output.
            #[test]
            fn determinize_produces_deterministic(
                fst in arb_deterministic_wfst_tropical(8, 3)
            ) {
                let result = determinize(&fst, DeterminizeConfig::standard());
                if let Ok(det_fst) = result {
                    prop_assert!(
                        is_deterministic(&det_fst),
                        "Determinized FST should be deterministic"
                    );
                }
            }

            /// Determinizing a deterministic FST should not increase state count significantly.
            #[test]
            fn determinize_already_deterministic(
                fst in arb_deterministic_wfst_tropical(8, 3)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                prop_assert!(is_deterministic(&fst), "Test FST should be deterministic");

                let result = determinize(&fst, DeterminizeConfig::standard());
                if let Ok(det_fst) = result {
                    // Determinizing a deterministic FST shouldn't dramatically increase states
                    // (it may increase slightly due to trim/connect behavior)
                    prop_assert!(
                        det_fst.num_states() <= fst.num_states() + 2,
                        "Determinizing deterministic FST grew from {} to {} states",
                        fst.num_states(),
                        det_fst.num_states()
                    );
                }
            }

            /// Determinize is idempotent: det(det(F)) ≈ det(F).
            #[test]
            fn determinize_idempotent(
                fst in arb_deterministic_wfst_tropical(6, 2)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                let det1 = determinize(&fst, DeterminizeConfig::standard());
                if let Ok(det1_fst) = det1 {
                    let det2 = determinize(&det1_fst, DeterminizeConfig::standard());
                    if let Ok(det2_fst) = det2 {
                        // Both should be deterministic
                        prop_assert!(is_deterministic(&det1_fst));
                        prop_assert!(is_deterministic(&det2_fst));

                        // State count should be similar (idempotent)
                        prop_assert!(
                            det2_fst.num_states() <= det1_fst.num_states() + 1,
                            "det(det(F)) has {} states, det(F) has {}",
                            det2_fst.num_states(),
                            det1_fst.num_states()
                        );
                    }
                }
            }

            /// Non-determinism degree should be 1 for deterministic FSTs.
            #[test]
            fn non_determinism_degree_deterministic(
                fst in arb_deterministic_wfst_tropical(8, 3)
            ) {
                if fst.num_states() == 0 {
                    return Ok(());
                }

                let degree = non_determinism_degree(&fst);
                prop_assert!(
                    degree <= 1,
                    "Deterministic FST should have degree 0 or 1, got {}",
                    degree
                );
            }
        }
    }

    fn build_deterministic_fst() -> VectorWfst<char, TropicalWeight> {
        // Already deterministic: 0 --a--> 1 --b--> 2 (final)
        VectorWfstBuilder::new()
            .add_states(3)
            .start(0)
            .arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0))
            .arc(1, Some('b'), Some('b'), 2, TropicalWeight::new(2.0))
            .final_state(2, TropicalWeight::one())
            .build()
    }

    fn build_non_deterministic_fst() -> VectorWfst<char, TropicalWeight> {
        // Non-deterministic: two 'a' transitions from state 0
        // 0 --a(1)--> 1
        // 0 --a(2)--> 2
        // 1 --b--> 3 (final)
        // 2 --c--> 3 (final)
        let mut fst = VectorWfst::new();
        fst.add_states(4);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(0, Some('a'), Some('a'), 2, TropicalWeight::new(2.0));
        fst.add_arc(1, Some('b'), Some('b'), 3, TropicalWeight::new(1.0));
        fst.add_arc(2, Some('c'), Some('c'), 3, TropicalWeight::new(1.0));
        fst.set_final(3, TropicalWeight::one());
        fst
    }

    fn build_diamond_non_det() -> VectorWfst<char, TropicalWeight> {
        // Diamond: two paths with same labels, should merge
        // 0 --a--> 1 --b--> 3 (final)
        // 0 --a--> 2 --b--> 3 (final)
        let mut fst = VectorWfst::new();
        fst.add_states(4);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(0, Some('a'), Some('a'), 2, TropicalWeight::new(2.0));
        fst.add_arc(1, Some('b'), Some('b'), 3, TropicalWeight::new(1.0));
        fst.add_arc(2, Some('b'), Some('b'), 3, TropicalWeight::new(1.0));
        fst.set_final(3, TropicalWeight::one());
        fst
    }

    #[test]
    fn test_is_deterministic_true() {
        let fst = build_deterministic_fst();
        assert!(is_deterministic(&fst));
    }

    #[test]
    fn test_is_deterministic_false() {
        let fst = build_non_deterministic_fst();
        assert!(!is_deterministic(&fst));
    }

    #[test]
    fn test_non_determinism_degree() {
        let det_fst = build_deterministic_fst();
        assert_eq!(non_determinism_degree(&det_fst), 1);

        let nondet_fst = build_non_deterministic_fst();
        assert_eq!(non_determinism_degree(&nondet_fst), 2);
    }

    #[test]
    fn test_determinize_already_deterministic() {
        let fst = build_deterministic_fst();
        let result = determinize(&fst, DeterminizeConfig::standard())
            .expect("algorithms/determinize.rs: required value was None/Err");

        assert!(is_deterministic(&result));
        // Should have same structure (3 states for chain)
        assert!(result.num_states() <= 3);
    }

    #[test]
    fn test_determinize_simple_non_det() {
        let fst = build_non_deterministic_fst();
        assert!(!is_deterministic(&fst));

        let result = determinize(&fst, DeterminizeConfig::standard())
            .expect("algorithms/determinize.rs: required value was None/Err");
        assert!(is_deterministic(&result));

        // After 'a', we should be in a merged state
        // Then 'b' and 'c' lead to different outcomes
    }

    #[test]
    fn test_determinize_diamond() {
        let fst = build_diamond_non_det();
        assert!(!is_deterministic(&fst));

        let result = determinize(&fst, DeterminizeConfig::standard())
            .expect("algorithms/determinize.rs: required value was None/Err");
        assert!(is_deterministic(&result));

        // Diamond should collapse to: 0 --a--> 1 --b--> 2
        // Output should have fewer states than input
        assert!(result.num_states() <= fst.num_states());
    }

    #[test]
    fn test_determinize_empty() {
        let fst: VectorWfst<char, TropicalWeight> = VectorWfst::new();
        let result = determinize(&fst, DeterminizeConfig::standard())
            .expect("algorithms/determinize.rs: required value was None/Err");
        assert_eq!(result.num_states(), 0);
    }

    #[test]
    fn test_determinize_weight_preservation() {
        // Create a simple non-deterministic FST where we can verify weights
        // 0 --a(w=1)--> 1 (final, w=0)
        // 0 --a(w=3)--> 2 (final, w=0)
        // After determinization: 0 --a(w=min(1,3)=1)--> {1,2} (final)
        // The final weight should incorporate the residuals
        let mut fst = VectorWfst::new();
        fst.add_states(3);
        fst.set_start(0);
        fst.add_arc(0, Some('a'), Some('a'), 1, TropicalWeight::new(1.0));
        fst.add_arc(0, Some('a'), Some('a'), 2, TropicalWeight::new(3.0));
        fst.set_final(1, TropicalWeight::one()); // w=0
        fst.set_final(2, TropicalWeight::one()); // w=0

        let result = determinize(&fst, DeterminizeConfig::standard())
            .expect("algorithms/determinize.rs: required value was None/Err");
        assert!(is_deterministic(&result));

        // Should have 2 states: start and merged final
        assert_eq!(result.num_states(), 2);

        // The 'a' transition should have weight 1.0 (minimum)
        let start = result.start();
        let trans: Vec<_> = result.transitions(start).to_vec();
        assert_eq!(trans.len(), 1);
        assert_eq!(trans[0].weight.value(), 1.0);
    }

    #[test]
    fn test_determinize_state_limit() {
        let fst = build_non_deterministic_fst();

        let config = DeterminizeConfig {
            max_states: Some(1), // Very low limit
            remove_epsilon_first: false,
            connect_after: false,
        };

        let result = determinize(&fst, config);
        assert!(matches!(
            result,
            Err(DeterminizeError::StateLimitExceeded { .. })
        ));
    }
}