libgrammstein 0.1.0

Hybrid language model (N-gram + Embeddings) for WFST text 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
//! Probabilistic Context-Free Grammar (PCFG) for programming languages.
//!
//! This module provides:
//! - PCFG training from parsed code corpora
//! - Grammar rule probability estimation
//! - WFST export for integration with lling-llang
//!
//! Since programming languages have known formal grammars, PCFGs can be used
//! to constrain correction candidates to syntactically valid outputs.

use super::ast::{AstNode, ParsedCode};
use super::language::CodeLanguage;
use std::collections::HashMap;
use std::hash::Hash;

/// A production rule in the grammar.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Production {
    /// Left-hand side (non-terminal)
    pub lhs: String,
    /// Right-hand side (sequence of symbols)
    pub rhs: Vec<Symbol>,
}

impl Production {
    /// Creates a new production rule.
    pub fn new(lhs: impl Into<String>, rhs: Vec<Symbol>) -> Self {
        Self {
            lhs: lhs.into(),
            rhs,
        }
    }

    /// Returns true if this is an epsilon (empty) production.
    pub fn is_epsilon(&self) -> bool {
        self.rhs.is_empty()
    }

    /// Returns the arity (number of RHS symbols) of this production.
    pub fn arity(&self) -> usize {
        self.rhs.len()
    }
}

impl std::fmt::Display for Production {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ->", self.lhs)?;
        for sym in &self.rhs {
            write!(f, " {}", sym)?;
        }
        Ok(())
    }
}

/// A symbol in the grammar (terminal or non-terminal).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Symbol {
    /// Non-terminal symbol (e.g., "expression", "statement")
    NonTerminal(String),
    /// Terminal symbol (actual token, e.g., "if", "+", identifier)
    Terminal(String),
}

impl Symbol {
    /// Creates a non-terminal symbol.
    pub fn non_terminal(s: impl Into<String>) -> Self {
        Symbol::NonTerminal(s.into())
    }

    /// Creates a terminal symbol.
    pub fn terminal(s: impl Into<String>) -> Self {
        Symbol::Terminal(s.into())
    }

    /// Returns true if this is a non-terminal.
    pub fn is_non_terminal(&self) -> bool {
        matches!(self, Symbol::NonTerminal(_))
    }

    /// Returns true if this is a terminal.
    pub fn is_terminal(&self) -> bool {
        matches!(self, Symbol::Terminal(_))
    }

    /// Returns the symbol name.
    pub fn name(&self) -> &str {
        match self {
            Symbol::NonTerminal(s) | Symbol::Terminal(s) => s,
        }
    }
}

impl std::fmt::Display for Symbol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Symbol::NonTerminal(s) => write!(f, "<{}>", s),
            Symbol::Terminal(s) => write!(f, "'{}'", s),
        }
    }
}

/// A weighted context-free grammar.
///
/// Each production rule has an associated weight (unnormalized) or probability.
/// Weights can be converted to probabilities by normalizing over all rules
/// with the same LHS.
#[derive(Debug, Clone)]
pub struct WeightedCFG {
    /// Production rules with their weights
    rules: HashMap<Production, f64>,
    /// Rules indexed by LHS for efficient lookup
    rules_by_lhs: HashMap<String, Vec<(Production, f64)>>,
    /// Start symbol
    start_symbol: String,
    /// Total weight for each non-terminal (for normalization)
    lhs_totals: HashMap<String, f64>,
}

impl WeightedCFG {
    /// Creates a new weighted CFG with the given start symbol.
    pub fn new(start_symbol: impl Into<String>) -> Self {
        Self {
            rules: HashMap::new(),
            rules_by_lhs: HashMap::new(),
            start_symbol: start_symbol.into(),
            lhs_totals: HashMap::new(),
        }
    }

    /// Adds a production rule with the given weight.
    pub fn add_rule(&mut self, production: Production, weight: f64) {
        let lhs = production.lhs.clone();

        // Update total weight for this LHS
        *self.lhs_totals.entry(lhs.clone()).or_insert(0.0) += weight;

        // Store the rule
        *self.rules.entry(production.clone()).or_insert(0.0) += weight;

        // Index by LHS
        self.rules_by_lhs
            .entry(lhs)
            .or_default()
            .push((production, weight));
    }

