duallity 0.1.0

Levenshtein automata as lling-llang WFSTs: composition adapters bridging liblevenshtein fuzzy matching and the lling-llang weighted-transducer framework.
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
//! Generalized Automata WFST wrapper.
//!
//! This module provides [`GeneralizedWfst`], a WFST wrapper for the generalized
//! Levenshtein automaton that supports runtime-configurable operations.
//!
//! # Overview
//!
//! The [`GeneralizedAutomaton`] supports:
//! - Standard operations (match, substitute, insert, delete)
//! - Transposition operations
//! - Merge and split operations
//! - Phonetic operations (digraphs like ph↔f, ch↔k)
//!
//! This WFST wrapper exposes these capabilities in a form compatible with
//! lling-llang WFST composition pipelines.
//!
//! # Example
//!
//! ```rust,ignore
//! use liblevenshtein::wfst::generalized_wfst::{GeneralizedWfst, GeneralizedWfstBuilder};
//! use liblevenshtein::transducer::OperationSet;
//! use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
//!
//! let dict = DynamicDawgChar::<()>::from_terms(vec!["phone", "graph", "church"]);
//!
//! // Create with phonetic operations
//! let wfst = GeneralizedWfstBuilder::new(&dict)
//!     .query("fone")
//!     .max_distance(2)
//!     .with_phonetic_digraphs()
//!     .build();
//! ```

use lling_llang::prelude::{
    LazyState, LazyWfst, StateId, StateSource, TropicalWeight, WeightedTransition, Wfst,
};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;

use liblevenshtein::transducer::generalized::GeneralizedAutomaton;
use liblevenshtein::transducer::{OperationSet, OperationSetBuilder};
use libdictenstein::{Dictionary, DictionaryNode};

/// Cached state information for the Generalized WFST.
#[derive(Clone)]
struct CachedGeneralizedState {
    /// Whether this is a final state.
    is_final: bool,
    /// Final weight if final.
    final_weight: TropicalWeight,
    /// Outgoing transitions.
    transitions: SmallVec<[WeightedTransition<char, TropicalWeight>; 4]>,
}

/// Generalized Automaton WFST wrapper.
///
/// Exposes the generalized Levenshtein automaton with runtime-configurable
/// operations as a WFST compatible with lling-llang composition.
#[derive(Clone)]
pub struct GeneralizedWfst<D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    /// Owned copy of the dictionary.
    dictionary: D,

    /// The query string.
    query: String,

    /// The generalized automaton.
    automaton: GeneralizedAutomaton,

    /// Cached state computations.
    cache: FxHashMap<StateId, CachedGeneralizedState>,

    /// Next available state ID.
    next_state_id: StateId,

    /// Cache policy.
    cache_policy: lling_llang::wfst::CachePolicy,
}

