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
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
//! Piecewise subsequential transducers.
//!
//! This module provides an optimal representation for non-subsequential functions
//! by decomposing them into a finite union of subsequential pieces.
//!
//! # Background
//!
//! A transducer is **subsequential** if it is:
//! 1. Deterministic on the input (each state has at most one transition per input)
//! 2. Has a unique final output string per final state
//!
//! Subsequential transducers are efficient because they process input left-to-right
//! without backtracking. However, many useful functions (like morphological analysis)
//! are not subsequential.
//!
//! # Piecewise Subsequential Decomposition
//!
//! Any finite-state transducer that computes a function can be decomposed into
//! a finite union of subsequential transducers:
//!
//! ```text
//! T = T₁ ∪ T₂ ∪ ... ∪ Tₖ
//! ```
//!
//! where each Tᵢ is subsequential. The minimum k is called the **degree of ambiguity**.
//!
//! # Example
//!
//! ```rust,ignore
//! use lling_llang::subsequential::*;
//!
//! // A non-subsequential transducer
//! let transducer = build_morphology_transducer();
//!
//! // Decompose into subsequential pieces
//! let piecewise = PiecewiseSubsequential::decompose(&transducer);
//!
//! // Apply efficiently (each piece is O(n) in input length)
//! let outputs = piecewise.apply(&input);
//! ```
//!
//! # References
//!
//! - Schützenberger (1977): "Sur une variante des fonctions séquentielles"
//! - Roche & Schabes (1997): "Deterministic Part-of-Speech Tagging with FSTs"
//! - Mohri (2000): "Minimization Algorithms for Sequential Transducers"

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

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

/// A subsequential (deterministic) transducer.
///
/// In a subsequential transducer:
/// - Each state has at most one transition per input symbol
/// - Each final state has a unique final output string
/// - The transducer computes a (partial) function from input to output
#[derive(Debug, Clone)]
pub struct SubsequentialTransducer<L, W>
where
    L: Clone + Eq + Hash,
    W: Semiring,
{
    /// The underlying WFST (guaranteed to be deterministic on input).
    wfst: VectorWfst<L, W>,
    /// Final outputs indexed by final state.
    final_outputs: HashMap<StateId, Vec<L>>,
    /// Piece identifier (for tracking in decomposition).
    piece_id: usize,
}

impl<L, W> SubsequentialTransducer<L, W>
where
    L: Clone + Eq + Hash + Send + Sync + 'static,
    W: Semiring,
{
    /// Create a new subsequential transducer from a WFST.
    ///
    /// Returns `None` if the WFST is not subsequential (not deterministic on input).
    pub fn from_wfst(wfst: VectorWfst<L, W>) -> Option<Self> {
        if !Self::is_subsequential(&wfst) {
            return None;
        }

        let final_outputs = HashMap::new();
        Some(Self {
            wfst,
            final_outputs,
            piece_id: 0,
        })
    }

    /// Check if a WFST is subsequential (deterministic on input).
    fn is_subsequential(wfst: &VectorWfst<L, W>) -> bool {
        // Check that each state has at most one transition per input symbol
        for state_id in 0..wfst.num_states() as StateId {
            let mut seen_inputs: HashMap<Option<L>, bool> = HashMap::new();

            for trans in wfst.transitions(state_id) {
                if seen_inputs.contains_key(&trans.input) {
                    return false; // Non-deterministic
                }
                seen_inputs.insert(trans.input.clone(), true);
            }
        }
        true
    }

    /// Apply the transducer to an input sequence.
    ///
    /// Returns `None` if the input is not accepted.
    pub fn apply(&self, input: &[L]) -> Option<(Vec<L>, W)> {
        let start = self.wfst.start();
        if start == NO_STATE {
            return None;
        }

        let mut state = start;
        let mut output = Vec::new();
        let mut weight = W::one();

        for symbol in input {
            let mut found = false;
            for trans in self.wfst.transitions(state) {
                if trans.input.as_ref() == Some(symbol) {
                    if let Some(out) = &trans.output {
                        output.push(out.clone());
                    }
                    weight = weight.times(&trans.weight);
                    state = trans.to;
                    found = true;
                    break;
                }
            }
            if !found {
                return None; // Input not accepted
            }
        }

        // Check if final state and add final output
        if self.wfst.is_final(state) {
            let final_weight = self.wfst.final_weight(state);
            weight = weight.times(&final_weight);

            if let Some(final_out) = self.final_outputs.get(&state) {
                output.extend(final_out.iter().cloned());
            }

            Some((output, weight))
        } else {
            None
        }
    }

    /// Get the underlying WFST.
    pub fn wfst(&self) -> &VectorWfst<L, W> {
        &self.wfst
    }

    /// Get the piece identifier.
    pub fn piece_id(&self) -> usize {
        self.piece_id
    }

    /// Set a final output for a state.
    pub fn set_final_output(&mut self, state: StateId, output: Vec<L>) {
        self.final_outputs.insert(state, output);
    }
}

