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
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
//! Ensemble corrector combining lexical, grammar, and semantic sources.
//!
//! This corrector orchestrates multiple correction sources and combines
//! their suggestions using configurable weighting and deduplication.

use super::grammar::GrammarCorrector;
use super::lexical::LexicalCorrector;
use super::semantic::SemanticCorrector;
use crate::code::ast::ParsedCode;
use crate::code::correction::{CodeCorrector, Correction, CorrectionSource};
use crate::code::cpg::CodePropertyGraph;
use crate::code::language::{CodeLanguage, TokenContext};
use crate::code::pcfg::WeightedCFG;
use crate::code::tokenizer::CodeToken;
use std::collections::HashMap;
use std::sync::Arc;

/// Configuration for the ensemble corrector.
#[derive(Debug, Clone)]
pub struct EnsembleCorrectorConfig {
    /// Weight for lexical corrections (0.0 - 1.0)
    pub lexical_weight: f64,
    /// Weight for grammar corrections (0.0 - 1.0)
    pub grammar_weight: f64,
    /// Weight for semantic corrections (0.0 - 1.0)
    pub semantic_weight: f64,
    /// Minimum confidence to include in results
    pub min_confidence: f64,
    /// Maximum total candidates to return
    pub max_candidates: usize,
    /// Whether to deduplicate similar corrections
    pub deduplicate: bool,
    /// Similarity threshold for deduplication
    pub dedup_threshold: f64,
    /// Whether to boost confidence when multiple sources agree
    pub agreement_boost: bool,
    /// Boost factor when sources agree
    pub agreement_boost_factor: f64,
}

impl Default for EnsembleCorrectorConfig {
    fn default() -> Self {
        Self {
            lexical_weight: 0.4,
            grammar_weight: 0.35,
            semantic_weight: 0.25,
            min_confidence: 0.3,
            max_candidates: 10,
            deduplicate: true,
            dedup_threshold: 0.9,
            agreement_boost: true,
            agreement_boost_factor: 1.3,
        }
    }
}

/// Ensemble corrector combining multiple correction sources.
///
/// This corrector aggregates suggestions from lexical, grammar, and semantic
/// correctors, applying configurable weights and deduplication to produce
/// a unified ranked list of corrections.
pub struct EnsembleCorrector<L: CodeLanguage> {
    language: Arc<L>,
    config: EnsembleCorrectorConfig,
    lexical: Option<LexicalCorrector<L>>,
    grammar: Option<GrammarCorrector<L>>,
    semantic: Option<SemanticCorrector<L>>,
}

impl<L: CodeLanguage + Clone> EnsembleCorrector<L> {
    /// Creates a new ensemble corrector with all components.
    pub fn new(
        language: Arc<L>,
        grammar: Option<WeightedCFG>,
        config: EnsembleCorrectorConfig,
    ) -> Self {
        let lexical = Some(LexicalCorrector::with_defaults(Arc::clone(&language)));
        let grammar_corrector =
            grammar.map(|g| GrammarCorrector::with_defaults(Arc::clone(&language), g));
        let semantic = Some(SemanticCorrector::with_defaults(Arc::clone(&language)));

        Self {
            language,
            config,
            lexical,
            grammar: grammar_corrector,
            semantic,
        }
    }

    /// Creates an ensemble corrector with default configuration.
    pub fn with_defaults(language: Arc<L>, grammar: Option<WeightedCFG>) -> Self {
        Self::new(language, grammar, EnsembleCorrectorConfig::default())
    }

    /// Creates a minimal ensemble with only lexical correction.
    pub fn lexical_only(language: Arc<L>) -> Self {
        Self {
            language: Arc::clone(&language),
            config: EnsembleCorrectorConfig::default(),
            lexical: Some(LexicalCorrector::with_defaults(language)),
            grammar: None,
            semantic: None,
        }
    }

    /// Returns a mutable reference to the lexical corrector.
    pub fn lexical_mut(&mut self) -> Option<&mut LexicalCorrector<L>> {
        self.lexical.as_mut()
    }

    /// Returns a mutable reference to the semantic corrector.
    pub fn semantic_mut(&mut self) -> Option<&mut SemanticCorrector<L>> {
        self.semantic.as_mut()
    }

