arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
Documentation
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
//! Path iteration utilities for FSTs.
//!
//! This module provides iterators for enumerating all accepting paths through
//! a weighted finite-state transducer, with support for filtering, cycle
//! detection, and symbol table integration.
//!
//! # Overview
//!
//! Path iteration is useful for:
//! - Extracting all input/output string pairs from an FST
//! - Finding the n-best paths by weight
//! - Debugging FST structure by examining accepted sequences
//! - Converting FSTs to explicit path representations
//!
//! # Cycle Handling
//!
//! The iterators automatically detect and skip cycles to ensure termination.
//! For cyclic FSTs, only acyclic paths are enumerated.
//!
//! # Examples
//!
//! ## Enumerate All Paths
//!
//! ```
//! use arcweight::prelude::*;
//! use arcweight::utils::PathIterExt;
//!
//! let mut fst = VectorFst::<TropicalWeight>::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());
//! fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(0.5), s1));
//! fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(1.0), s1));
//! fst.add_arc(s1, Arc::new(3, 3, TropicalWeight::new(0.5), s2));
//!
//! for path in fst.paths_iter() {
//!     println!("Input: {:?}, Weight: {:?}", path.input_labels(), path.weight);
//! }
//! ```
//!
//! ## Limit Number of Paths
//!
//! ```
//! use arcweight::prelude::*;
//! use arcweight::utils::PathIterExt;
//!
//! let mut fst = VectorFst::<TropicalWeight>::new();
//! let s0 = fst.add_state();
//! fst.set_start(s0);
//! fst.set_final(s0, TropicalWeight::one());
//!
//! // Get at most 10 paths
//! let paths: Vec<_> = fst.paths_iter().with_max_paths(10).collect();
//! ```
//!
//! # Complexity
//!
//! - **Time**: O(number of paths) in the acyclic case
//! - **Space**: O(path length) for the current path being explored
//!
//! # References
//!
//! - Mehryar Mohri and Michael Riley. 2002. An efficient algorithm for the
//!   n-best-strings problem. In *Proc. ICSLP 2002*, 1313-1316.

use crate::arc::Arc;
use crate::fst::{Fst, StateId};
use crate::semiring::Semiring;
use std::collections::VecDeque;

/// Represents a complete accepting path through an FST.
///
/// An `FstPath` contains all information about a path from the start state
/// to a final state, including the sequence of states, arcs, and the total
/// path weight (product of arc weights times final weight).
///
/// # Examples
///
/// ```
/// use arcweight::prelude::*;
/// use arcweight::utils::PathIterExt;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, TropicalWeight::one());
/// fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.5), s1));
///
/// let path = fst.paths_iter().next().unwrap();
/// assert_eq!(path.input_labels(), vec![1]);
/// assert_eq!(path.output_labels(), vec![2]);
/// assert_eq!(path.states, vec![s0, s1]);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct FstPath<W: Semiring> {
    /// Sequence of states visited, starting from start state.
    pub states: Vec<StateId>,
    /// Sequence of arcs traversed along the path.
    pub arcs: Vec<Arc<W>>,
    /// Total weight of the path (product of arc weights and final weight).
    pub weight: W,
    /// The final state where the path terminates.
    pub final_state: StateId,
}

impl<W: Semiring> FstPath<W> {
    /// Extracts the sequence of input labels from the path.
    ///
    /// # Returns
    ///
    /// A vector of input labels in traversal order.
    pub fn input_labels(&self) -> Vec<u32> {
        self.arcs.iter().map(|arc| arc.ilabel).collect()
    }

    /// Extracts the sequence of output labels from the path.
    ///
    /// # Returns
    ///
    /// A vector of output labels in traversal order.
    pub fn output_labels(&self) -> Vec<u32> {
        self.arcs.iter().map(|arc| arc.olabel).collect()
    }

