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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Earley parser for lattice input.
//!
//! The Earley parser is modified to work with lattices instead of strings:
//! - Scanner follows lattice edges instead of string positions
//! - Multiple edges at a position are handled naturally
//! - Chart positions correspond to lattice nodes

use std::collections::VecDeque;
use std::fmt;
use std::hash::{Hash, Hasher};

use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;

use super::forest::{ForestChild, ForestNode, ForestNodeId, ParseForest};
use super::grammar::Grammar;
use super::types::{NonTerminal, RuleId, Symbol, Terminal};

use crate::backend::LatticeBackend;
use crate::lattice::{EdgeId, Lattice, NodeId};
use crate::semiring::Semiring;

/// An Earley item (chart state).
///
/// Represents a partially-matched production rule.
///
/// Note: Hash and Eq are implemented manually to only compare (rule, dot, start).
/// The forest_node, terminal_edges, and child_nodes fields are metadata and don't affect state identity.
#[derive(Clone, Debug)]
pub struct EarleyState {
    /// The production rule.
    pub rule: RuleId,
    /// Position of the dot (how much has been matched).
    pub dot: usize,
    /// Starting position (lattice node) of this rule.
    pub start: NodeId,
    /// Parse forest back-pointers for completed constituents (deprecated, kept for compatibility).
    pub forest_node: Option<ForestNodeId>,
    /// Terminal edges consumed during scanning (in order of consumption).
    pub terminal_edges: SmallVec<[EdgeId; 4]>,
    /// Child forest nodes accumulated as we match RHS symbols (non-terminals and terminals).
    pub child_nodes: SmallVec<[ForestChild; 4]>,
}

impl PartialEq for EarleyState {
    fn eq(&self, other: &Self) -> bool {
        // State identity is based on (rule, dot, start) only
        self.rule == other.rule && self.dot == other.dot && self.start == other.start
    }
}

impl Eq for EarleyState {}

impl Hash for EarleyState {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Hash only (rule, dot, start) for chart deduplication
        self.rule.hash(state);
        self.dot.hash(state);
        self.start.hash(state);
    }
}

impl EarleyState {
    /// Create a new Earley state.
    pub fn new(rule: RuleId, dot: usize, start: NodeId) -> Self {
        Self {
            rule,
            dot,
            start,
            forest_node: None,
            terminal_edges: SmallVec::new(),
            child_nodes: SmallVec::new(),
        }
    }

    /// Create a state with a forest node.
    pub fn with_forest(rule: RuleId, dot: usize, start: NodeId, forest: ForestNodeId) -> Self {
        Self {
            rule,
            dot,
            start,
            forest_node: Some(forest),
            terminal_edges: SmallVec::new(),
            child_nodes: SmallVec::new(),
        }
    }

    /// Advance the dot by one position.
    pub fn advance(&self) -> Self {
        Self {
            rule: self.rule,
            dot: self.dot + 1,
            start: self.start,
            forest_node: self.forest_node,
            terminal_edges: self.terminal_edges.clone(),
            child_nodes: self.child_nodes.clone(),
        }
    }

    /// Advance the dot by one position after consuming a terminal edge.
    pub fn advance_with_terminal(&self, edge_id: EdgeId) -> Self {
        let mut terminal_edges = self.terminal_edges.clone();
        terminal_edges.push(edge_id);
        let mut child_nodes = self.child_nodes.clone();
        child_nodes.push(ForestChild::Terminal(edge_id));
        Self {
            rule: self.rule,
            dot: self.dot + 1,
            start: self.start,
            forest_node: self.forest_node,
            terminal_edges,
            child_nodes,
        }
    }