    /// Returns the weight of a production rule.
    pub fn weight(&self, production: &Production) -> f64 {
        self.rules.get(production).copied().unwrap_or(0.0)
    }

    /// Returns the probability of a production rule (normalized).
    pub fn probability(&self, production: &Production) -> f64 {
        let weight = self.weight(production);
        let total = self.lhs_totals.get(&production.lhs).copied().unwrap_or(1.0);
        if total > 0.0 {
            weight / total
        } else {
            0.0
        }
    }

    /// Returns the log probability of a production rule.
    pub fn log_probability(&self, production: &Production) -> f64 {
        let prob = self.probability(production);
        if prob > 0.0 {
            prob.ln()
        } else {
            f64::NEG_INFINITY
        }
    }

    /// Returns all rules with the given LHS.
    pub fn rules_for(&self, lhs: &str) -> Vec<(&Production, f64)> {
        self.rules_by_lhs
            .get(lhs)
            .map(|rules| rules.iter().map(|(p, w)| (p, *w)).collect())
            .unwrap_or_default()
    }

    /// Returns the start symbol.
    pub fn start_symbol(&self) -> &str {
        &self.start_symbol
    }

    /// Returns all non-terminals in the grammar.
    pub fn non_terminals(&self) -> impl Iterator<Item = &str> {
        self.rules_by_lhs.keys().map(|s| s.as_str())
    }

    /// Returns all terminals in the grammar.
    pub fn terminals(&self) -> impl Iterator<Item = &str> {
        self.rules
            .keys()
            .flat_map(|p| p.rhs.iter())
            .filter_map(|s| match s {
                Symbol::Terminal(t) => Some(t.as_str()),
                _ => None,
            })
    }

    /// Returns the number of production rules.
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// Iterates over all production rules and their weights.
    pub fn iter_rules(&self) -> impl Iterator<Item = (&Production, &f64)> {
        self.rules.iter()
    }

    /// Returns all production rules.
    pub fn rules(&self) -> &HashMap<Production, f64> {
        &self.rules
    }

    /// Normalizes all weights to probabilities.
    pub fn normalize(&mut self) {
        let mut normalized_rules = HashMap::new();

        for (production, weight) in &self.rules {
            let total = self.lhs_totals.get(&production.lhs).copied().unwrap_or(1.0);
            let prob = if total > 0.0 { weight / total } else { 0.0 };
            normalized_rules.insert(production.clone(), prob);
        }

        self.rules = normalized_rules;

        // Reset totals to 1.0 for all LHS
        for total in self.lhs_totals.values_mut() {
            *total = 1.0;
        }

        // Rebuild rules_by_lhs
        self.rules_by_lhs.clear();
        for (production, weight) in &self.rules {
            self.rules_by_lhs
                .entry(production.lhs.clone())
                .or_default()
                .push((production.clone(), *weight));
        }
    }
}

/// Trainer for building PCFGs from parsed code corpora.
pub struct PcfgTrainer<'a, L: CodeLanguage> {
    language: &'a L,
    rule_counts: HashMap<Production, u64>,
    start_symbol: String,
}

impl<'a, L: CodeLanguage> PcfgTrainer<'a, L> {
    /// Creates a new PCFG trainer for the given language.
    pub fn new(language: &'a L) -> Self {
        Self {
            language,
            rule_counts: HashMap::new(),
            start_symbol: "source_file".to_string(),
        }
    }

