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
//! Builder for weighted pushdown automata.

use std::hash::Hash;

#[cfg(test)]
use super::WeightedPda;
use super::{PdaAcceptMode, PdaTransition, StackAction, StackSymbol, VectorPda};
use crate::semiring::Semiring;
use crate::wfst::StateId;

/// Builder for constructing weighted pushdown automata.
#[derive(Debug, Clone)]
pub struct PdaBuilder<L, W: Semiring> {
    /// The PDA being built.
    pda: VectorPda<L, W>,
    /// Next stack symbol ID.
    next_stack_symbol: u32,
}

impl<L: Clone + Eq + Hash, W: Semiring + Clone> PdaBuilder<L, W> {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            pda: VectorPda::new(),
            next_stack_symbol: 1, // 0 is reserved for BOTTOM
        }
    }

    /// Create a builder with a specific acceptance mode.
    pub fn with_accept_mode(mode: PdaAcceptMode) -> Self {
        Self {
            pda: VectorPda::with_accept_mode(mode),
            next_stack_symbol: 1,
        }
    }

    /// Add a state and return its ID.
    pub fn add_state(&mut self) -> StateId {
        self.pda.add_state()
    }

    /// Add a final state with the given weight.
    pub fn add_final_state(&mut self, weight: W) -> StateId {
        self.pda.add_final_state(weight)
    }

    /// Set the start state.
    pub fn set_start(&mut self, state: StateId) {
        self.pda.set_start(state);
    }

    /// Get the initial stack symbol (Z₀).
    pub fn initial_stack(&self) -> StackSymbol {
        self.pda.get_initial_stack()
    }

    /// Set a custom initial stack symbol.
    pub fn set_initial_stack(&mut self, symbol: StackSymbol) {
        self.pda.set_initial_stack(symbol);
    }

    /// Allocate a new stack symbol.
    pub fn add_stack_symbol(&mut self) -> StackSymbol {
        let id = self.next_stack_symbol;
        self.next_stack_symbol += 1;
        StackSymbol::new(id)
    }

    /// Make a state final.
    pub fn set_final(&mut self, state: StateId, weight: W) {
        self.pda.set_final(state, weight);
    }

    /// Add a transition.
    pub fn add_transition(
        &mut self,
        from: StateId,
        input: Option<L>,
        stack_top: StackSymbol,
        to: StateId,
        stack_action: StackAction,
        weight: W,
    ) -> &mut Self {
        self.pda.add_transition(PdaTransition::new(
            from,
            input,
            stack_top,
            stack_action,
            to,
            weight,
        ));
        self
    }

    /// Add an epsilon transition.
    pub fn add_epsilon_transition(
        &mut self,
        from: StateId,
        stack_top: StackSymbol,
        to: StateId,
        stack_action: StackAction,
        weight: W,
    ) -> &mut Self {
        self.pda
            .add_epsilon_transition(from, stack_top, stack_action, to, weight);
        self
    }

    /// Add a transition that only reads input (no stack change).
    pub fn add_read_transition(
        &mut self,
        from: StateId,
        input: L,
        stack_top: StackSymbol,
        to: StateId,
        weight: W,
    ) -> &mut Self {
        self.add_transition(from, Some(input), stack_top, to, StackAction::Noop, weight)
    }

    /// Add a transition that pushes a symbol.
    pub fn add_push_transition(
        &mut self,
        from: StateId,
        input: Option<L>,
        stack_top: StackSymbol,
        push_symbols: Vec<StackSymbol>,
        to: StateId,
        weight: W,
    ) -> &mut Self {
        self.add_transition(
            from,
            input,
            stack_top,
            to,
            StackAction::Push(push_symbols),
            weight,
        )
    }

    /// Add a transition that pops the stack.
    pub fn add_pop_transition(
        &mut self,
        from: StateId,
        input: Option<L>,
        stack_top: StackSymbol,
        to: StateId,
        weight: W,
    ) -> &mut Self {
        self.add_transition(from, input, stack_top, to, StackAction::Pop, weight)
    }

    /// Add a transition that replaces the stack top.
    pub fn add_replace_transition(
        &mut self,
        from: StateId,
        input: Option<L>,
        stack_top: StackSymbol,
        replace_symbols: Vec<StackSymbol>,
        to: StateId,
        weight: W,
    ) -> &mut Self {
        self.add_transition(
            from,
            input,
            stack_top,
            to,
            StackAction::Replace(replace_symbols),
            weight,
        )
    }

    /// Build the PDA.
    pub fn build(self) -> VectorPda<L, W> {
        self.pda
    }

    /// Get the number of states.
    pub fn num_states(&self) -> usize {
        self.pda.get_num_states()
    }

    /// Get the number of transitions.
    pub fn num_transitions(&self) -> usize {
        self.pda.get_num_transitions()
    }
}