    /// Advance the dot by one position after matching a non-terminal.
    pub fn advance_with_nonterminal(&self, child_forest_node: ForestNodeId) -> Self {
        let mut child_nodes = self.child_nodes.clone();
        child_nodes.push(ForestChild::Derivation(smallvec::smallvec![
            child_forest_node
        ]));
        Self {
            rule: self.rule,
            dot: self.dot + 1,
            start: self.start,
            forest_node: Some(child_forest_node),
            terminal_edges: self.terminal_edges.clone(),
            child_nodes,
        }
    }

    /// Check if the rule is complete (dot at end).
    pub fn is_complete(&self, grammar: &Grammar) -> bool {
        let prod = grammar.production(self.rule).expect("valid rule");
        self.dot >= prod.rhs_len()
    }

    /// Get the symbol after the dot, if any.
    pub fn next_symbol<'a>(&self, grammar: &'a Grammar) -> Option<&'a Symbol> {
        let prod = grammar.production(self.rule)?;
        prod.rhs_at(self.dot)
    }

    /// Get the LHS of this state's rule.
    pub fn lhs(&self, grammar: &Grammar) -> NonTerminal {
        grammar.production(self.rule).expect("valid rule").lhs
    }
}

impl fmt::Display for EarleyState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}, {}, {}, {:?}, edges: {}]",
            self.rule,
            self.dot,
            self.start.0,
            self.forest_node,
            self.terminal_edges.len()
        )
    }
}

/// Key for Earley chart lookup (uses rule, dot, start for identity).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct EarleyKey {
    rule: RuleId,
    dot: usize,
    start: NodeId,
}

impl From<&EarleyState> for EarleyKey {
    fn from(state: &EarleyState) -> Self {
        Self {
            rule: state.rule,
            dot: state.dot,
            start: state.start,
        }
    }
}

/// Earley chart: collection of items indexed by position.
#[derive(Clone, Debug)]
pub struct EarleyChart {
    /// Items at each position (lattice node), keyed for deduplication with child merging.
    positions: FxHashMap<NodeId, FxHashMap<EarleyKey, EarleyState>>,
    /// Agenda for processing.
    agenda: VecDeque<(NodeId, EarleyState)>,
    /// Set of (pos, key) pairs already processed to avoid re-adding to agenda.
    processed: FxHashSet<(NodeId, EarleyKey)>,
}

impl EarleyChart {
    /// Create a new empty chart.
    pub fn new() -> Self {
        Self {
            positions: FxHashMap::default(),
            agenda: VecDeque::new(),
            processed: FxHashSet::default(),
        }
    }

    /// Add an item to the chart at a position.
    ///
    /// Returns true if the item was new (not a duplicate).
    /// If the item already exists, children are merged.
    pub fn add(&mut self, pos: NodeId, state: EarleyState) -> bool {
        let key = EarleyKey::from(&state);
        let map = self.positions.entry(pos).or_default();

        if let Some(existing) = map.get_mut(&key) {
            // Merge children from the new state into the existing one
            for child in state.child_nodes {
                if !existing.child_nodes.contains(&child) {
                    existing.child_nodes.push(child);
                }
            }
            for edge in state.terminal_edges {
                if !existing.terminal_edges.contains(&edge) {
                    existing.terminal_edges.push(edge);
                }
            }
            // Update forest_node if the new one is set
            if state.forest_node.is_some() && existing.forest_node.is_none() {
                existing.forest_node = state.forest_node;
            }
            false // Not a new state
        } else {
            map.insert(key.clone(), state.clone());
            // Only add to agenda if not already processed
            if !self.processed.contains(&(pos, key.clone())) {
                self.processed.insert((pos, key));
                self.agenda.push_back((pos, state));
            }
            true
        }
    }

    /// Get all items at a position.
    pub fn at(&self, pos: NodeId) -> impl Iterator<Item = &EarleyState> {
        self.positions
            .get(&pos)
            .into_iter()
            .flat_map(|s| s.values())
    }

    /// Pop the next item from the agenda.
    pub fn pop(&mut self) -> Option<(NodeId, EarleyState)> {
        self.agenda.pop_front()
    }