    /// Extracts input/output label pairs from the path.
    ///
    /// # Returns
    ///
    /// A vector of (input_label, output_label) tuples in traversal order.
    pub fn io_pairs(&self) -> Vec<(u32, u32)> {
        self.arcs
            .iter()
            .map(|arc| (arc.ilabel, arc.olabel))
            .collect()
    }
}

/// Iterator over all accepting paths in an FST.
///
/// This iterator performs a breadth-first search from the start state,
/// yielding each complete path to a final state. Cycles are automatically
/// detected and skipped to ensure termination.
///
/// # Complexity
///
/// - **Time**: O(number of acyclic paths)
/// - **Space**: O(maximum path length) for the exploration queue
///
/// # Examples
///
/// ```
/// use arcweight::prelude::*;
/// use arcweight::utils::{PathsIterator, PathIterExt};
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, TropicalWeight::one());
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(0.5), s1));
///
/// let paths: Vec<_> = fst.paths_iter().collect();
/// assert_eq!(paths.len(), 1);
/// ```
#[derive(Debug)]
pub struct PathsIterator<'a, W: Semiring, F: Fst<W>> {
    fst: &'a F,
    queue: VecDeque<(Vec<StateId>, Vec<Arc<W>>, W, StateId)>,
    max_paths: Option<usize>,
    paths_found: usize,
    weight_threshold: Option<W>,
}

impl<'a, W: Semiring, F: Fst<W>> PathsIterator<'a, W, F> {
    /// Creates a new path iterator for the given FST.
    ///
    /// # Arguments
    ///
    /// * `fst` - The FST to iterate over
    pub fn new(fst: &'a F) -> Self {
        let mut queue = VecDeque::new();
        if let Some(start) = fst.start() {
            queue.push_back((vec![start], Vec::new(), W::one(), start));
        }
        Self {
            fst,
            queue,
            max_paths: None,
            paths_found: 0,
            weight_threshold: None,
        }
    }

    /// Sets the maximum number of paths to return.
    ///
    /// After returning this many paths, the iterator will stop even if more
    /// paths exist. Useful for n-best path extraction.
    ///
    /// # Arguments
    ///
    /// * `max` - Maximum number of paths to yield
    ///
    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    /// use arcweight::utils::PathIterExt;
    ///
    /// let mut fst = VectorFst::<TropicalWeight>::new();
    /// let s0 = fst.add_state();
    /// fst.set_start(s0);
    /// fst.set_final(s0, TropicalWeight::one());
    ///
    /// // Get at most 5 paths
    /// let paths: Vec<_> = fst.paths_iter().with_max_paths(5).collect();
    /// assert!(paths.len() <= 5);
    /// ```
    pub fn with_max_paths(mut self, max: usize) -> Self {
        self.max_paths = Some(max);
        self
    }

    /// Sets a weight threshold for path filtering.
    ///
    /// Only paths with weight less than or equal to the threshold will be
    /// returned. For tropical semiring, this filters by path cost.
    ///
    /// # Type Requirements
    ///
    /// Requires a [`NaturallyOrderedSemiring`](crate::semiring::NaturallyOrderedSemiring)
    /// for weight comparison.
    ///
    /// # Arguments
    ///
    /// * `threshold` - Maximum weight for returned paths
    pub fn with_weight_threshold(mut self, threshold: W) -> Self
    where
        W: crate::semiring::NaturallyOrderedSemiring,
    {
        self.weight_threshold = Some(threshold);
        self
    }
}