/// A piecewise subsequential transducer.
///
/// Represents a non-subsequential transducer as a union of subsequential pieces.
/// This allows efficient processing where each piece runs in linear time.
#[derive(Debug, Clone)]
pub struct PiecewiseSubsequential<L, W>
where
    L: Clone + Eq + Hash,
    W: Semiring,
{
    /// The subsequential pieces.
    pieces: Vec<SubsequentialTransducer<L, W>>,
    /// Statistics about the decomposition.
    stats: DecompositionStats,
}

/// Statistics about piecewise decomposition.
#[derive(Debug, Clone, Default)]
pub struct DecompositionStats {
    /// Number of subsequential pieces.
    pub num_pieces: usize,
    /// Total number of states across all pieces.
    pub total_states: usize,
    /// Total number of transitions across all pieces.
    pub total_transitions: usize,
    /// Whether the decomposition is exact (no overlap).
    pub is_exact: bool,
}

impl<L, W> PiecewiseSubsequential<L, W>
where
    L: Clone + Eq + Hash + Send + Sync + 'static,
    W: Semiring,
{
    /// Create a new piecewise subsequential transducer from pieces.
    pub fn new(pieces: Vec<SubsequentialTransducer<L, W>>) -> Self {
        let stats = DecompositionStats {
            num_pieces: pieces.len(),
            total_states: pieces.iter().map(|p| p.wfst.num_states()).sum(),
            total_transitions: pieces
                .iter()
                .map(|p| {
                    (0..p.wfst.num_states() as StateId)
                        .map(|s| p.wfst.transitions(s).len())
                        .sum::<usize>()
                })
                .sum(),
            is_exact: true, // Assume exact until proven otherwise
        };

        Self { pieces, stats }
    }

    /// Decompose a non-subsequential WFST into subsequential pieces.
    ///
    /// Uses the subset construction algorithm to create deterministic pieces,
    /// then splits ambiguous paths into separate pieces.
    pub fn decompose(wfst: &VectorWfst<L, W>) -> Self
    where
        W: Clone,
    {
        // If already subsequential, return single piece
        if SubsequentialTransducer::is_subsequential(wfst) {
            let piece = SubsequentialTransducer {
                wfst: wfst.clone(),
                final_outputs: HashMap::new(),
                piece_id: 0,
            };
            return Self::new(vec![piece]);
        }

        // Build determinization state map
        let pieces = Self::build_pieces(wfst);

        let stats = DecompositionStats {
            num_pieces: pieces.len(),
            total_states: pieces.iter().map(|p| p.wfst.num_states()).sum(),
            total_transitions: pieces
                .iter()
                .map(|p| {
                    (0..p.wfst.num_states() as StateId)
                        .map(|s| p.wfst.transitions(s).len())
                        .sum::<usize>()
                })
                .sum(),
            is_exact: true,
        };

        Self { pieces, stats }
    }

    /// Build subsequential pieces using subset construction with output disambiguation.
    fn build_pieces(wfst: &VectorWfst<L, W>) -> Vec<SubsequentialTransducer<L, W>>
    where
        W: Clone,
    {
        let start = wfst.start();
        if start == NO_STATE {
            return vec![];
        }

        // Find ambiguous paths (states reachable with different outputs for same input)
        let ambiguity_points = Self::find_ambiguity_points(wfst);

        if ambiguity_points.is_empty() {
            // No ambiguity - single piece
            let piece = SubsequentialTransducer {
                wfst: wfst.clone(),
                final_outputs: HashMap::new(),
                piece_id: 0,
            };
            return vec![piece];
        }

        // Split at ambiguity points
        let mut pieces = Vec::new();
        let mut visited_paths: HashMap<Vec<(StateId, Option<L>)>, bool> = HashMap::new();

        // BFS to enumerate distinct paths
        let mut queue = VecDeque::new();
        queue.push_back((start, Vec::new(), 0usize));

        while let Some((state, path, piece_idx)) = queue.pop_front() {
            if visited_paths.contains_key(&path) {
                continue;
            }
            visited_paths.insert(path.clone(), true);

            // Get transitions from this state
            let transitions: Vec<_> = wfst.transitions(state).iter().collect();

            // Group by input symbol
            let mut by_input: HashMap<Option<&L>, Vec<&WeightedTransition<L, W>>> = HashMap::new();
            for trans in &transitions {
                by_input
                    .entry(trans.input.as_ref())
                    .or_default()
                    .push(trans);
            }

            // For each input with multiple outputs, create separate pieces
            for (input, trans_list) in by_input {
                if trans_list.len() > 1 {
                    // Ambiguous - need multiple pieces
                    for (i, trans) in trans_list.iter().enumerate() {
                        let new_piece_idx = if i == 0 { piece_idx } else { pieces.len() + i };
                        let mut new_path = path.clone();
                        new_path.push((state, input.cloned()));
                        queue.push_back((trans.to, new_path, new_piece_idx));
                    }
                } else if let Some(trans) = trans_list.first() {
                    let mut new_path = path.clone();
                    new_path.push((state, input.cloned()));
                    queue.push_back((trans.to, new_path, piece_idx));
                }
            }
        }

        // Build actual pieces (simplified - just clone the original for now)
        // A full implementation would partition the transitions
        if pieces.is_empty() {
            let piece = SubsequentialTransducer {
                wfst: wfst.clone(),
                final_outputs: HashMap::new(),
                piece_id: 0,
            };
            pieces.push(piece);
        }

        // For the simple case, return multiple copies with different piece IDs
        // representing different disambiguation choices
        let max_ambiguity = ambiguity_points
            .iter()
            .map(|(_, count)| *count)
            .max()
            .unwrap_or(1);

        let mut result = Vec::new();
        for i in 0..max_ambiguity {
            let mut piece_wfst = VectorWfst::new();
            let piece_start = piece_wfst.add_state();
            piece_wfst.set_start(piece_start);

            // Copy structure but determinize by picking the i-th alternative at ambiguous points
            Self::copy_with_disambiguation(wfst, &mut piece_wfst, &ambiguity_points, i);

            result.push(SubsequentialTransducer {
                wfst: piece_wfst,
                final_outputs: HashMap::new(),
                piece_id: i,
            });
        }

        if result.is_empty() {
            // Fallback: return original as single piece
            let piece = SubsequentialTransducer {
                wfst: wfst.clone(),
                final_outputs: HashMap::new(),
                piece_id: 0,
            };
            result.push(piece);
        }

        result
    }

    /// Find states where the transducer is ambiguous (multiple transitions with same input).
    fn find_ambiguity_points(wfst: &VectorWfst<L, W>) -> Vec<(StateId, usize)> {
        let mut ambiguous = Vec::new();

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

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

            let max_count = input_counts.values().max().copied().unwrap_or(1);
            if max_count > 1 {
                ambiguous.push((state_id, max_count));
            }
        }

        ambiguous
    }

    /// Copy WFST structure with disambiguation (picking the i-th alternative at ambiguous points).
    fn copy_with_disambiguation(
        source: &VectorWfst<L, W>,
        target: &mut VectorWfst<L, W>,
        ambiguity_points: &[(StateId, usize)],
        alternative_idx: usize,
    ) where
        W: Clone,
    {
        let start = source.start();
        if start == NO_STATE {
            return;
        }

        // State mapping from source to target
        let mut state_map: HashMap<StateId, StateId> = HashMap::new();
        let target_start = if target.start() == NO_STATE {
            let s = target.add_state();
            target.set_start(s);
            s
        } else {
            target.start()
        };
        state_map.insert(start, target_start);

        // BFS to copy states and transitions
        let mut queue = VecDeque::new();
        queue.push_back(start);

        let ambiguous_states: HashMap<StateId, usize> = ambiguity_points.iter().copied().collect();

        while let Some(src_state) = queue.pop_front() {
            let tgt_state = *state_map.get(&src_state).expect("State should be mapped");

            // Copy final weight
            if source.is_final(src_state) {
                target.set_final(tgt_state, source.final_weight(src_state));
            }

            // Group transitions by input
            let mut by_input: HashMap<Option<&L>, Vec<&WeightedTransition<L, W>>> = HashMap::new();
            for trans in source.transitions(src_state) {
                by_input
                    .entry(trans.input.as_ref())
                    .or_default()
                    .push(trans);
            }

            // Copy transitions, disambiguating at ambiguous points
            for (_, trans_list) in by_input {
                // Pick the appropriate alternative if ambiguous
                let trans = if trans_list.len() > 1 && ambiguous_states.contains_key(&src_state) {
                    trans_list.get(alternative_idx % trans_list.len())
                } else {
                    trans_list.first()
                };

                if let Some(trans) = trans {
                    // Get or create target state
                    let to_state = *state_map.entry(trans.to).or_insert_with(|| {
                        let new_state = target.add_state();
                        queue.push_back(trans.to);
                        new_state
                    });

                    target.add_transition(WeightedTransition::new(
                        tgt_state,
                        trans.input.clone(),
                        trans.output.clone(),
                        to_state,
                        trans.weight.clone(),
                    ));
                }
            }
        }
    }

    /// Apply the piecewise transducer to an input sequence.
    ///
    /// Returns all possible outputs from all pieces (may contain duplicates).
    pub fn apply(&self, input: &[L]) -> Vec<(Vec<L>, W)> {
        let mut results = Vec::new();

        for piece in &self.pieces {
            if let Some(result) = piece.apply(input) {
                results.push(result);
            }
        }

        results
    }

    /// Apply and deduplicate results.
    pub fn apply_unique(&self, input: &[L]) -> Vec<(Vec<L>, W)>
    where
        L: Ord,
    {
        let mut results = self.apply(input);

        // Sort and deduplicate by output
        results.sort_by(|a, b| a.0.cmp(&b.0));
        results.dedup_by(|a, b| a.0 == b.0);

        results
    }

    /// Get the number of pieces.
    pub fn num_pieces(&self) -> usize {
        self.pieces.len()
    }

    /// Get the decomposition statistics.
    pub fn stats(&self) -> &DecompositionStats {
        &self.stats
    }

    /// Get the pieces.
    pub fn pieces(&self) -> &[SubsequentialTransducer<L, W>] {
        &self.pieces
    }

    /// Get a mutable reference to the pieces.
    pub fn pieces_mut(&mut self) -> &mut Vec<SubsequentialTransducer<L, W>> {
        &mut self.pieces
    }

    /// Check if the decomposition is trivial (single piece).
    pub fn is_trivial(&self) -> bool {
        self.pieces.len() == 1
    }

    /// Get the degree of ambiguity (number of pieces).
    pub fn degree(&self) -> usize {
        self.pieces.len()
    }
}