    /// Check if the agenda is empty.
    pub fn is_agenda_empty(&self) -> bool {
        self.agenda.is_empty()
    }

    /// Get the number of items in the chart.
    pub fn len(&self) -> usize {
        self.positions.values().map(|s| s.len()).sum()
    }

    /// Check if the chart is empty.
    pub fn is_empty(&self) -> bool {
        self.positions.is_empty()
    }
}

impl Default for EarleyChart {
    fn default() -> Self {
        Self::new()
    }
}

/// Parse error.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParseError {
    /// No complete parse found.
    NoParse,
    /// Empty lattice.
    EmptyLattice,
    /// Grammar error.
    GrammarError(String),
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::NoParse => write!(f, "no complete parse found"),
            ParseError::EmptyLattice => write!(f, "empty lattice"),
            ParseError::GrammarError(msg) => write!(f, "grammar error: {}", msg),
        }
    }
}

impl std::error::Error for ParseError {}

/// Earley parser for lattices.
pub struct EarleyParser<'g> {
    grammar: &'g Grammar,
    /// Nullable non-terminals (can derive epsilon).
    nullable: Vec<bool>,
}

impl<'g> EarleyParser<'g> {
    /// Create a new parser for the given grammar.
    pub fn new(grammar: &'g Grammar) -> Self {
        let nullable = grammar.compute_nullable();
        Self { grammar, nullable }
    }

    /// Parse a lattice and return a parse forest.
    pub fn parse_lattice<W: Semiring, B: LatticeBackend>(
        &self,
        lattice: &Lattice<W, B>,
    ) -> Result<ParseForest, ParseError> {
        if lattice.is_empty() && lattice.start() != lattice.end() {
            return Err(ParseError::EmptyLattice);
        }

        let mut chart = EarleyChart::new();
        let mut forest = ParseForest::new();

        let start = lattice.start();

        // Initialize: Add S → •α for all S-productions
        self.predict(&mut chart, start, self.grammar.start());

        // Process agenda
        while let Some((pos, state)) = chart.pop() {
            if state.is_complete(self.grammar) {
                // Completer
                self.complete(&mut chart, &mut forest, lattice, pos, &state);
            } else {
                let next_sym = state.next_symbol(self.grammar);
                match next_sym {
                    Some(Symbol::NonTerminal(nt)) => {
                        // Predictor
                        self.predict(&mut chart, pos, *nt);

                        // Handle nullable non-terminals (epsilon completion)
                        if self.nullable[nt.index() as usize] {
                            let advanced = state.advance();
                            chart.add(pos, advanced);
                        }
                    }
                    Some(Symbol::Terminal(terminal)) => {
                        // Scanner
                        self.scan(&mut chart, lattice, pos, &state, *terminal);
                    }
                    Some(Symbol::Epsilon) => {
                        // Skip epsilon
                        let advanced = state.advance();
                        chart.add(pos, advanced);
                    }
                    None => {
                        // Already complete, handled above
                    }
                }
            }
        }

        // Check for successful parse by looking at the forest roots
        // (roots are now added in complete() when start-symbol rules finish)
        if !forest.is_empty() {
            Ok(forest)
        } else {
            Err(ParseError::NoParse)
        }
    }

    /// Predictor: Add items for productions of a non-terminal.
    fn predict(&self, chart: &mut EarleyChart, pos: NodeId, nt: NonTerminal) {
        for prod in self.grammar.productions_for(nt) {
            let state = EarleyState::new(prod.id, 0, pos);
            chart.add(pos, state);
        }
    }

    /// Scanner: Match a terminal against lattice edges.
    fn scan<W: Semiring, B: LatticeBackend>(
        &self,
        chart: &mut EarleyChart,
        lattice: &Lattice<W, B>,
        pos: NodeId,
        state: &EarleyState,
        terminal: Terminal,
    ) {
        // Look for matching edges
        for edge in lattice.outgoing_edges(pos) {
            if edge.label == terminal.vocab_id() {
                // Advance and track the edge that was consumed
                let advanced = state.advance_with_terminal(edge.id);
                chart.add(edge.target, advanced);
            }
        }
    }