impl<L: Clone + Eq + Hash, W: Semiring + Clone> Default for PdaBuilder<L, W> {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience methods for building common PDA patterns.
impl<L: Clone + Eq + Hash, W: Semiring + Clone> PdaBuilder<L, W> {
    /// Build a PDA for balanced brackets.
    ///
    /// Recognizes strings of balanced open/close pairs.
    /// Uses two states: s0 (final, balanced) and s1 (processing, unbalanced).
    pub fn balanced_brackets(open: L, close: L, weight_one: W) -> VectorPda<L, W> {
        let mut builder = Self::new();

        // s0 is the accepting state (only reachable when brackets are balanced)
        let s0 = builder.add_final_state(weight_one.clone());
        // s1 is the processing state (when we have unmatched open brackets)
        let s1 = builder.add_state();
        builder.set_start(s0);

        let z0 = builder.initial_stack();
        let bracket = builder.add_stack_symbol();

        // From s0 (balanced): on open bracket, push marker and go to s1
        builder.add_push_transition(
            s0,
            Some(open.clone()),
            z0,
            vec![z0, bracket],
            s1,
            weight_one.clone(),
        );

        // From s1 (unbalanced): on open bracket, push another marker
        builder.add_push_transition(
            s1,
            Some(open),
            bracket,
            vec![bracket, bracket],
            s1,
            weight_one.clone(),
        );

        // From s1: on close bracket, pop marker (stay in s1)
        builder.add_pop_transition(s1, Some(close), bracket, s1, weight_one.clone());

        // From s1: when z0 is revealed (all brackets matched), epsilon to s0
        builder.add_epsilon_transition(s1, z0, s0, StackAction::Noop, weight_one);

        builder.build()
    }

    /// Build a PDA for { a^n b^n | n >= 1 }.
    pub fn a_n_b_n(a: L, b: L, weight_one: W) -> VectorPda<L, W> {
        let mut builder = Self::new();

        let reading_as = builder.add_state();
        let reading_bs = builder.add_state();
        let accepting = builder.add_final_state(weight_one.clone());

        builder.set_start(reading_as);

        let z0 = builder.initial_stack();
        let marker = builder.add_stack_symbol();

        // Read first 'a', push marker
        builder.add_push_transition(
            reading_as,
            Some(a.clone()),
            z0,
            vec![z0, marker],
            reading_as,
            weight_one.clone(),
        );

        // Read more 'a's, push markers
        builder.add_push_transition(
            reading_as,
            Some(a),
            marker,
            vec![marker, marker],
            reading_as,
            weight_one.clone(),
        );

        // Switch to reading 'b's
        builder.add_epsilon_transition(
            reading_as,
            marker,
            reading_bs,
            StackAction::Noop,
            weight_one.clone(),
        );

        // Read 'b', pop marker
        builder.add_pop_transition(reading_bs, Some(b), marker, reading_bs, weight_one.clone());

        // Accept when stack has only z0
        builder.add_epsilon_transition(reading_bs, z0, accepting, StackAction::Noop, weight_one);

        builder.build()
    }

