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 unique algorithm for removing exact duplicate arcs
//!
//! ## Overview
//!
//! Removes exact duplicate arcs, keeping only the first occurrence. An arc is
//! considered a duplicate if it has identical (ilabel, olabel, weight, nextstate)
//! to another arc from the same source state.
//!
//! This is simpler than arc_sum - it doesn't combine weights, just removes
//! exact duplicates.
//!
//! ## Algorithm
//!
//! For each state:
//! 1. Use HashSet to track seen (ilabel, olabel, weight, nextstate) tuples
//! 2. Keep only first occurrence of each unique arc
//! 3. Discard exact duplicates
//!
//! ## Complexity
//!
//! - **Time:** O(|V| + |E| log |E|)
//!   - Iterate over all states: O(|V|)
//!   - For each state with k arcs: O(k log k) for hashing/deduplication
//!   - Total arcs processed: O(|E| log |E|)
//!
//! - **Space:** O(|E|) - HashSet for duplicate detection
//!
//! ## Use Cases
//!
//! - Removing accidentally duplicated arcs
//! - Cleaning FST representations
//! - Normalizing FST structure before serialization
//!
//! ## 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 exact duplicate arcs
//! let arc = Arc::new(1, 2, TropicalWeight::new(1.0), s1);
//! fst.add_arc(s0, arc.clone());
//! fst.add_arc(s0, arc.clone());
//!
//! arc_unique(&mut fst)?;
//!
//! // Now only one arc remains
//! 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., Schalkwyk, J., Skut, W., and Mohri, M. (2007).
//!   OpenFst: A general and efficient weighted finite-state transducer library.
//!   In *International Conference on Implementation and Application of Automata*
//!   (pp. 11-23). Springer. <https://doi.org/10.1007/978-3-540-76336-9_3>

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

/// Removes exact duplicate arcs, keeping only the first occurrence.
///
/// An arc is considered a duplicate if it has identical
/// (ilabel, olabel, weight, nextstate) to another arc from the same
/// source state. This is simpler than arc_sum - it doesn't combine
/// weights, just removes exact duplicates.
///
/// # Algorithm
///
/// For each state:
/// 1. Use HashSet to track seen (ilabel, olabel, weight, nextstate) tuples
/// 2. Keep only first occurrence of each unique arc
/// 3. Discard exact duplicates
///
/// # Complexity
///
/// - **Time:** O(|V| + |E| log |E|)
///   - Iterate states: O(|V|)
///   - Hash/deduplicate arcs per state: O(k log k) where k = arcs from state
///   - Total: O(|E| log |E|) across all arcs
///
/// - **Space:** O(|E|) - HashSet for duplicate detection
///
/// # Examples
///
/// ## Exact Duplicates
///
/// ```
/// use arcweight::prelude::*;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
///
/// // Add exact duplicate arcs
/// let arc = Arc::new(1, 2, TropicalWeight::new(1.0), s1);
/// fst.add_arc(s0, arc.clone());
/// fst.add_arc(s0, arc.clone());
/// fst.add_arc(s0, arc.clone());
///
/// arc_unique(&mut fst)?;
///
/// // Only one arc remains
/// assert_eq!(fst.num_arcs(s0), 1);
/// # 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);
///
/// 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_unique(&mut fst)?;
///
/// // No change
/// assert_eq!(fst.num_arcs(s0), 2);
/// # 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);
///
/// let arc1 = Arc::new(1, 1, TropicalWeight::new(1.0), s1);
/// fst.add_arc(s0, arc1.clone());
/// fst.add_arc(s0, arc1.clone()); // Duplicate
/// fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(2.0), s2)); // Unique
///
/// arc_unique(&mut fst)?;
///
/// // One duplicate removed
/// assert_eq!(fst.num_arcs(s0), 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Same Labels Different Weights (Not Duplicates)
///
/// ```
/// use arcweight::prelude::*;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
///
/// // Same labels but different weights - 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), s1));
///
/// arc_unique(&mut fst)?;
///
/// // Both kept (different weights)
/// assert_eq!(fst.num_arcs(s0), 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## Empty FST
///
/// ```
/// use arcweight::prelude::*;
///
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// arc_unique(&mut fst)?;
/// assert_eq!(fst.num_states(), 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Performance Notes
///
/// - **No duplicates:** O(|E|) with minimal overhead for unique arcs
/// - **Many duplicates:** Reduces arc count and improves FST performance
/// - **Hash operations:** O(1) average case for duplicate detection
/// - **Memory:** HashSet storage proportional to unique arcs per state
/// - **Best practice:** Apply after operations that may accidentally duplicate arcs
///
/// # See Also
///
/// - [`arc_sum`] - Combines duplicate arcs by summing weights
/// - [`arc_sort`] - Sorts arcs for easier duplicate detection
///
/// [`arc_sum`]: crate::algorithms::arc_sum::arc_sum
/// [`arc_sort`]: crate::algorithms::arc_sort::arc_sort
pub fn arc_unique<W, F>(fst: &mut F) -> Result<()>
where
    W: Semiring + Clone + Eq + std::hash::Hash,
    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;
        }

        // Use HashSet to track unique arcs
        let mut seen = HashSet::new();
        let mut unique_arcs = Vec::new();

        for arc in arcs {
            // Create a hashable key from the arc
            let key = (arc.ilabel, arc.olabel, arc.weight.clone(), arc.nextstate);

            if seen.insert(key) {
                // First occurrence - keep it
                unique_arcs.push(arc);
            }
            // Otherwise it's a duplicate - skip it
        }

        // Clear existing arcs and add back only unique ones
        fst.delete_arcs(state);
        for arc in unique_arcs {
            fst.add_arc(state, arc);
        }
    }

    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);

        let arc = Arc::new(1, 2, TropicalWeight::new(1.0), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());

        arc_unique(&mut fst).unwrap();

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

    #[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_unique(&mut fst).unwrap();

        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);

        let arc = Arc::new(1, 1, TropicalWeight::new(1.0), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone()); // Dup
        fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(2.0), s2)); // Unique

        arc_unique(&mut fst).unwrap();

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

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

        // Same labels but different weights - 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), s1));

        arc_unique(&mut fst).unwrap();

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

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

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

        let arc = Arc::new(1, 1, TropicalWeight::new(1.0), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());

        arc_unique(&mut fst).unwrap();

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

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

        let arc = Arc::new(1, 1, TropicalWeight::new(3.0), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());

        arc_unique(&mut fst).unwrap();

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

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

        let arc = Arc::new(1, 1, LogWeight::new(2.0), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());

        arc_unique(&mut fst).unwrap();

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

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

        let arc = Arc::new(1, 1, BooleanWeight::one(), s1);
        fst.add_arc(s0, arc.clone());
        fst.add_arc(s0, arc.clone());

        arc_unique(&mut fst).unwrap();

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

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

        let arc1 = Arc::new(1, 1, TropicalWeight::new(1.0), s1);
        fst.add_arc(s0, arc1.clone());
        fst.add_arc(s0, arc1.clone());

        let arc2 = Arc::new(2, 2, TropicalWeight::new(2.0), s2);
        fst.add_arc(s1, arc2.clone());
        fst.add_arc(s1, arc2.clone());

        arc_unique(&mut fst).unwrap();

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