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
//! Arc sum algorithm for combining duplicate arcs
//!
//! ## Overview
//!
//! Combines duplicate arcs by summing their weights. For arcs with identical
//! (ilabel, olabel, nextstate) tuples from the same source state, replaces them
//! with a single arc whose weight is the sum (⊕) of the original weights.
//!
//! ## Algorithm
//!
//! For each state:
//! 1. Collect all arcs from the state
//! 2. Group arcs by (ilabel, olabel, nextstate) using HashMap
//! 3. For each group: sum weights using semiring addition (⊕)
//! 4. Replace all arcs with one arc per group with summed weight
//!
//! ## Complexity
//!
//! - **Time:** O(|V| + |E| log |E|)
//!   - Iterate over all states: O(|V|)
//!   - For each state with k arcs: O(k log k) for sorting/grouping
//!   - Total arcs processed: O(|E| log |E|)
//!
//! - **Space:** O(|E|) - temporary arc storage
//!
//! ## Use Cases
//!
//! - Simplifying FSTs with redundant transitions
//! - Normalizing FST representation
//! - Preprocessing for determinization
//! - Combining multiple weighted paths
//!
//! ## Examples
//!
//! ```
//! use arcweight::prelude::*;
//!
//! let mut fst = VectorFst::<TropicalWeight>::new();
//! let s0 = fst.add_state();
//! let s1 = fst.add_state();
//! fst.set_start(s0);
//!
//! // Add duplicate arcs with different weights
//! fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.0), s1));
//! fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(2.0), s1));
//!
//! arc_sum(&mut fst)?;
//!
//! // Now only one arc with summed weight (min for tropical)
//! assert_eq!(fst.num_arcs(s0), 1);
//! # Ok::<(), arcweight::Error>(())
//! ```
//!
//! # References
//!
//! - Mohri, M. (2009). Weighted automata algorithms. In *Handbook of Weighted
//!   Automata* (pp. 213-254). Springer. <https://doi.org/10.1007/978-3-642-01492-5_6>
//! - Allauzen, C., Riley, M., and Mohri, M. (2004). A filter-based algorithm
//!   for efficient weighted epsilon-removal. In *International Conference on
//!   Implementation and Application of Automata* (pp. 65-76). Springer.
//!   <https://doi.org/10.1007/978-3-540-30500-2_7>

use crate::arc::Arc;
use crate::fst::{Label, MutableFst, StateId};
use crate::semiring::Semiring;
use crate::Result;
use std::collections::HashMap;

/// Arc key for grouping duplicate arcs
type ArcKey = (Label, Label, StateId);