impl<D> GeneralizedWfst<D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    /// Create a new generalized WFST.
    pub fn new(dictionary: &D, query: &str, max_distance: u8, operations: OperationSet) -> Self {
        let automaton = GeneralizedAutomaton::with_operations(max_distance, operations);

        Self {
            dictionary: dictionary.clone(),
            query: query.to_string(),
            automaton,
            cache: FxHashMap::default(),
            next_state_id: 1, // Reserve 0 for start
            cache_policy: lling_llang::wfst::CachePolicy::CacheAll,
        }
    }

    /// Get the query string.
    #[inline]
    pub fn query(&self) -> &str {
        &self.query
    }

    /// Get the maximum distance.
    #[inline]
    pub fn max_distance(&self) -> u8 {
        self.automaton.max_distance()
    }

    /// Ensure a state is computed and cached.
    fn ensure_state(&mut self, state_id: StateId) {
        if self.cache.contains_key(&state_id) {
            return;
        }

        // For now, just cache the start state with basic info
        // Full implementation would track dictionary position + automaton state
        if state_id == 0 {
            let query_chars: Vec<char> = self.query.chars().collect();
            let (is_final, final_weight, transitions) = self.compute_start_state(&query_chars);

            self.cache.insert(
                state_id,
                CachedGeneralizedState {
                    is_final,
                    final_weight,
                    transitions,
                },
            );
        }
    }

    /// Compute transitions from the start state.
    fn compute_start_state(
        &mut self,
        query_chars: &[char],
    ) -> (
        bool,
        TropicalWeight,
        SmallVec<[WeightedTransition<char, TropicalWeight>; 4]>,
    ) {
        let mut transitions = SmallVec::new();
        let root = self.dictionary.root();
        let max_dist = self.automaton.max_distance();

        // Check if start is final (empty query and empty term accepted)
        let is_final = query_chars.is_empty() && root.is_final();
        let final_weight = if is_final {
            TropicalWeight::new(0.0)
        } else {
            TropicalWeight::infinity()
        };

        // Get first query character if available
        let query_char = query_chars.first().copied();

        // Iterate over dictionary edges from root
        for (dict_char, _child) in root.edges() {
            // Match transition
            if let Some(qc) = query_char {
                if qc == dict_char {
                    // Exact match - no error
                    let new_id = self.next_state_id;
                    self.next_state_id += 1;
                    transitions.push(WeightedTransition::new(
                        0,
                        Some(qc),
                        Some(dict_char),
                        new_id,
                        TropicalWeight::new(0.0),
                    ));
                } else {
                    // Substitution - 1 error
                    let new_id = self.next_state_id;
                    self.next_state_id += 1;
                    transitions.push(WeightedTransition::new(
                        0,
                        Some(qc),
                        Some(dict_char),
                        new_id,
                        TropicalWeight::new(1.0),
                    ));
                }
            }

            // Insertion transition (consume dictionary char, no query char)
            if max_dist > 0 {
                let new_id = self.next_state_id;
                self.next_state_id += 1;
                transitions.push(WeightedTransition::new(
                    0,
                    None,
                    Some(dict_char),
                    new_id,
                    TropicalWeight::new(1.0),
                ));
            }
        }

        // Deletion transition (consume query char, no dictionary char)
        if query_char.is_some() && max_dist > 0 {
            let new_id = self.next_state_id;
            self.next_state_id += 1;
            transitions.push(WeightedTransition::new(
                0,
                query_char,
                None,
                new_id,
                TropicalWeight::new(1.0),
            ));
        }

        (is_final, final_weight, transitions)
    }
}

impl<D> Wfst<char, TropicalWeight> for GeneralizedWfst<D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    fn start(&self) -> StateId {
        0
    }

    fn is_final(&self, state: StateId) -> bool {
        self.cache.get(&state).map(|s| s.is_final).unwrap_or(false)
    }

    fn final_weight(&self, state: StateId) -> TropicalWeight {
        self.cache
            .get(&state)
            .map(|s| s.final_weight)
            .unwrap_or_else(TropicalWeight::infinity)
    }

    fn transitions(&self, state: StateId) -> &[WeightedTransition<char, TropicalWeight>] {
        static EMPTY: &[WeightedTransition<char, TropicalWeight>] = &[];
        self.cache
            .get(&state)
            .map(|s| s.transitions.as_slice())
            .unwrap_or(EMPTY)
    }

    fn num_states(&self) -> usize {
        self.next_state_id as usize
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.query.is_empty()
    }

    #[inline]
    fn is_valid_state(&self, state: StateId) -> bool {
        state < self.next_state_id
    }
}

impl<D> LazyWfst<char, TropicalWeight> for GeneralizedWfst<D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    fn is_expanded(&self, state: StateId) -> bool {
        self.cache.contains_key(&state)
    }

    fn expand(&mut self, state: StateId) {
        self.ensure_state(state);
    }

    fn transitions_lazy(&mut self, state: StateId) -> &[WeightedTransition<char, TropicalWeight>] {
        self.ensure_state(state);
        self.transitions(state)
    }

    fn cache_policy(&self) -> lling_llang::wfst::CachePolicy {
        self.cache_policy
    }

    fn set_cache_policy(&mut self, policy: lling_llang::wfst::CachePolicy) {
        self.cache_policy = policy;
    }

    fn computed_states(&self) -> usize {
        self.cache.len()
    }

    fn clear_cache(&mut self) {
        self.cache.clear();
    }
}

impl<D> StateSource<char, TropicalWeight> for GeneralizedWfst<D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    fn compute_state(&self, _state: StateId) -> LazyState<char, TropicalWeight> {
        // Return Pending since we need mutable access for state registration
        LazyState::Pending
    }

    fn start(&self) -> StateId {
        0
    }

    fn num_states_hint(&self) -> Option<usize> {
        // Rough estimate based on query length and distance
        let query_len = self.query.len();
        let max_dist = self.automaton.max_distance() as usize;
        Some((query_len + 1) * (max_dist + 1) * 10)
    }
}

/// Builder for Generalized WFST.
pub struct GeneralizedWfstBuilder<'a, D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    dictionary: &'a D,
    query: Option<String>,
    max_distance: u8,
    operations: OperationSet,
}