    /// Completer: When a rule completes, advance waiting rules.
    fn complete<W: Semiring, B: LatticeBackend>(
        &self,
        chart: &mut EarleyChart,
        forest: &mut ParseForest,
        lattice: &Lattice<W, B>,
        pos: NodeId,
        completed: &EarleyState,
    ) {
        let completed_nt = completed.lhs(self.grammar);

        // Create forest node for completed rule
        let mut node = ForestNode::new(completed.rule, completed.start, pos);

        // Add all children accumulated during parsing this rule
        node.children = completed.child_nodes.clone();

        let forest_node = forest.add_node(node);

        // If this is a start-symbol rule spanning the entire input, add as root
        if completed_nt == self.grammar.start()
            && completed.start == lattice.start()
            && pos == lattice.end()
        {
            forest.add_root(forest_node);
        }

        // Find items waiting for this non-terminal
        let waiting: Vec<_> = chart.at(completed.start)
            .filter(|s| {
                !s.is_complete(self.grammar) &&
                matches!(s.next_symbol(self.grammar), Some(Symbol::NonTerminal(nt)) if *nt == completed_nt)
            })
            .cloned()
            .collect();

        for waiter in waiting {
            // Advance with the completed non-terminal's forest node
            let advanced = waiter.advance_with_nonterminal(forest_node);
            chart.add(pos, advanced);
        }
    }

    /// Check if the grammar accepts the lattice.
    pub fn accepts<W: Semiring, B: LatticeBackend>(&self, lattice: &Lattice<W, B>) -> bool {
        self.parse_lattice(lattice).is_ok()
    }