impl<'a, W: Semiring, F: Fst<W>> Iterator for PathsIterator<'a, W, F>
where
    W: Clone,
{
    type Item = FstPath<W>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some((states, arcs, weight, current_state)) = self.queue.pop_front() {
            // Check if we've exceeded max paths
            if let Some(max) = self.max_paths {
                if self.paths_found >= max {
                    return None;
                }
            }

            // Check weight threshold (only if set and semiring is naturally ordered)
            if let Some(ref threshold) = self.weight_threshold {
                // This comparison is safe because with_weight_threshold requires NaturallyOrderedSemiring
                if weight > *threshold {
                    continue;
                }
            }

            // Check if current state is final
            if let Some(final_weight) = self.fst.final_weight(current_state) {
                let total_weight = weight.clone() * final_weight.clone();

                // Check final weight threshold
                if let Some(ref threshold) = self.weight_threshold {
                    if total_weight > *threshold {
                        // Continue exploring, but don't return this path
                    } else {
                        self.paths_found += 1;
                        return Some(FstPath {
                            states: states.clone(),
                            arcs: arcs.clone(),
                            weight: total_weight,
                            final_state: current_state,
                        });
                    }
                } else {
                    self.paths_found += 1;
                    return Some(FstPath {
                        states: states.clone(),
                        arcs: arcs.clone(),
                        weight: total_weight,
                        final_state: current_state,
                    });
                }
            }

            // Explore outgoing arcs
            for arc in self.fst.arcs(current_state) {
                // Check for cycles - if the next state is already in the current path, skip it
                // This ensures we detect cycles regardless of path length
                if states.contains(&arc.nextstate) {
                    continue;
                }

                let next_weight = weight.clone() * arc.weight.clone();
                let mut next_states = states.clone();
                next_states.push(arc.nextstate);
                let mut next_arcs = arcs.clone();
                next_arcs.push(arc.clone());

                self.queue
                    .push_back((next_states, next_arcs, next_weight, arc.nextstate));
            }
        }

        None
    }
}

/// Iterator over paths with human-readable string representations.
///
/// This iterator wraps [`PathsIterator`] and uses symbol tables to convert
/// numeric labels to human-readable strings.
///
/// # Examples
///
/// ```
/// use arcweight::prelude::*;
/// use arcweight::utils::{PathIterExt, SymbolTable};
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let mut syms = SymbolTable::new();
/// let hello = syms.add_symbol("hello");
///
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, TropicalWeight::one());
/// fst.add_arc(s0, Arc::new(hello, hello, TropicalWeight::one(), s1));
///
/// for path in fst.string_paths_iter(Some(&syms), Some(&syms)) {
///     println!("Input: {}, Output: {}", path.input, path.output);
/// }
/// ```
#[derive(Debug)]
pub struct StringPathsIterator<'a, W: Semiring, F: Fst<W>> {
    path_iter: PathsIterator<'a, W, F>,
    input_symbols: Option<&'a crate::utils::SymbolTable>,
    output_symbols: Option<&'a crate::utils::SymbolTable>,
}

impl<'a, W: Semiring, F: Fst<W>> StringPathsIterator<'a, W, F> {
    /// Create a new string paths iterator
    pub fn new(
        fst: &'a F,
        input_symbols: Option<&'a crate::utils::SymbolTable>,
        output_symbols: Option<&'a crate::utils::SymbolTable>,
    ) -> Self {
        Self {
            path_iter: PathsIterator::new(fst),
            input_symbols,
            output_symbols,
        }
    }

    /// Set maximum number of paths
    pub fn with_max_paths(mut self, max: usize) -> Self {
        self.path_iter = self.path_iter.with_max_paths(max);
        self
    }
}

/// Human-readable string representation of a path.
///
/// Contains the input and output sequences as space-separated strings,
/// and the path weight as a string.
#[derive(Debug, Clone)]
pub struct StringPath {
    /// Input string (space-separated symbols from symbol table).
    pub input: String,
    /// Output string (space-separated symbols from symbol table).
    pub output: String,
    /// Path weight formatted as a string.
    pub weight: String,
}

impl<'a, W: Semiring, F: Fst<W>> Iterator for StringPathsIterator<'a, W, F>
where
    W: Clone + std::fmt::Display,
{
    type Item = StringPath;

    fn next(&mut self) -> Option<Self::Item> {
        let path = self.path_iter.next()?;

        let input = if let Some(symbols) = self.input_symbols {
            let labels: Vec<&str> = path
                .input_labels()
                .iter()
                .filter_map(|&label| symbols.find(label))
                .collect();
            labels.join(" ")
        } else {
            let labels: Vec<String> = path.input_labels().iter().map(|&l| l.to_string()).collect();
            labels.join(" ")
        };

        let output = if let Some(symbols) = self.output_symbols {
            let labels: Vec<&str> = path
                .output_labels()
                .iter()
                .filter_map(|&label| symbols.find(label))
                .collect();
            labels.join(" ")
        } else {
            let labels: Vec<String> = path
                .output_labels()
                .iter()
                .map(|&l| l.to_string())
                .collect();
            labels.join(" ")
        };

        Some(StringPath {
            input,
            output,
            weight: path.weight.to_string(),
        })
    }
}