    /// Build a PDA for palindromes (with a center marker).
    ///
    /// Recognizes strings of the form w # w^R where w is over the alphabet
    /// and # is a center marker.
    pub fn palindrome_with_center(alphabet: &[L], center: L, weight_one: W) -> VectorPda<L, W>
    where
        L: Clone + Eq + Hash,
    {
        let mut builder = Self::new();

        let reading_first_half = builder.add_state();
        let reading_second_half = builder.add_state();
        let accepting = builder.add_final_state(weight_one.clone());

        builder.set_start(reading_first_half);

        let z0 = builder.initial_stack();

        // Create stack symbols for each alphabet symbol
        let mut symbol_map: std::collections::HashMap<L, StackSymbol> =
            std::collections::HashMap::new();
        for sym in alphabet {
            let stack_sym = builder.add_stack_symbol();
            symbol_map.insert(sym.clone(), stack_sym);
        }

        // First half: read symbols and push them (from z0)
        for sym in alphabet {
            let stack_sym = symbol_map[sym];
            builder.add_push_transition(
                reading_first_half,
                Some(sym.clone()),
                z0,
                vec![z0, stack_sym],
                reading_first_half,
                weight_one.clone(),
            );
        }

        // First half: read symbols and push them (from any stack symbol)
        for sym in alphabet {
            let stack_sym = symbol_map[sym];
            for other_sym in alphabet {
                let other_stack_sym = symbol_map[other_sym];
                builder.add_push_transition(
                    reading_first_half,
                    Some(sym.clone()),
                    other_stack_sym,
                    vec![other_stack_sym, stack_sym],
                    reading_first_half,
                    weight_one.clone(),
                );
            }
        }

        // Read center marker, transition to second half
        for &stack_sym in symbol_map.values() {
            builder.add_read_transition(
                reading_first_half,
                center.clone(),
                stack_sym,
                reading_second_half,
                weight_one.clone(),
            );
        }

        // Also allow center from z0 (empty first half)
        builder.add_read_transition(
            reading_first_half,
            center,
            z0,
            reading_second_half,
            weight_one.clone(),
        );

        // Second half: read symbols and pop matching ones
        for sym in alphabet {
            let stack_sym = symbol_map[sym];
            builder.add_pop_transition(
                reading_second_half,
                Some(sym.clone()),
                stack_sym,
                reading_second_half,
                weight_one.clone(),
            );
        }

        // Accept when stack is empty (only z0 remains)
        builder.add_epsilon_transition(
            reading_second_half,
            z0,
            accepting,
            StackAction::Noop,
            weight_one,
        );

        builder.build()
    }
}

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

    #[test]
    fn test_builder_basic() {
        let mut builder: PdaBuilder<char, TropicalWeight> = PdaBuilder::new();

        let s0 = builder.add_state();
        let s1 = builder.add_final_state(TropicalWeight::one());

        builder.set_start(s0);

        let z0 = builder.initial_stack();
        let marker = builder.add_stack_symbol();

        builder.add_push_transition(
            s0,
            Some('a'),
            z0,
            vec![z0, marker],
            s1,
            TropicalWeight::one(),
        );

        let pda = builder.build();

        assert_eq!(pda.num_states(), 2);
        assert_eq!(pda.num_transitions(), 1);
    }

    #[test]
    fn test_add_stack_symbol() {
        let mut builder: PdaBuilder<char, TropicalWeight> = PdaBuilder::new();

        let sym1 = builder.add_stack_symbol();
        let sym2 = builder.add_stack_symbol();
        let sym3 = builder.add_stack_symbol();

        assert_eq!(sym1.id(), 1);
        assert_eq!(sym2.id(), 2);
        assert_eq!(sym3.id(), 3);
    }

    #[test]
    fn test_balanced_brackets() {
        let pda = PdaBuilder::balanced_brackets('(', ')', TropicalWeight::one());

        assert!(pda.accepts("".chars()));
        assert!(pda.accepts("()".chars()));
        assert!(pda.accepts("(())".chars()));
        assert!(pda.accepts("((()))".chars()));
        assert!(!pda.accepts("(".chars()));
        assert!(!pda.accepts(")".chars()));
        assert!(!pda.accepts("(()".chars()));
    }