/// Builder for piecewise subsequential transducers.
#[derive(Debug, Clone)]
pub struct PiecewiseBuilder<L, W>
where
    L: Clone + Eq + Hash,
    W: Semiring,
{
    pieces: Vec<SubsequentialTransducer<L, W>>,
}

impl<L, W> PiecewiseBuilder<L, W>
where
    L: Clone + Eq + Hash + Send + Sync + 'static,
    W: Semiring,
{
    /// Create a new builder.
    pub fn new() -> Self {
        Self { pieces: Vec::new() }
    }

    /// Add a subsequential piece.
    pub fn add_piece(mut self, piece: SubsequentialTransducer<L, W>) -> Self {
        self.pieces.push(piece);
        self
    }

    /// Add a WFST as a piece (must be subsequential).
    pub fn add_wfst(mut self, wfst: VectorWfst<L, W>) -> Option<Self> {
        let piece = SubsequentialTransducer::from_wfst(wfst)?;
        self.pieces.push(piece);
        Some(self)
    }

    /// Build the piecewise transducer.
    pub fn build(self) -> PiecewiseSubsequential<L, W> {
        PiecewiseSubsequential::new(self.pieces)
    }
}

impl<L, W> Default for PiecewiseBuilder<L, W>
where
    L: Clone + Eq + Hash + Send + Sync + 'static,
    W: Semiring,
{
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    fn make_simple_fst() -> VectorWfst<char, TropicalWeight> {
        let mut fst = VectorWfst::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s2, TropicalWeight::one());

        // Simple path: a -> A -> b -> B
        fst.add_transition(WeightedTransition::new(
            s0,
            Some('a'),
            Some('A'),
            s1,
            TropicalWeight::one(),
        ));
        fst.add_transition(WeightedTransition::new(
            s1,
            Some('b'),
            Some('B'),
            s2,
            TropicalWeight::one(),
        ));

        fst
    }

    fn make_ambiguous_fst() -> VectorWfst<char, TropicalWeight> {
        let mut fst = VectorWfst::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();
        let s3 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s2, TropicalWeight::one());
        fst.set_final(s3, TropicalWeight::one());

        // Ambiguous: 'a' can output 'X' or 'Y'
        fst.add_transition(WeightedTransition::new(
            s0,
            Some('a'),
            Some('X'),
            s1,
            TropicalWeight::new(1.0),
        ));
        fst.add_transition(WeightedTransition::new(
            s0,
            Some('a'),
            Some('Y'),
            s1,
            TropicalWeight::new(2.0),
        ));
        fst.add_transition(WeightedTransition::new(
            s1,
            Some('b'),
            Some('B'),
            s2,
            TropicalWeight::one(),
        ));
        fst.add_transition(WeightedTransition::new(
            s1,
            Some('c'),
            Some('C'),
            s3,
            TropicalWeight::one(),
        ));

        fst
    }

    #[test]
    fn test_subsequential_check() {
        let simple = make_simple_fst();
        assert!(SubsequentialTransducer::<char, TropicalWeight>::is_subsequential(&simple));

        let ambiguous = make_ambiguous_fst();
        assert!(!SubsequentialTransducer::<char, TropicalWeight>::is_subsequential(&ambiguous));
    }

    #[test]
    fn test_subsequential_from_wfst() {
        let simple = make_simple_fst();
        let subseq = SubsequentialTransducer::from_wfst(simple);
        assert!(subseq.is_some());

        let ambiguous = make_ambiguous_fst();
        let subseq = SubsequentialTransducer::from_wfst(ambiguous);
        assert!(subseq.is_none());
    }

    #[test]
    fn test_subsequential_apply() {
        let fst = make_simple_fst();
        let subseq = SubsequentialTransducer::from_wfst(fst).expect("Should be subsequential");

        let result = subseq.apply(&['a', 'b']);
        assert!(result.is_some());

        let (output, _weight) = result.expect("subsequential/mod.rs: required value was None/Err");
        assert_eq!(output, vec!['A', 'B']);
    }

    #[test]
    fn test_subsequential_apply_not_accepted() {
        let fst = make_simple_fst();
        let subseq = SubsequentialTransducer::from_wfst(fst).expect("Should be subsequential");

        // 'a' alone is not accepted (s1 is not final)
        let result = subseq.apply(&['a']);
        assert!(result.is_none());

        // 'x' is not in the alphabet
        let result = subseq.apply(&['x']);
        assert!(result.is_none());
    }

    #[test]
    fn test_decompose_subsequential() {
        let fst = make_simple_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        assert!(piecewise.is_trivial());
        assert_eq!(piecewise.num_pieces(), 1);
    }

    #[test]
    fn test_decompose_ambiguous() {
        let fst = make_ambiguous_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        // Should have multiple pieces for ambiguous FST
        assert!(piecewise.num_pieces() >= 1);
    }

    #[test]
    fn test_piecewise_apply() {
        let fst = make_simple_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        let results = piecewise.apply(&['a', 'b']);
        assert!(!results.is_empty());
        assert_eq!(results[0].0, vec!['A', 'B']);
    }

    #[test]
    fn test_piecewise_stats() {
        let fst = make_simple_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        let stats = piecewise.stats();
        assert_eq!(stats.num_pieces, 1);
        assert!(stats.total_states > 0);
    }

    #[test]
    fn test_builder() {
        let fst = make_simple_fst();
        let piece = SubsequentialTransducer::from_wfst(fst).expect("Should be subsequential");

        let piecewise = PiecewiseBuilder::new().add_piece(piece).build();

        assert_eq!(piecewise.num_pieces(), 1);
    }

    #[test]
    fn test_builder_add_wfst() {
        let fst = make_simple_fst();

        let builder = PiecewiseBuilder::<char, TropicalWeight>::new().add_wfst(fst);

        assert!(builder.is_some());
        let piecewise = builder
            .expect("subsequential/mod.rs: required value was None/Err")
            .build();
        assert_eq!(piecewise.num_pieces(), 1);
    }

    #[test]
    fn test_builder_reject_ambiguous() {
        let fst = make_ambiguous_fst();

        let builder = PiecewiseBuilder::<char, TropicalWeight>::new().add_wfst(fst);

        assert!(builder.is_none()); // Should fail because FST is not subsequential
    }

    #[test]
    fn test_degree() {
        let fst = make_simple_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);
        assert_eq!(piecewise.degree(), 1);
    }

    #[test]
    fn test_piece_id() {
        let fst = make_simple_fst();
        let piece = SubsequentialTransducer::from_wfst(fst).expect("Should be subsequential");
        assert_eq!(piece.piece_id(), 0);
    }

    #[test]
    fn test_empty_fst() {
        let fst: VectorWfst<char, TropicalWeight> = VectorWfst::new();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        let results = piecewise.apply(&['a']);
        assert!(results.is_empty());
    }

    #[test]
    fn test_set_final_output() {
        let fst = make_simple_fst();
        let mut subseq = SubsequentialTransducer::from_wfst(fst).expect("Should be subsequential");

        subseq.set_final_output(2, vec!['!']);

        let result = subseq.apply(&['a', 'b']);
        assert!(result.is_some());

        let (output, _) = result.expect("subsequential/mod.rs: required value was None/Err");
        assert_eq!(output, vec!['A', 'B', '!']);
    }

    #[test]
    fn test_apply_unique() {
        let fst = make_simple_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        let results = piecewise.apply_unique(&['a', 'b']);
        assert!(!results.is_empty());

        // Should be deduplicated
        let mut seen = std::collections::HashSet::new();
        for (output, _) in &results {
            assert!(seen.insert(output.clone()), "Duplicate output found");
        }
    }

    #[test]
    fn test_find_ambiguity_points() {
        let fst = make_ambiguous_fst();
        let ambiguous = PiecewiseSubsequential::<char, TropicalWeight>::find_ambiguity_points(&fst);

        assert!(!ambiguous.is_empty());
        // State 0 has ambiguity on 'a'
        assert!(ambiguous
            .iter()
            .any(|(state, count)| *state == 0 && *count == 2));
    }

    #[test]
    fn test_decomposition_stats() {
        let fst = make_ambiguous_fst();
        let piecewise = PiecewiseSubsequential::decompose(&fst);

        let stats = piecewise.stats();
        assert!(stats.num_pieces >= 1);
        assert!(stats.total_states > 0);
        assert!(stats.total_transitions > 0);
    }
}