/// Combines duplicate arcs by summing their weights.
///
/// For arcs with identical (ilabel, olabel, nextstate) tuples from the
/// same source state, replaces them with a single arc whose weight is
/// the sum (⊕) of the original weights in the semiring.
///
/// This operation is useful for:
/// - Simplifying FSTs with redundant transitions
/// - Normalizing FST representation
/// - Preprocessing for determinization
///
/// # Algorithm
///
/// For each state:
/// 1. Group arcs by (ilabel, olabel, nextstate)
/// 2. For each group: sum weights using semiring addition (⊕)
/// 3. Replace all arcs in group with single arc with summed weight
///
/// # Complexity
///
/// - **Time:** O(|V| + |E| log |E|)
///   - Iterate states: O(|V|)
///   - Group arcs per state: O(k log k) where k = arcs from state
///   - Total: O(|E| log |E|) across all arcs
///
/// - **Space:** O(|E|) - temporary arc storage
///
/// # Examples
///
/// ## Tropical Semiring (Min Combination)
///
/// ```
/// use arcweight::prelude::*;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
///
/// // Add duplicate arcs - tropical takes minimum
/// fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.0), s1));
/// fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(2.0), s1));
///
/// arc_sum(&mut fst)?;
///
/// // Result: one arc with weight 1.0 (min)
/// assert_eq!(fst.num_arcs(s0), 1);
/// let arc = fst.arcs(s0).next().unwrap();
/// assert_eq!(arc.weight, TropicalWeight::new(1.0));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Probability Semiring (Sum Combination)
///
/// ```
/// use arcweight::prelude::*;
///
/// let mut fst = VectorFst::<ProbabilityWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
///
/// // Add duplicate arcs - probability sums them
/// fst.add_arc(s0, Arc::new(1, 2, ProbabilityWeight::new(0.3), s1));
/// fst.add_arc(s0, Arc::new(1, 2, ProbabilityWeight::new(0.4), s1));
///
/// arc_sum(&mut fst)?;
///
/// // Result: one arc with weight 0.7 (sum)
/// assert_eq!(fst.num_arcs(s0), 1);
/// let arc = fst.arcs(s0).next().unwrap();
/// assert_eq!(arc.weight, ProbabilityWeight::new(0.7));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Partial Duplicates
///
/// ```
/// use arcweight::prelude::*;
///
/// 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);
///
/// // Some duplicate, some unique
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.0), s1)); // Duplicate
/// fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(3.0), s2)); // Unique
///
/// arc_sum(&mut fst)?;
///
/// // Result: 2 arcs (one combined, one unique)
/// assert_eq!(fst.num_arcs(s0), 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## No Duplicates (No Change)
///
/// ```
/// use arcweight::prelude::*;
///
/// 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);
///
/// // All unique arcs
/// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
/// fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(2.0), s2));
///
/// arc_sum(&mut fst)?;
///
/// // No change
/// assert_eq!(fst.num_arcs(s0), 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Performance Notes
///
/// - **No duplicates:** O(|E|) with no redundant work for unique arcs
/// - **Many duplicates:** Significantly reduces arc count and memory usage
/// - **Hash lookups:** O(1) average case for grouping by (ilabel, olabel, nextstate)
/// - **Memory:** Temporary storage proportional to max arcs per state
/// - **Best practice:** Apply after FST construction or operations that create duplicates
///
/// # See Also
///
/// - [`arc_unique`] - Removes duplicate arcs instead of summing weights
/// - [`determinize`] - May benefit from arc_sum preprocessing
///
/// [`arc_unique`]: crate::algorithms::arc_unique::arc_unique
/// [`determinize`]: crate::algorithms::determinize::determinize
pub fn arc_sum<W, F>(fst: &mut F) -> Result<()>
where
    W: Semiring + Clone,
    F: MutableFst<W>,
{
    let num_states = fst.num_states();

    for state in 0..num_states as StateId {
        // Collect all arcs from this state
        let arcs: Vec<Arc<W>> = fst.arcs(state).collect();

        if arcs.is_empty() {
            continue;
        }

        // Group arcs by (ilabel, olabel, nextstate)
        let mut arc_groups: HashMap<ArcKey, W> = HashMap::new();

        for arc in arcs {
            let key = (arc.ilabel, arc.olabel, arc.nextstate);
            arc_groups
                .entry(key)
                .and_modify(|w| *w = w.plus(&arc.weight))
                .or_insert(arc.weight);
        }

        // Clear existing arcs
        fst.delete_arcs(state);

        // Add back one arc per group with summed weight
        for ((ilabel, olabel, nextstate), weight) in arc_groups {
            fst.add_arc(state, Arc::new(ilabel, olabel, weight, nextstate));
        }
    }

    Ok(())
}

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

    #[test]
    fn test_exact_duplicates() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);

        // Two exact duplicates (same labels, different weights)
        fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(2.0), s1));

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 1);
        let arc = fst.arcs(s0).next().unwrap();
        assert_eq!(arc.ilabel, 1);
        assert_eq!(arc.olabel, 2);
        assert_eq!(arc.weight, TropicalWeight::new(1.0)); // min(1.0, 2.0)
    }

    #[test]
    fn test_multiple_duplicates() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);

        // Three duplicates
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(3.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.0), s1));

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 1);
        let arc = fst.arcs(s0).next().unwrap();
        assert_eq!(arc.weight, TropicalWeight::new(1.0)); // min(3, 1, 2)
    }

    #[test]
    fn test_no_duplicates() {
        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.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(2.0), s2));

        arc_sum(&mut fst).unwrap();

        // Should be unchanged
        assert_eq!(fst.num_arcs(s0), 2);
    }

    #[test]
    fn test_partial_duplicates() {
        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.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.0), s1)); // Dup
        fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(3.0), s2)); // Unique

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 2);
    }

    #[test]
    fn test_different_weights() {
        let mut fst = VectorFst::<ProbabilityWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);

        // Probability semiring sums
        fst.add_arc(s0, Arc::new(1, 1, ProbabilityWeight::new(0.3), s1));
        fst.add_arc(s0, Arc::new(1, 1, ProbabilityWeight::new(0.4), s1));

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 1);
        let arc = fst.arcs(s0).next().unwrap();
        assert_eq!(arc.weight, ProbabilityWeight::new(0.7));
    }

    #[test]
    fn test_empty_fst() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        arc_sum(&mut fst).unwrap();
        assert_eq!(fst.num_states(), 0);
    }

    #[test]
    fn test_tropical_semiring() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);

        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(5.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(3.0), s1));

        arc_sum(&mut fst).unwrap();

        let arc = fst.arcs(s0).next().unwrap();
        assert_eq!(arc.weight, TropicalWeight::new(3.0)); // min
    }

    #[test]
    fn test_log_semiring() {
        let mut fst = VectorFst::<LogWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);

        fst.add_arc(s0, Arc::new(1, 1, LogWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, LogWeight::new(2.0), s1));

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 1);
        // Log semiring does log-add-exp
    }

    #[test]
    fn test_multiple_states_with_duplicates() {
        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);

        // Duplicates from s0
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.0), s1));

        // Duplicates from s1
        fst.add_arc(s1, Arc::new(2, 2, TropicalWeight::new(3.0), s2));
        fst.add_arc(s1, Arc::new(2, 2, TropicalWeight::new(4.0), s2));

        arc_sum(&mut fst).unwrap();

        assert_eq!(fst.num_arcs(s0), 1);
        assert_eq!(fst.num_arcs(s1), 1);
    }

    #[test]
    fn test_same_labels_different_nextstates() {
        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);

        // Same labels but different next states - NOT duplicates
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(2.0), s2));

        arc_sum(&mut fst).unwrap();

        // Should remain separate
        assert_eq!(fst.num_arcs(s0), 2);
    }
}