    /// Adds identifiers to the lexical corrector.
    pub fn add_identifiers(&mut self, identifiers: &[&str]) {
        if let Some(ref mut lexical) = self.lexical {
            for id in identifiers {
                lexical.add_identifier(id);
            }
        }
    }

    /// Registers variables with the semantic corrector.
    pub fn register_variables(&mut self, variables: &[(String, Option<String>)]) {
        if let Some(ref mut semantic) = self.semantic {
            for (name, type_name) in variables {
                semantic.register_variable(name.clone(), type_name.clone(), 0);
            }
        }
    }

    /// Collects corrections from all sources for a token.
    fn collect_corrections(
        &self,
        token: &CodeToken,
        context: &TokenContext,
    ) -> Vec<(Correction, f64)> {
        let mut corrections = Vec::new();

        // Lexical corrections
        if let Some(ref lexical) = self.lexical {
            for c in lexical.correct_token(token, context) {
                corrections.push((c, self.config.lexical_weight));
            }
        }

        // Grammar corrections
        if let Some(ref grammar) = self.grammar {
            for c in grammar.correct_token(token, context) {
                corrections.push((c, self.config.grammar_weight));
            }
        }

        // Semantic corrections
        if let Some(ref semantic) = self.semantic {
            for c in semantic.correct_token(token, context) {
                corrections.push((c, self.config.semantic_weight));
            }
        }

        corrections
    }

    /// Applies source weighting to correction confidence.
    fn apply_weight(correction: &Correction, weight: f64) -> Correction {
        let mut c = correction.clone();
        c.confidence *= weight;
        c
    }