impl<'a, D> GeneralizedWfstBuilder<'a, D>
where
    D: Dictionary + Clone + Send + Sync,
    D::Node: DictionaryNode<Unit = char>,
{
    /// Create a new builder.
    pub fn new(dictionary: &'a D) -> Self {
        Self {
            dictionary,
            query: None,
            max_distance: 2,
            operations: OperationSet::standard(),
        }
    }

    /// Set the query string.
    pub fn query(mut self, query: &str) -> Self {
        self.query = Some(query.to_string());
        self
    }

    /// Set the maximum distance.
    pub fn max_distance(mut self, distance: u8) -> Self {
        self.max_distance = distance;
        self
    }

    /// Use standard operations.
    pub fn with_standard_ops(mut self) -> Self {
        self.operations = OperationSet::standard();
        self
    }

    /// Add transposition support.
    pub fn with_transposition(mut self) -> Self {
        self.operations = OperationSet::with_transposition();
        self
    }

    /// Add merge/split support.
    pub fn with_merge_split(mut self) -> Self {
        self.operations = OperationSet::with_merge_split();
        self
    }

    /// Use custom operations.
    pub fn with_operations(mut self, operations: OperationSet) -> Self {
        self.operations = operations;
        self
    }

    /// Add phonetic digraph operations.
    pub fn with_phonetic_digraphs(mut self) -> Self {
        use liblevenshtein::transducer::phonetic::consonant_digraphs;

        let phonetic_ops = consonant_digraphs();
        let mut builder = OperationSetBuilder::new().with_standard_ops();

        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }

        self.operations = builder.build();
        self
    }

    /// Build the WFST.
    pub fn build(self) -> Result<GeneralizedWfst<D>, String> {
        let query = self.query.ok_or_else(|| "Query not set".to_string())?;
        Ok(GeneralizedWfst::new(
            self.dictionary,
            &query,
            self.max_distance,
            self.operations,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use libdictenstein::dynamic_dawg_char::DynamicDawgChar;

    #[test]
    fn test_generalized_wfst_creation() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["hello", "help", "world"]);
        let wfst = GeneralizedWfst::new(&dict, "helo", 2, OperationSet::standard());

        assert!(!wfst.is_empty());
        assert_eq!(wfst.query(), "helo");
        assert_eq!(wfst.max_distance(), 2);
    }

    #[test]
    fn test_generalized_wfst_start_state() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["test"]);
        let wfst = GeneralizedWfst::new(&dict, "tset", 2, OperationSet::standard());

        let start = Wfst::start(&wfst);
        assert!(wfst.is_valid_state(start));
    }

    #[test]
    fn test_generalized_wfst_lazy_expansion() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["hello", "help"]);
        let mut wfst = GeneralizedWfst::new(&dict, "helo", 2, OperationSet::standard());

        let start = Wfst::start(&wfst);
        assert!(!wfst.is_expanded(start));

        wfst.expand(start);
        assert!(wfst.is_expanded(start));
    }

    #[test]
    fn test_builder_creation() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["test"]);
        let result = GeneralizedWfstBuilder::new(&dict)
            .query("tset")
            .max_distance(2)
            .build();

        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_no_query() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["test"]);
        let result = GeneralizedWfstBuilder::new(&dict).build();

        assert!(result.is_err());
    }

    #[test]
    fn test_builder_with_transposition() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["test", "tset"]);
        let result = GeneralizedWfstBuilder::new(&dict)
            .query("tset")
            .max_distance(1)
            .with_transposition()
            .build();

        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_phonetic() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["phone", "graph"]);
        let result = GeneralizedWfstBuilder::new(&dict)
            .query("fone")
            .max_distance(2)
            .with_phonetic_digraphs()
            .build();

        assert!(result.is_ok());
    }

    #[test]
    fn test_wfst_transitions() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["ab", "abc"]);
        let mut wfst = GeneralizedWfst::new(&dict, "ab", 1, OperationSet::standard());

        let start = Wfst::start(&wfst);
        wfst.expand(start);

        let transitions = wfst.transitions(start);
        // Should have transitions for matching 'a' and possibly insertions
        assert!(!transitions.is_empty());
    }

    #[test]
    fn test_wfst_cache_operations() {
        let dict = DynamicDawgChar::<()>::from_terms(vec!["test"]);
        let mut wfst = GeneralizedWfst::new(&dict, "test", 1, OperationSet::standard());

        wfst.expand(0);
        let before = wfst.computed_states();

        wfst.clear_cache();
        assert_eq!(wfst.computed_states(), 0);
        assert!(before > 0);
    }
}