    /// Get the grammar.
    pub fn grammar(&self) -> &Grammar {
        self.grammar
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::HashMapBackend;
    use crate::cfg::GrammarBuilder;
    use crate::lattice::{EdgeMetadata, LatticeBuilder};
    use crate::semiring::TropicalWeight;

    fn simple_grammar() -> Grammar {
        // S → NP VP
        // NP → Det N
        // VP → V NP | V
        // Det → "the" | "a"
        // N → "dog" | "cat"
        // V → "saw" | "chased"
        GrammarBuilder::new()
            .start("S")
            .rule("S", &["NP", "VP"])
            .rule("NP", &["Det", "N"])
            .rule("VP", &["V", "NP"])
            .rule("VP", &["V"])
            .rule("Det", &["the"])
            .rule("Det", &["a"])
            .rule("N", &["dog"])
            .rule("N", &["cat"])
            .rule("V", &["saw"])
            .rule("V", &["chased"])
            .build()
            .expect("valid grammar")
    }

    fn build_lattice(words: &[&str], grammar: &Grammar) -> Lattice<TropicalWeight, HashMapBackend> {
        let mut backend = HashMapBackend::new();

        // Get terminal IDs from grammar and also intern words in backend
        let word_ids: Vec<_> = words
            .iter()
            .map(|w| {
                // Get the terminal ID from grammar (this is what the parser expects)
                let t = grammar
                    .terminal_by_name(w)
                    .expect(&format!("unknown word: {}", w));
                // Intern in backend for lookup purposes
                let _id = backend.intern(w);
                // Use grammar's terminal ID for the edge
                t.vocab_id()
            })
            .collect();

        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        for (i, &id) in word_ids.iter().enumerate() {
            builder.add_correction_by_id(
                i,
                i + 1,
                id,
                TropicalWeight::one(),
                EdgeMetadata::default(),
            );
        }

        builder.build(words.len())
    }

    #[test]
    fn test_earley_state() {
        let state = EarleyState::new(RuleId::new(0), 1, NodeId(0));
        assert_eq!(state.rule, RuleId::new(0));
        assert_eq!(state.dot, 1);
        assert_eq!(state.start, NodeId(0));
        assert!(state.forest_node.is_none());

        let advanced = state.advance();
        assert_eq!(advanced.dot, 2);
    }

    #[test]
    fn test_earley_chart() {
        let mut chart = EarleyChart::new();
        assert!(chart.is_empty());

        let state = EarleyState::new(RuleId::new(0), 0, NodeId(0));
        assert!(chart.add(NodeId(0), state.clone()));
        assert!(!chart.add(NodeId(0), state.clone())); // Duplicate

        assert_eq!(chart.len(), 1);
        assert!(!chart.is_agenda_empty());

        let (pos, s) = chart.pop().expect("item");
        assert_eq!(pos, NodeId(0));
        assert_eq!(s.rule, RuleId::new(0));
    }

    #[test]
    fn test_parse_simple_sentence() {
        let grammar = simple_grammar();
        let parser = EarleyParser::new(&grammar);

        // "the dog saw a cat"
        let lattice = build_lattice(&["the", "dog", "saw", "a", "cat"], &grammar);
        let result = parser.parse_lattice(&lattice);

        assert!(result.is_ok(), "Parse should succeed: {:?}", result);
    }

    #[test]
    fn test_parse_intransitive() {
        let grammar = simple_grammar();
        let parser = EarleyParser::new(&grammar);

        // "the dog saw" (intransitive use)
        let lattice = build_lattice(&["the", "dog", "saw"], &grammar);
        let result = parser.parse_lattice(&lattice);

        assert!(result.is_ok(), "Parse should succeed: {:?}", result);
    }

    #[test]
    fn test_parse_failure() {
        let grammar = simple_grammar();
        let parser = EarleyParser::new(&grammar);

        // "saw the" - not a valid sentence
        let mut backend = HashMapBackend::new();
        // Intern for backend lookup
        let _saw_interned = backend.intern("saw");
        let _the_interned = backend.intern("the");

        // Use grammar's terminal IDs for edges (what the parser expects)
        let saw_id = grammar.terminal_by_name("saw").expect("saw").vocab_id();
        let the_id = grammar.terminal_by_name("the").expect("the").vocab_id();

        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);
        builder.add_correction_by_id(0, 1, saw_id, TropicalWeight::one(), EdgeMetadata::default());
        builder.add_correction_by_id(1, 2, the_id, TropicalWeight::one(), EdgeMetadata::default());
        let lattice = builder.build(2);

        let result = parser.parse_lattice(&lattice);
        assert!(matches!(result, Err(ParseError::NoParse)));
    }

    #[test]
    fn test_accepts() {
        let grammar = simple_grammar();
        let parser = EarleyParser::new(&grammar);

        let lattice = build_lattice(&["the", "dog", "saw"], &grammar);
        assert!(parser.accepts(&lattice));
    }

    #[test]
    fn test_nullable_handling() {
        // Grammar with nullable: S → A B, A → ε | "a"
        let grammar = GrammarBuilder::new()
            .start("S")
            .rule("S", &["A", "B"])
            .epsilon_rule("A")
            .rule("A", &["a"])
            .rule("B", &["b"])
            .build()
            .expect("valid grammar");

        let parser = EarleyParser::new(&grammar);

        // "b" should parse (A → ε)
        let mut backend = HashMapBackend::new();
        let _b_interned = backend.intern("b");
        // Use grammar's terminal ID for the edge
        let b_id = grammar.terminal_by_name("b").expect("b").vocab_id();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);
        builder.add_correction_by_id(0, 1, b_id, TropicalWeight::one(), EdgeMetadata::default());
        let lattice = builder.build(1);

        let result = parser.parse_lattice(&lattice);
        assert!(
            result.is_ok(),
            "Parse with nullable should succeed: {:?}",
            result
        );
    }
}