    /// Sets the start symbol for the grammar.
    pub fn with_start_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.start_symbol = symbol.into();
        self
    }

    /// Returns the language used to parse training inputs.
    pub fn language(&self) -> &L {
        self.language
    }

    /// Trains the PCFG from a single parsed file.
    pub fn train_from_parsed(&mut self, parsed: &ParsedCode) {
        let ast = AstNode::from_ts_node(parsed.root(), &parsed.source);
        self.extract_rules(&ast);
    }

    /// Trains the PCFG from multiple parsed files.
    pub fn train_from_parsed_iter<'b, I>(&mut self, parsed_iter: I)
    where
        I: Iterator<Item = &'b ParsedCode>,
    {
        for parsed in parsed_iter {
            self.train_from_parsed(parsed);
        }
    }

    /// Extracts production rules from an AST node recursively.
    fn extract_rules(&mut self, node: &AstNode) {
        // Skip error nodes
        if node.is_error || node.is_missing {
            return;
        }

        // Only create rules for named nodes (non-terminals)
        if node.is_named && !node.children.is_empty() {
            let lhs = node.kind.clone();
            let rhs: Vec<Symbol> = node
                .children
                .iter()
                .filter(|c| c.is_named) // Only named children
                .map(|c| {
                    if c.children.is_empty() && c.text.is_some() {
                        // Leaf node with text -> terminal
                        Symbol::Terminal(c.kind.clone())
                    } else {
                        // Non-leaf -> non-terminal
                        Symbol::NonTerminal(c.kind.clone())
                    }
                })
                .collect();

            if !rhs.is_empty() {
                let production = Production::new(lhs, rhs);
                *self.rule_counts.entry(production).or_insert(0) += 1;
            }
        }

        // Recurse into children
        for child in &node.children {
            self.extract_rules(child);
        }
    }

    /// Converts accumulated counts to a weighted CFG.
    pub fn to_weighted_cfg(&self) -> WeightedCFG {
        let mut cfg = WeightedCFG::new(self.start_symbol.clone());

        for (production, count) in &self.rule_counts {
            cfg.add_rule(production.clone(), *count as f64);
        }

        cfg
    }

    /// Returns the rule counts for inspection.
    pub fn rule_counts(&self) -> &HashMap<Production, u64> {
        &self.rule_counts
    }

    /// Returns the number of unique rules observed.
    pub fn unique_rule_count(&self) -> usize {
        self.rule_counts.len()
    }

    /// Returns the total number of rule instances observed.
    pub fn total_rule_count(&self) -> u64 {
        self.rule_counts.values().sum()
    }

    /// Clears all accumulated counts.
    pub fn clear(&mut self) {
        self.rule_counts.clear();
    }
}

/// Configuration for WFST export of a PCFG.
#[derive(Debug, Clone)]
pub struct PcfgWfstConfig {
    /// Whether to include epsilon transitions for optional rules
    pub include_epsilon: bool,
    /// Minimum probability threshold for rules (rules below this are excluded)
    pub min_probability: f64,
    /// Maximum number of rules to include
    pub max_rules: Option<usize>,
}

impl Default for PcfgWfstConfig {
    fn default() -> Self {
        Self {
            include_epsilon: true,
            min_probability: 1e-10,
            max_rules: None,
        }
    }
}

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

    #[test]
    fn test_production_display() {
        let prod = Production::new(
            "expr",
            vec![
                Symbol::NonTerminal("term".to_string()),
                Symbol::Terminal("+".to_string()),
                Symbol::NonTerminal("expr".to_string()),
            ],
        );

        assert_eq!(format!("{}", prod), "expr -> <term> '+' <expr>");
    }

    #[test]
    fn test_weighted_cfg_probability() {
        let mut cfg = WeightedCFG::new("S");

        // Add rules: S -> A (weight 3), S -> B (weight 1)
        cfg.add_rule(
            Production::new("S", vec![Symbol::NonTerminal("A".to_string())]),
            3.0,
        );
        cfg.add_rule(
            Production::new("S", vec![Symbol::NonTerminal("B".to_string())]),
            1.0,
        );

        let prob_a = cfg.probability(&Production::new(
            "S",
            vec![Symbol::NonTerminal("A".to_string())],
        ));
        let prob_b = cfg.probability(&Production::new(
            "S",
            vec![Symbol::NonTerminal("B".to_string())],
        ));

        assert!((prob_a - 0.75).abs() < 1e-6);
        assert!((prob_b - 0.25).abs() < 1e-6);
    }

    #[test]
    fn test_symbol_types() {
        let nt = Symbol::non_terminal("expr");
        let t = Symbol::terminal("+");

        assert!(nt.is_non_terminal());
        assert!(!nt.is_terminal());
        assert!(!t.is_non_terminal());
        assert!(t.is_terminal());
        assert_eq!(nt.name(), "expr");
        assert_eq!(t.name(), "+");
    }
}