    /// Merges similar corrections, boosting confidence when sources agree.
    fn merge_corrections(&self, corrections: Vec<(Correction, f64)>) -> Vec<Correction> {
        if corrections.is_empty() {
            return vec![];
        }

        if !self.config.deduplicate {
            return corrections
                .into_iter()
                .map(|(c, weight)| Self::apply_weight(&c, weight))
                .collect();
        }

        // Group by (replacement, position). Pre-size both collections to
        // `corrections.len()` — the worst case is one group per correction,
        // and that's the only size knowable up front. Eliminates mid-loop
        // resize doubling in the grouping pass.
        let n_in = corrections.len();
        let mut groups: HashMap<(String, usize, usize), Vec<(Correction, f64)>> =
            HashMap::with_capacity(n_in);

        for (c, weight) in corrections {
            let key = (c.replacement.clone(), c.start_byte, c.end_byte);
            groups.entry(key).or_default().push((c, weight));
        }

        let mut merged = Vec::with_capacity(groups.len());

        for ((_replacement, _start_byte, _end_byte), group) in groups {
            if group.len() == 1 {
                // Single source
                let (c, weight) = group.into_iter().next().unwrap();
                merged.push(Self::apply_weight(&c, weight));
            } else {
                // Multiple sources agree - merge and boost
                let sources: Vec<CorrectionSource> = group.iter().map(|(c, _)| c.source).collect();
                let total_weight: f64 = group.iter().map(|(_, w)| w).sum();
                let avg_confidence: f64 =
                    group.iter().map(|(c, w)| c.confidence * w).sum::<f64>() / total_weight;

                // Take the best correction as base. NaN confidences are
                // treated as Equal so a single bad score can't panic the
                // ensemble. The outer `.unwrap().0` is safe because `group`
                // was built from a non-empty iterator above; we surface
                // the invariant via `.expect()` to make it grep-able.
                let mut best = group
                    .into_iter()
                    .max_by(|a, b| {
                        (a.0.confidence * a.1)
                            .partial_cmp(&(b.0.confidence * b.1))
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .expect("ensemble group is non-empty by construction")
                    .0;

                // Apply agreement boost
                let boost = if self.config.agreement_boost && sources.len() > 1 {
                    self.config.agreement_boost_factor
                } else {
                    1.0
                };

                best.confidence = (avg_confidence * boost).min(1.0);
                best.source = CorrectionSource::Combined;
                best.context = Some(format!("Suggested by {} sources", sources.len()));

                merged.push(best);
            }
        }

        merged
    }

    /// Filters and ranks the final corrections.
    fn finalize_corrections(&self, mut corrections: Vec<Correction>) -> Vec<Correction> {
        // Filter by minimum confidence
        corrections.retain(|c| c.confidence >= self.config.min_confidence);

        // Sort by confidence descending
        corrections.sort_by(|a, b| {
            b.confidence
                .partial_cmp(&a.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Truncate to max candidates
        corrections.truncate(self.config.max_candidates);

        corrections
    }

    /// Performs full analysis on parsed code with CPG.
    pub fn analyze_full(&self, parsed: &ParsedCode, cpg: &CodePropertyGraph) -> Vec<Correction> {
        let mut all_corrections = Vec::new();

        // Get semantic corrections from full analysis
        if let Some(ref semantic) = self.semantic {
            let semantic_corrections = semantic.analyze_parsed(parsed, cpg);
            for c in semantic_corrections {
                all_corrections.push((c, self.config.semantic_weight));
            }
        }

        // Merge and finalize
        let merged = self.merge_corrections(all_corrections);
        self.finalize_corrections(merged)
    }

    /// Returns the configuration.
    pub fn config(&self) -> &EnsembleCorrectorConfig {
        &self.config
    }

    /// Returns the language handler.
    pub fn language(&self) -> &L {
        &self.language
    }
}

impl<L: CodeLanguage + Clone + Send + Sync> CodeCorrector for EnsembleCorrector<L> {
    fn correct_token(&self, token: &CodeToken, context: &TokenContext) -> Vec<Correction> {
        let corrections = self.collect_corrections(token, context);
        let merged = self.merge_corrections(corrections);
        self.finalize_corrections(merged)
    }

    fn correct_range(&self, source: &str, start_byte: usize, end_byte: usize) -> Vec<Correction> {
        let text = &source[start_byte..end_byte];
        let token = CodeToken::new(
            text,
            start_byte,
            0,
            0,
            crate::code::language::TokenType::Unknown,
            "unknown",
        );

        let context = TokenContext::new(crate::code::language::TokenType::Unknown);
        self.correct_token(&token, &context)
    }

    fn max_edit_distance(&self) -> usize {
        // Return max of all correctors
        let mut max = 2;
        if let Some(ref lexical) = self.lexical {
            max = max.max(lexical.max_edit_distance());
        }
        if let Some(ref grammar) = self.grammar {
            max = max.max(grammar.max_edit_distance());
        }
        if let Some(ref semantic) = self.semantic {
            max = max.max(semantic.max_edit_distance());
        }
        max
    }

    fn name(&self) -> &str {
        "EnsembleCorrector"
    }
}

/// Builder for creating EnsembleCorrector with custom configuration.
pub struct EnsembleCorrectorBuilder<L: CodeLanguage> {
    language: Arc<L>,
    config: EnsembleCorrectorConfig,
    grammar: Option<WeightedCFG>,
    enable_lexical: bool,
    enable_grammar: bool,
    enable_semantic: bool,
}

impl<L: CodeLanguage + Clone> EnsembleCorrectorBuilder<L> {
    /// Creates a new builder.
    pub fn new(language: Arc<L>) -> Self {
        Self {
            language,
            config: EnsembleCorrectorConfig::default(),
            grammar: None,
            enable_lexical: true,
            enable_grammar: true,
            enable_semantic: true,
        }
    }

    /// Sets the grammar for grammar-based correction.
    pub fn with_grammar(mut self, grammar: WeightedCFG) -> Self {
        self.grammar = Some(grammar);
        self
    }

    /// Sets custom configuration.
    pub fn with_config(mut self, config: EnsembleCorrectorConfig) -> Self {
        self.config = config;
        self
    }

    /// Disables lexical correction.
    pub fn without_lexical(mut self) -> Self {
        self.enable_lexical = false;
        self
    }

    /// Disables grammar correction.
    pub fn without_grammar(mut self) -> Self {
        self.enable_grammar = false;
        self
    }

    /// Disables semantic correction.
    pub fn without_semantic(mut self) -> Self {
        self.enable_semantic = false;
        self
    }

    /// Sets the weight for lexical corrections.
    pub fn lexical_weight(mut self, weight: f64) -> Self {
        self.config.lexical_weight = weight;
        self
    }

    /// Sets the weight for grammar corrections.
    pub fn grammar_weight(mut self, weight: f64) -> Self {
        self.config.grammar_weight = weight;
        self
    }

    /// Sets the weight for semantic corrections.
    pub fn semantic_weight(mut self, weight: f64) -> Self {
        self.config.semantic_weight = weight;
        self
    }

    /// Builds the ensemble corrector.
    pub fn build(self) -> EnsembleCorrector<L> {
        let lexical = if self.enable_lexical {
            Some(LexicalCorrector::with_defaults(Arc::clone(&self.language)))
        } else {
            None
        };

        let grammar = if self.enable_grammar {
            self.grammar
                .map(|g| GrammarCorrector::with_defaults(Arc::clone(&self.language), g))
        } else {
            None
        };

        let semantic = if self.enable_semantic {
            Some(SemanticCorrector::with_defaults(Arc::clone(&self.language)))
        } else {
            None
        };

        EnsembleCorrector {
            language: self.language,
            config: self.config,
            lexical,
            grammar,
            semantic,
        }
    }
}

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

    // Mock language for testing
    #[derive(Debug, Clone, Default)]
    struct MockLanguage;

    impl CodeLanguage for MockLanguage {
        fn name(&self) -> &str {
            "mock"
        }
        fn display_name(&self) -> &str {
            "Mock"
        }
        fn tree_sitter_language(&self) -> tree_sitter::Language {
            tree_sitter_rust::LANGUAGE.into()
        }
        fn keywords(&self) -> &[&str] {
            &["if", "else", "while", "for", "return", "function"]
        }
        fn special_tokens(&self) -> &[&str] {
            &[]
        }
        fn file_extensions(&self) -> &[&str] {
            &["mock"]
        }
        fn classify_token(
            &self,
            _token: &str,
            _node_kind: &str,
        ) -> crate::code::language::TokenType {
            crate::code::language::TokenType::Unknown
        }
        fn is_valid_identifier(&self, s: &str) -> bool {
            !s.is_empty() && s.chars().next().map(|c| c.is_alphabetic()).unwrap_or(false)
        }
        fn builtin_types(&self) -> &[&str] {
            &["int", "string", "bool"]
        }
        fn stdlib_functions(&self) -> &[&str] {
            &["print", "read"]
        }
        fn comment_syntax(&self) -> crate::code::language::CommentSyntax {
            crate::code::language::CommentSyntax::default()
        }
        fn is_whitespace_significant(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_ensemble_corrector_creation() {
        let lang = Arc::new(MockLanguage);
        let corrector = EnsembleCorrector::with_defaults(lang, None);

        assert!(corrector.lexical.is_some());
        assert!(corrector.grammar.is_none()); // No grammar provided
        assert!(corrector.semantic.is_some());
    }

    #[test]
    fn test_ensemble_builder() {
        let lang = Arc::new(MockLanguage);
        let corrector = EnsembleCorrectorBuilder::new(lang)
            .without_semantic()
            .lexical_weight(0.6)
            .build();

        assert!(corrector.lexical.is_some());
        assert!(corrector.semantic.is_none());
        assert!((corrector.config.lexical_weight - 0.6).abs() < 0.01);
    }

    #[test]
    fn test_ensemble_correction() {
        let lang = Arc::new(MockLanguage);
        let mut corrector = EnsembleCorrector::lexical_only(Arc::clone(&lang));

        // Add some identifiers
        corrector.add_identifiers(&["calculateTotal", "processData"]);

        let token = CodeToken::new(
            "funtion", // Misspelled "function"
            0,
            1,
            0,
            crate::code::language::TokenType::Keyword,
            "keyword",
        );

        let context = TokenContext::new(crate::code::language::TokenType::Keyword);
        let corrections = corrector.correct_token(&token, &context);

        // Should get corrections for the misspelled keyword
        assert!(!corrections.is_empty());
    }

    #[test]
    fn test_agreement_boost() {
        let config = EnsembleCorrectorConfig {
            agreement_boost: true,
            agreement_boost_factor: 1.5,
            ..Default::default()
        };

        // Corrections from multiple sources should be boosted
        assert!(config.agreement_boost);
        assert!((config.agreement_boost_factor - 1.5).abs() < 0.01);
    }
}