    #[test]
    fn test_a_n_b_n() {
        let pda = PdaBuilder::a_n_b_n('a', 'b', TropicalWeight::one());

        assert!(pda.accepts("ab".chars()));
        assert!(pda.accepts("aabb".chars()));
        assert!(pda.accepts("aaabbb".chars()));
        assert!(!pda.accepts("".chars()));
        assert!(!pda.accepts("a".chars()));
        assert!(!pda.accepts("b".chars()));
        assert!(!pda.accepts("aab".chars()));
        assert!(!pda.accepts("abb".chars()));
    }

    #[test]
    fn test_palindrome_with_center() {
        let alphabet = vec!['a', 'b'];
        let pda = PdaBuilder::palindrome_with_center(&alphabet, '#', TropicalWeight::one());

        assert!(pda.accepts("#".chars())); // Empty palindrome
        assert!(pda.accepts("a#a".chars()));
        assert!(pda.accepts("b#b".chars()));
        assert!(pda.accepts("ab#ba".chars()));
        assert!(pda.accepts("aba#aba".chars()));
        assert!(pda.accepts("aab#baa".chars()));
        assert!(!pda.accepts("a#b".chars()));
        assert!(!pda.accepts("ab#ab".chars()));
        assert!(!pda.accepts("ab#".chars()));
    }

    #[test]
    fn test_builder_chaining() {
        let mut builder: PdaBuilder<char, TropicalWeight> = PdaBuilder::new();

        let s0 = builder.add_state();
        let s1 = builder.add_state();
        let s2 = builder.add_final_state(TropicalWeight::one());

        builder.set_start(s0);

        let z0 = builder.initial_stack();
        let marker = builder.add_stack_symbol();

        builder
            .add_push_transition(
                s0,
                Some('a'),
                z0,
                vec![z0, marker],
                s1,
                TropicalWeight::one(),
            )
            .add_pop_transition(s1, Some('b'), marker, s2, TropicalWeight::one());

        let pda = builder.build();

        assert!(pda.accepts("ab".chars()));
        assert!(!pda.accepts("a".chars()));
        assert!(!pda.accepts("b".chars()));
    }

    #[test]
    fn test_accept_mode_builder() {
        let builder: PdaBuilder<char, TropicalWeight> =
            PdaBuilder::with_accept_mode(PdaAcceptMode::EmptyStack);

        let pda = builder.build();
        assert_eq!(pda.accept_mode(), PdaAcceptMode::EmptyStack);
    }

    #[test]
    fn test_read_transition() {
        let mut builder: PdaBuilder<char, TropicalWeight> = PdaBuilder::new();

        let s0 = builder.add_state();
        let s1 = builder.add_final_state(TropicalWeight::one());

        builder.set_start(s0);

        let z0 = builder.initial_stack();

        builder.add_read_transition(s0, 'a', z0, s1, TropicalWeight::one());

        let pda = builder.build();

        let trans = &pda.transitions(s0)[0];
        assert!(trans.stack_action.is_noop());
        assert_eq!(trans.input, Some('a'));
    }

    #[test]
    fn test_replace_transition() {
        let mut builder: PdaBuilder<char, TropicalWeight> = PdaBuilder::new();

        let s0 = builder.add_state();
        let s1 = builder.add_state();

        builder.set_start(s0);

        let z0 = builder.initial_stack();
        let marker1 = builder.add_stack_symbol();
        let marker2 = builder.add_stack_symbol();

        builder.add_replace_transition(
            s0,
            Some('a'),
            z0,
            vec![marker1, marker2],
            s1,
            TropicalWeight::one(),
        );

        let pda = builder.build();

        let trans = &pda.transitions(s0)[0];
        match &trans.stack_action {
            StackAction::Replace(symbols) => {
                assert_eq!(symbols.len(), 2);
                assert_eq!(symbols[0], marker1);
                assert_eq!(symbols[1], marker2);
            }
            _ => panic!("Expected Replace action"),
        }
    }
}