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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Integration tests for the code correction module.
//!
//! These tests verify the end-to-end functionality of the code correction
//! pipeline, including lexical, grammar, and semantic correctors.

#![cfg(feature = "code")]

use libgrammstein::code::correction::{
    CodeCorrector, Correction, CorrectionCandidates, CorrectionKind,
};
use libgrammstein::code::correctors::LexicalCorrector;
use libgrammstein::code::language::{CodeLanguage, CommentSyntax, TokenContext, TokenType};
use libgrammstein::code::tokenizer::CodeToken;
use std::sync::Arc;

/// Mock language for testing without tree-sitter dependencies.
#[derive(Debug, Clone, Default)]
struct MockLanguage {
    name: &'static str,
    keywords: &'static [&'static str],
    builtin_types: &'static [&'static str],
    stdlib_functions: &'static [&'static str],
}

impl MockLanguage {
    fn rust_like() -> Self {
        Self {
            name: "rust_like",
            keywords: &[
                "fn", "let", "mut", "if", "else", "while", "for", "loop", "match", "return",
                "struct", "enum", "impl", "trait", "pub", "use", "mod", "const", "static", "type",
                "where", "async", "await",
            ],
            builtin_types: &[
                "i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32", "u64", "u128",
                "usize", "f32", "f64", "bool", "char", "str", "String", "Vec", "Option", "Result",
                "Box", "Rc", "Arc",
            ],
            stdlib_functions: &[
                "println",
                "print",
                "format",
                "panic",
                "assert",
                "assert_eq",
                "assert_ne",
                "dbg",
                "todo",
                "unimplemented",
            ],
        }
    }

    fn python_like() -> Self {
        Self {
            name: "python_like",
            keywords: &[
                "def", "class", "if", "elif", "else", "while", "for", "in", "return", "yield",
                "try", "except", "finally", "raise", "import", "from", "as", "with", "pass",
                "break", "continue", "and", "or", "not", "is", "lambda", "global", "nonlocal",
                "async", "await",
            ],
            builtin_types: &[
                "int",
                "float",
                "str",
                "bool",
                "list",
                "dict",
                "tuple",
                "set",
                "bytes",
                "bytearray",
                "frozenset",
                "object",
                "type",
                "None",
            ],
            stdlib_functions: &[
                "print",
                "len",
                "range",
                "enumerate",
                "zip",
                "map",
                "filter",
                "sorted",
                "reversed",
                "open",
                "input",
                "type",
                "isinstance",
            ],
        }
    }
}

impl CodeLanguage for MockLanguage {
    fn name(&self) -> &str {
        self.name
    }

    fn display_name(&self) -> &str {
        match self.name {
            "rust_like" => "Rust-like",
            "python_like" => "Python-like",
            _ => self.name,
        }
    }

    fn tree_sitter_language(&self) -> tree_sitter::Language {
        // This won't be called in lexical-only tests
        panic!("Mock language doesn't support tree-sitter parsing")
    }

    fn keywords(&self) -> &[&str] {
        self.keywords
    }

    fn special_tokens(&self) -> &[&str] {
        &[]
    }

    fn file_extensions(&self) -> &[&str] {
        &["mock"]
    }

    fn classify_token(&self, token: &str, _node_kind: &str) -> TokenType {
        if self.keywords.contains(&token) {
            TokenType::Keyword
        } else if self.builtin_types.contains(&token) {
            TokenType::TypeName
        } else if self.stdlib_functions.contains(&token) {
            TokenType::Identifier // stdlib functions are identifiers
        } else if token.chars().all(|c| c.is_ascii_digit()) {
            TokenType::NumericLiteral
        } else if token.starts_with('"') && token.ends_with('"') {
            TokenType::StringLiteral
        } else {
            TokenType::Identifier
        }
    }