/// Extension trait for FSTs to provide path iteration.
///
/// This trait is automatically implemented for all types that implement
/// [`Fst`], providing convenient path iteration methods.
///
/// # Examples
///
/// ```
/// use arcweight::prelude::*;
/// use arcweight::utils::PathIterExt;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s0, TropicalWeight::one());
///
/// // Numeric labels
/// for path in fst.paths_iter() {
///     println!("Weight: {:?}", path.weight);
/// }
///
/// // String labels (requires symbol tables)
/// for path in fst.string_paths_iter(None, None) {
///     println!("{} -> {} ({})", path.input, path.output, path.weight);
/// }
/// ```
pub trait PathIterExt<W: Semiring>: crate::fst::Fst<W> {
    /// Returns an iterator over all accepting paths in the FST.
    ///
    /// Paths are yielded in breadth-first order. Cycles are automatically
    /// detected and skipped.
    fn paths_iter(&self) -> PathsIterator<'_, W, Self>
    where
        Self: Sized;

    /// Returns an iterator over paths with human-readable string representations.
    ///
    /// # Arguments
    ///
    /// * `input_symbols` - Optional symbol table for input labels
    /// * `output_symbols` - Optional symbol table for output labels
    fn string_paths_iter<'a>(
        &'a self,
        input_symbols: Option<&'a crate::utils::SymbolTable>,
        output_symbols: Option<&'a crate::utils::SymbolTable>,
    ) -> StringPathsIterator<'a, W, Self>
    where
        Self: Sized;
}

impl<W: Semiring, F: Fst<W>> PathIterExt<W> for F {
    fn paths_iter(&self) -> PathsIterator<'_, W, Self> {
        PathsIterator::new(self)
    }

    fn string_paths_iter<'a>(
        &'a self,
        input_symbols: Option<&'a crate::utils::SymbolTable>,
        output_symbols: Option<&'a crate::utils::SymbolTable>,
    ) -> StringPathsIterator<'a, W, Self> {
        StringPathsIterator::new(self, input_symbols, output_symbols)
    }
}

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

    #[test]
    fn test_fst_path_input_labels() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 10, TropicalWeight::one(), s1));

        let path = fst.paths_iter().next().unwrap();
        assert_eq!(path.input_labels(), vec![1]);
    }

    #[test]
    fn test_fst_path_output_labels() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 10, TropicalWeight::one(), s1));

        let path = fst.paths_iter().next().unwrap();
        assert_eq!(path.output_labels(), vec![10]);
    }

    #[test]
    fn test_fst_path_io_pairs() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 10, TropicalWeight::one(), s1));

        let path = fst.paths_iter().next().unwrap();
        assert_eq!(path.io_pairs(), vec![(1, 10)]);
    }

    #[test]
    fn test_paths_iterator_empty() {
        let fst = VectorFst::<TropicalWeight>::new();
        let mut iter = fst.paths_iter();
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_paths_iterator_single_path() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::one(), s1));

        let paths: Vec<_> = fst.paths_iter().collect();
        assert_eq!(paths.len(), 1);
    }

    #[test]
    fn test_string_paths_iterator() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let mut symbols = SymbolTable::new();
        let hello_id = symbols.add_symbol("hello");

        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(hello_id, hello_id, TropicalWeight::one(), s1));

        let paths: Vec<_> = fst
            .string_paths_iter(Some(&symbols), Some(&symbols))
            .collect();
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].input, "hello");
    }
}