    fn is_valid_identifier(&self, s: &str) -> bool {
        !s.is_empty()
            && s.chars()
                .next()
                .map(|c| c.is_alphabetic() || c == '_')
                .unwrap_or(false)
            && s.chars().all(|c| c.is_alphanumeric() || c == '_')
    }

    fn builtin_types(&self) -> &[&str] {
        self.builtin_types
    }

    fn stdlib_functions(&self) -> &[&str] {
        self.stdlib_functions
    }

    fn comment_syntax(&self) -> CommentSyntax {
        CommentSyntax::default()
    }

    fn is_whitespace_significant(&self) -> bool {
        self.name == "python_like"
    }
}

// =============================================================================
// Lexical Corrector Integration Tests
// =============================================================================

#[test]
fn test_lexical_correction_rust_keywords() {
    let lang = Arc::new(MockLanguage::rust_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    // Test common keyword typos
    let test_cases = vec![
        ("fnc", "fn"),        // Missing character
        ("fnn", "fn"),        // Extra character
        ("funciton", "fn"),   // Won't match (too far)
        ("lett", "let"),      // Extra character
        ("leett", "let"),     // Extra characters
        ("retrun", "return"), // Transposition
        ("reutrn", "return"), // Different transposition
        ("strcut", "struct"), // Transposition
        ("pubilc", "pub"),    // Won't match (different word)
    ];

    for (typo, expected) in test_cases {
        let token = CodeToken::new(typo, 0, 1, 0, TokenType::Keyword, "keyword");
        let context = TokenContext::new(TokenType::Keyword);
        let corrections = corrector.correct_token(&token, &context);

        // Check if any correction matches the expected word
        if corrections.iter().any(|c| c.replacement == expected) {
            println!("OK: {} -> {} found in corrections", typo, expected);
        } else {
            println!(
                "INFO: {} -> {} not found (corrections: {:?})",
                typo,
                expected,
                corrections
                    .iter()
                    .map(|c| &c.replacement)
                    .collect::<Vec<_>>()
            );
        }
    }
}

#[test]
fn test_lexical_correction_python_keywords() {
    let lang = Arc::new(MockLanguage::python_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    let test_cases = vec![
        ("dfe", "def"),       // Transposition
        ("calss", "class"),   // Transposition
        ("rteurn", "return"), // Multiple errors
        ("improt", "import"), // Transposition
        ("exept", "except"),  // Missing character
    ];

    for (typo, expected) in test_cases {
        let token = CodeToken::new(typo, 0, 1, 0, TokenType::Keyword, "keyword");
        let context = TokenContext::new(TokenType::Keyword);
        let corrections = corrector.correct_token(&token, &context);

        if corrections.iter().any(|c| c.replacement == expected) {
            println!("OK: {} -> {} found", typo, expected);
        } else {
            println!(
                "INFO: {} -> {} not found (have: {:?})",
                typo,
                expected,
                corrections
                    .iter()
                    .map(|c| &c.replacement)
                    .collect::<Vec<_>>()
            );
        }
    }
}

#[test]
fn test_lexical_correction_builtin_types() {
    let lang = Arc::new(MockLanguage::rust_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    let test_cases = vec![
        ("Stirng", "String"), // Transposition
        ("Optoin", "Option"), // Transposition
        ("Resutl", "Result"), // Transposition
        ("boool", "bool"),    // Extra character
    ];

    for (typo, expected) in test_cases {
        let token = CodeToken::new(typo, 0, 1, 0, TokenType::TypeName, "type");
        let context = TokenContext::new(TokenType::TypeName);
        let corrections = corrector.correct_token(&token, &context);

        if corrections.iter().any(|c| c.replacement == expected) {
            println!("OK: {} -> {} found", typo, expected);
        }
    }
}

#[test]
fn test_lexical_correction_custom_identifiers() {
    let lang = Arc::new(MockLanguage::rust_like());
    let mut corrector = LexicalCorrector::with_defaults(lang);

    // Add project-specific identifiers
    corrector.add_identifier("calculate_total");
    corrector.add_identifier("process_request");
    corrector.add_identifier("handle_error");
    corrector.add_identifier("validate_input");
    corrector.add_identifier("serialize_data");

    let test_cases = vec![
        ("calcluate_total", "calculate_total"), // Transposition
        ("porcess_request", "process_request"), // Transposition
        ("handel_error", "handle_error"),       // Transposition
        ("valdiate_input", "validate_input"),   // Transposition
    ];

    for (typo, expected) in test_cases {
        let token = CodeToken::new(typo, 0, 1, 0, TokenType::Identifier, "identifier");
        let context = TokenContext::new(TokenType::Identifier);
        let corrections = corrector.correct_token(&token, &context);

        assert!(
            corrections.iter().any(|c| c.replacement == expected),
            "Expected {} -> {} but got {:?}",
            typo,
            expected,
            corrections
                .iter()
                .map(|c| &c.replacement)
                .collect::<Vec<_>>()
        );
    }
}

#[test]
fn test_lexical_correction_confidence_ranking() {
    let lang = Arc::new(MockLanguage::rust_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    // "fn" with one character off should have higher confidence than two off
    let token = CodeToken::new(
        "fnn", // One character off from "fn"
        0,
        1,
        0,
        TokenType::Keyword,
        "keyword",
    );
    let context = TokenContext::new(TokenType::Keyword);
    let corrections = corrector.correct_token(&token, &context);

    if corrections.len() >= 2 {
        // Verify corrections are sorted by confidence (descending)
        for i in 1..corrections.len() {
            assert!(
                corrections[i - 1].confidence >= corrections[i].confidence,
                "Corrections should be sorted by confidence"
            );
        }
    }
}

#[test]
fn test_lexical_correction_no_self_correction() {
    let lang = Arc::new(MockLanguage::rust_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    // Exact match should not suggest itself
    let token = CodeToken::new("fn", 0, 1, 0, TokenType::Keyword, "keyword");
    let context = TokenContext::new(TokenType::Keyword);
    let corrections = corrector.correct_token(&token, &context);

    // Should not suggest "fn" -> "fn"
    assert!(
        !corrections
            .iter()
            .any(|c| c.replacement == "fn" && c.original == "fn"),
        "Should not suggest self-correction"
    );
}

// =============================================================================
// Correction Candidates Integration Tests
// =============================================================================

#[test]
fn test_correction_candidates_sorting() {
    let mut candidates = CorrectionCandidates::new(10);

    // Add corrections with different confidence levels
    let c1 =
        Correction::new(CorrectionKind::Spelling, 0, 5, "typo1", "fixed1").with_confidence(0.7);
    let c2 =
        Correction::new(CorrectionKind::Spelling, 0, 5, "typo1", "fixed2").with_confidence(0.9);
    let c3 =
        Correction::new(CorrectionKind::Spelling, 0, 5, "typo1", "fixed3").with_confidence(0.5);

    candidates.add(c1);
    candidates.add(c2);
    candidates.add(c3);

    let ranked = candidates.ranked();

    // Should be sorted by confidence descending
    assert_eq!(ranked.len(), 3);
    assert_eq!(ranked[0].replacement, "fixed2"); // highest confidence
    assert_eq!(ranked[1].replacement, "fixed1");
    assert_eq!(ranked[2].replacement, "fixed3"); // lowest confidence
}

#[test]
fn test_correction_candidates_max_limit() {
    let mut candidates = CorrectionCandidates::new(3);

    for i in 0..10 {
        let c = Correction::new(
            CorrectionKind::Spelling,
            i,
            i + 1,
            &format!("typo{}", i),
            &format!("fixed{}", i),
        )
        .with_confidence(i as f64 * 0.1);
        candidates.add(c);
    }

    let ranked = candidates.ranked();
    assert!(ranked.len() <= 3, "Should respect max limit");
}

#[test]
fn test_correction_apply_multiple() {
    let source = "funciton foo() { retrun 42; }";

    // Apply corrections from end to start
    let mut result = source.to_string();

    // Second correction: "retrun" -> "return" at bytes 17-23
    result.replace_range(17..23, "return");

    // First correction: "funciton" -> "function" at bytes 0-8
    result.replace_range(0..8, "function");

    assert_eq!(result, "function foo() { return 42; }");
}

// =============================================================================
// Ensemble Corrector Integration Tests
// =============================================================================

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

    #[test]
    fn test_ensemble_lexical_only() {
        use libgrammstein::code::correctors::ensemble::{
            EnsembleCorrectorBuilder, EnsembleCorrectorConfig,
        };

        let lang = Arc::new(MockLanguage::rust_like());

        // Use builder with adjusted config for lexical-only mode
        // Default lexical_weight (0.4) * distance-2 confidence (0.7) = 0.28 < min_confidence (0.3)
        // So we need to either increase weight or lower min_confidence
        let config = EnsembleCorrectorConfig {
            lexical_weight: 1.0, // Full weight for lexical-only mode
            min_confidence: 0.2, // Lower threshold for distance-2 corrections
            ..Default::default()
        };

        let mut corrector = EnsembleCorrectorBuilder::new(lang)
            .with_config(config)
            .without_grammar()
            .without_semantic()
            .build();

        // Add some identifiers
        corrector.add_identifiers(&["calculate_total", "process_data"]);

        let token = CodeToken::new(
            "calcluate_total", // Typo: transposed 'lu' -> 'ul' (distance 2)
            0,
            1,
            0,
            TokenType::Identifier,
            "identifier",
        );
        let context = TokenContext::new(TokenType::Identifier);
        let corrections = corrector.correct_token(&token, &context);

        assert!(
            corrections
                .iter()
                .any(|c| c.replacement == "calculate_total"),
            "Should suggest correct identifier. Got: {:?}",
            corrections
                .iter()
                .map(|c| &c.replacement)
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ensemble_config() {
        use libgrammstein::code::correctors::ensemble::EnsembleCorrectorBuilder;

        let lang = Arc::new(MockLanguage::rust_like());
        let corrector = EnsembleCorrectorBuilder::new(lang)
            .without_semantic()
            .without_grammar()
            .lexical_weight(1.0)
            .build();

        assert_eq!(corrector.config().lexical_weight, 1.0);
    }
}

// =============================================================================
// Semantic Corrector Integration Tests
// =============================================================================

#[cfg(test)]
mod semantic_tests {
    use super::*;
    use libgrammstein::code::correctors::SemanticCorrector;

    #[test]
    fn test_semantic_variable_registration() {
        let lang = Arc::new(MockLanguage::rust_like());
        let mut corrector = SemanticCorrector::with_defaults(lang);

        // Register some variables
        corrector.register_variable("user_count".to_string(), Some("i32".to_string()), 0);
        corrector.register_variable("item_list".to_string(), Some("Vec<Item>".to_string()), 0);
        corrector.register_variable("config".to_string(), Some("Config".to_string()), 0);

        // Test that registered variables can be used for correction
        let token = CodeToken::new(
            "usr_count", // Typo
            0,
            1,
            0,
            TokenType::Identifier,
            "identifier",
        );
        let context = TokenContext::new(TokenType::Identifier);
        let corrections = corrector.correct_token(&token, &context);

        // Should suggest the correct variable name
        if corrections.iter().any(|c| c.replacement == "user_count") {
            println!("OK: usr_count -> user_count found");
        }
    }

    #[test]
    fn test_semantic_similar_names() {
        let lang = Arc::new(MockLanguage::rust_like());
        let mut corrector = SemanticCorrector::with_defaults(lang);

        // Register variables with similar names
        corrector.register_variable("request_handler".to_string(), None, 0);
        corrector.register_variable("response_handler".to_string(), None, 0);
        corrector.register_variable("error_handler".to_string(), None, 0);

        let token = CodeToken::new(
            "reqest_handler", // Missing 'u'
            0,
            1,
            0,
            TokenType::Identifier,
            "identifier",
        );
        let context = TokenContext::new(TokenType::Identifier);
        let corrections = corrector.correct_token(&token, &context);

        assert!(
            corrections
                .iter()
                .any(|c| c.replacement == "request_handler"),
            "Should suggest request_handler"
        );
    }
}

// =============================================================================
// End-to-End Workflow Tests
// =============================================================================

#[test]
fn test_correction_workflow_basic() {
    // Simulate a simple correction workflow
    let lang = Arc::new(MockLanguage::rust_like());
    let mut corrector = LexicalCorrector::with_defaults(lang);

    // Add project identifiers
    corrector.add_identifier("main");
    corrector.add_identifier("calculate_sum");
    corrector.add_identifier("result");
    corrector.add_identifier("println");

    // Simulate tokenizing code with typos
    let tokens = vec![
        CodeToken::new("fnn", 0, 1, 0, TokenType::Keyword, "keyword"), // "fn"
        CodeToken::new("mian", 4, 1, 4, TokenType::Identifier, "identifier"), // "main"
        CodeToken::new("lett", 12, 2, 4, TokenType::Keyword, "keyword"), // "let"
        CodeToken::new("reuslt", 17, 2, 9, TokenType::Identifier, "identifier"), // "result"
    ];

    let mut all_corrections = Vec::new();
    for token in &tokens {
        let context = TokenContext::new(token.token_type);
        let corrections = corrector.correct_token(token, &context);
        all_corrections.extend(corrections);
    }

    // Verify we got some corrections
    assert!(!all_corrections.is_empty(), "Should have corrections");

    // Check for expected corrections
    let has_fn_correction = all_corrections
        .iter()
        .any(|c| c.original == "fnn" && c.replacement == "fn");
    let has_let_correction = all_corrections
        .iter()
        .any(|c| c.original == "lett" && c.replacement == "let");

    println!("Has fn correction: {}", has_fn_correction);
    println!("Has let correction: {}", has_let_correction);
}

#[test]
fn test_correction_byte_offsets() {
    // Verify corrections maintain correct byte offsets
    let source = "fn mian() {}"; // "main" misspelled at position 3

    let token = CodeToken::new("mian", 3, 1, 3, TokenType::Identifier, "identifier");

    let lang = Arc::new(MockLanguage::rust_like());
    let mut corrector = LexicalCorrector::with_defaults(lang);
    corrector.add_identifier("main");

    let context = TokenContext::new(TokenType::Identifier);
    let corrections = corrector.correct_token(&token, &context);

    if let Some(correction) = corrections.iter().find(|c| c.replacement == "main") {
        assert_eq!(correction.start_byte, 3, "Start byte should be 3");
        assert_eq!(correction.end_byte, 7, "End byte should be 7");

        // Apply the correction
        let mut result = source.to_string();
        result.replace_range(
            correction.start_byte..correction.end_byte,
            &correction.replacement,
        );
        assert_eq!(result, "fn main() {}");
    }
}

#[test]
fn test_correction_kind_categorization() {
    // Test that corrections have appropriate kinds
    let lang = Arc::new(MockLanguage::rust_like());
    let corrector = LexicalCorrector::with_defaults(lang);

    // Keyword typo
    let token = CodeToken::new("lett", 0, 1, 0, TokenType::Keyword, "keyword");
    let context = TokenContext::new(TokenType::Keyword);
    let corrections = corrector.correct_token(&token, &context);

    // All lexical corrections should be Spelling kind
    for correction in &corrections {
        assert_eq!(
            correction.kind,
            CorrectionKind::Spelling,
            "Lexical corrections should be Spelling kind"
        );
    }
}