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
# Correction Pipeline

The correction pipeline provides an end-to-end workflow for analyzing and correcting source code, integrating parsing, tokenization, semantic analysis, and multi-source correction.

## Overview

The `CorrectionPipeline` orchestrates:

1. **Parse**: Tree-sitter parsing with error recovery
2. **Tokenize**: Extract tokens with context information
3. **Analyze**: Build CPG for semantic analysis (optional)
4. **Correct**: Apply ensemble of correctors
5. **Rank**: Combine and rank correction candidates

## Architecture

```
┌──────────────────────────────────────────────────────────────────────┐
│                       CorrectionPipeline                             │
│                                                                      │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                          Input: Source Code                     │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │ Phase 1: Parse                                                  │ │
│  │ ┌──────────────────────────────────────────────────────────┐   │ │
│  │ │ CodeParser (Tree-sitter) → ParsedCode + Error Regions    │   │ │
│  │ └──────────────────────────────────────────────────────────┘   │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │ Phase 2: Tokenize                                               │ │
│  │ ┌──────────────────────────────────────────────────────────┐   │ │
│  │ │ CodeTokenizer → Tokens with Context                       │   │ │
│  │ └──────────────────────────────────────────────────────────┘   │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │ Phase 3: Analyze (optional)                                     │ │
│  │ ┌──────────────────────────────────────────────────────────┐   │ │
│  │ │ CodePropertyGraph (AST + CFG + DFG)                       │   │ │
│  │ └──────────────────────────────────────────────────────────┘   │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │ Phase 4: Correct                                                │ │
│  │ ┌──────────────────────────────────────────────────────────┐   │ │
│  │ │ EnsembleCorrector (Lexical + Grammar + Semantic)          │   │ │
│  │ └──────────────────────────────────────────────────────────┘   │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │ Phase 5: Rank & Filter                                          │ │
│  │ ┌──────────────────────────────────────────────────────────┐   │ │
│  │ │ Deduplicate → Sort by confidence → Truncate              │   │ │
│  │ └──────────────────────────────────────────────────────────┘   │ │
│  └───────────────────────────────┬────────────────────────────────┘ │
│                                  │                                   │
│                                  ▼                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                    Output: AnalysisResult                       │ │
│  │  • source       • corrections   • diagnostics                   │ │
│  │  • tokens       • error_count   • has_parse_errors              │ │
│  └────────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────────┘
```

## PipelineConfig

Configuration options for the correction pipeline:

```rust
pub struct PipelineConfig {
    /// Maximum corrections to return per file (default: 50)
    pub max_corrections: usize,
    /// Minimum confidence threshold (default: 0.3)
    pub min_confidence: f64,
    /// Whether to include diagnostic messages (default: true)
    pub include_diagnostics: bool,
    /// Threshold for auto-applying fixes (default: None)
    pub auto_apply_threshold: Option<f64>,
    /// Whether to do full semantic analysis (default: true)
    pub full_semantic_analysis: bool,
}
```

### Configuration Parameters

| Parameter | Default | Description |
|-----------|---------|-------------|
| `max_corrections` | 50 | Maximum suggestions per file |
| `min_confidence` | 0.3 | Filter low-confidence corrections |
| `include_diagnostics` | true | Generate diagnostic messages |
| `auto_apply_threshold` | None | Auto-apply above this confidence |
| `full_semantic_analysis` | true | Build CPG for semantic checks |

## Creating a Pipeline

### Basic Creation

```rust
use libgrammstein::code::{CorrectionPipeline, PipelineConfig, Python};
use std::sync::Arc;

let python = Arc::new(Python::new());

// With default configuration
let pipeline = CorrectionPipeline::with_defaults(python.clone(), None)?;

// With grammar for syntax checking
let grammar = build_python_grammar();
let pipeline = CorrectionPipeline::with_defaults(python, Some(grammar))?;
```

### Custom Configuration

```rust
let config = PipelineConfig {
    max_corrections: 20,
    min_confidence: 0.5,
    include_diagnostics: true,
    auto_apply_threshold: Some(0.9),  // Auto-apply very confident fixes
    full_semantic_analysis: true,
};

let pipeline = CorrectionPipeline::new(python, Some(grammar), config)?;
```

### Minimal Pipeline

For fast analysis without semantic checks:

```rust
// Lexical-only, no CPG construction
let pipeline = CorrectionPipeline::minimal(python)?;
```

## AnalysisResult

The result of analyzing source code:

```rust
pub struct AnalysisResult {
    /// Original source code
    pub source: String,
    /// Whether parsing produced any errors
    pub has_parse_errors: bool,
    /// Number of parse errors found
    pub error_count: usize,
    /// Tokens extracted from source
    pub tokens: Vec<CodeToken>,
    /// Ranked corrections
    pub corrections: CorrectionCandidates,
    /// Diagnostic messages
    pub diagnostics: Vec<Diagnostic>,
}
```

### Accessing Results

```rust
let result = pipeline.analyze(source)?;

// Check for parse errors
if result.has_parse_errors {
    println!("Found {} parse errors", result.error_count);
}

// Get best correction
if let Some(best) = result.corrections.best() {
    println!("Top suggestion: {} → {} ({:.0}%)",
        best.original, best.replacement, best.confidence * 100.0);
}

// Iterate all corrections
for correction in result.corrections.ranked() {
    println!("  {} → {} ({:.2})",
        correction.original, correction.replacement, correction.confidence);
}

// Access diagnostics
for diagnostic in &result.diagnostics {
    println!("[{:?}] Line {}: {}",
        diagnostic.severity, diagnostic.line + 1, diagnostic.message);
}
```

## Diagnostic

Diagnostic messages from analysis:

```rust
pub struct Diagnostic {
    /// Severity level
    pub severity: DiagnosticSeverity,
    /// Message text
    pub message: String,
    /// Start byte offset
    pub start_byte: usize,
    /// End byte offset
    pub end_byte: usize,
    /// Line number (0-indexed)
    pub line: usize,
    /// Column number (0-indexed)
    pub column: usize,
}
```

### DiagnosticSeverity

```rust
pub enum DiagnosticSeverity {
    Error,    // Prevents compilation/execution
    Warning,  // Potential issues
    Info,     // Informational
    Hint,     // Suggestions for improvement
}
```

### Diagnostic Examples

```rust
// Parse error diagnostic
Diagnostic {
    severity: DiagnosticSeverity::Error,
    message: "Syntax error: ERROR 'retrun'",
    start_byte: 20,
    end_byte: 26,
    line: 1,
    column: 4,
}

// Correction hint diagnostic
Diagnostic {
    severity: DiagnosticSeverity::Hint,
    message: "Consider: retrun -> return",
    start_byte: 20,
    end_byte: 26,
    line: 1,
    column: 4,
}
```

## Analyzing Code

### Basic Analysis

```rust
let mut pipeline = CorrectionPipeline::with_defaults(python, None)?;

let source = r#"
def calculate(x, y):
    retrun x + y
"#;

let result = pipeline.analyze(source)?;

println!("Parse errors: {}", result.error_count);
println!("Corrections available: {}", result.corrections.len());
```

### With Project Context

Add project-specific identifiers for better corrections:

```rust
let mut pipeline = CorrectionPipeline::with_defaults(python, None)?;

// Add project identifiers to the corrector
pipeline.corrector_mut().add_identifiers(&[
    "calculateTotal",
    "processUserData",
    "handleNetworkError",
]);

// Register known variables
pipeline.corrector_mut().register_variables(&[
    ("userCount".to_string(), Some("int".to_string())),
    ("userName".to_string(), Some("string".to_string())),
]);

let result = pipeline.analyze(source)?;
```

## Applying Corrections

### Apply All Corrections

```rust
let result = pipeline.analyze(source)?;

// Get all high-confidence corrections
let corrections: Vec<_> = result.corrections.ranked()
    .iter()
    .filter(|c| c.confidence >= 0.7)
    .cloned()
    .collect();

// Apply to source
let fixed_source = pipeline.apply_corrections(source, &corrections);
println!("Fixed:\n{}", fixed_source);
```

### Apply Best Correction Only

```rust
let result = pipeline.analyze(source)?;

if let Some(best) = result.corrections.best() {
    let fixed = pipeline.apply_corrections(source, &[best.clone()]);
    println!("After applying best fix:\n{}", fixed);
}
```

### Apply Corrections Above Threshold

```rust
let config = PipelineConfig {
    auto_apply_threshold: Some(0.9),
    ..Default::default()
};

let mut pipeline = CorrectionPipeline::new(python, None, config)?;
let result = pipeline.analyze(source)?;

// Get auto-applicable corrections
let auto_apply: Vec<_> = result.corrections.ranked()
    .iter()
    .filter(|c| c.confidence >= 0.9)
    .cloned()
    .collect();

if !auto_apply.is_empty() {
    let fixed = pipeline.apply_corrections(source, &auto_apply);
    println!("Auto-fixed {} issues", auto_apply.len());
}
```

## Pipeline Phases

### Phase 1: Parse

The pipeline uses `CodeParser` with tree-sitter for error-tolerant parsing:

```rust
// Internal: Phase 1
let parsed = self.parser.parse(source)?;

// Access errors
for error in parsed.errors() {
    println!("Error at line {}: {}", error.start_position.0, error.text);
}
```

### Phase 2: Tokenize

Extract tokens with context information:

```rust
// Internal: Phase 2
let tokenizer = CodeTokenizer::new(&*self.language);
let tokens = tokenizer.tokenize(&parsed.tree, &parsed.source);
```

### Phase 3: Analyze (CPG)

Build Code Property Graph for semantic analysis:

```rust
// Internal: Phase 3 (if full_semantic_analysis is true)
let cpg = CodePropertyGraph::from_parsed_code(&parsed);
```

### Phase 4: Correct

Apply ensemble corrector to tokens:

```rust
// Internal: Phase 4
for token in &tokens {
    let context = TokenContext::new(token.token_type);
    let corrections = self.corrector.correct_token(token, &context);
    all_corrections.extend(corrections);
}

// Semantic corrections from CPG
if let Some(ref cpg) = cpg {
    let semantic = self.corrector.analyze_full(&parsed, cpg);
    all_corrections.extend(semantic);
}
```

### Phase 5: Rank

Filter, deduplicate, and sort corrections:

```rust
// Internal: Phase 5
corrections.retain(|c| c.confidence >= self.config.min_confidence);
corrections.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());

// Deduplicate by (position, replacement)
let mut seen = HashSet::new();
corrections.retain(|c| {
    let key = (c.start_byte, c.end_byte, c.replacement.clone());
    seen.insert(key)
});

corrections.truncate(self.config.max_corrections);
```

## Error Handling

### PipelineError

Errors that can occur during pipeline execution:

```rust
pub enum PipelineError {
    ParseError(String),      // Tree-sitter parsing failed
    TokenizeError(String),   // Tokenization failed
    CpgError(String),        // CPG construction failed
    CorrectionError(String), // Correction failed
    IoError(std::io::Error), // I/O error
}
```

### Handling Errors

```rust
use libgrammstein::code::PipelineError;

match pipeline.analyze(source) {
    Ok(result) => {
        println!("Analysis complete: {} corrections", result.corrections.len());
    }
    Err(PipelineError::ParseError(msg)) => {
        eprintln!("Parse failed: {}", msg);
    }
    Err(PipelineError::CpgError(msg)) => {
        eprintln!("CPG construction failed: {}", msg);
    }
    Err(e) => {
        eprintln!("Pipeline error: {}", e);
    }
}
```

## Integration Example

Complete example using the pipeline:

```rust
use libgrammstein::code::{
    CorrectionPipeline, PipelineConfig, Python, DiagnosticSeverity
};
use std::sync::Arc;

fn analyze_and_fix(source: &str) -> Result<String, Box<dyn std::error::Error>> {
    let python = Arc::new(Python::new());

    // Create pipeline with custom config
    let config = PipelineConfig {
        max_corrections: 20,
        min_confidence: 0.5,
        include_diagnostics: true,
        auto_apply_threshold: Some(0.85),
        full_semantic_analysis: true,
    };

    let mut pipeline = CorrectionPipeline::new(python, None, config)?;

    // Add project context
    pipeline.corrector_mut().add_identifiers(&[
        "calculate_total", "process_data", "handle_error"
    ]);

    // Analyze
    let result = pipeline.analyze(source)?;

    // Report diagnostics
    println!("=== Diagnostics ===");
    for diag in &result.diagnostics {
        let prefix = match diag.severity {
            DiagnosticSeverity::Error => "ERROR",
            DiagnosticSeverity::Warning => "WARN",
            DiagnosticSeverity::Info => "INFO",
            DiagnosticSeverity::Hint => "HINT",
        };
        println!("[{}] Line {}: {}", prefix, diag.line + 1, diag.message);
    }

    // Report corrections
    println!("\n=== Corrections ===");
    for correction in result.corrections.ranked() {
        println!("  {} → {} (confidence: {:.0}%)",
            correction.original,
            correction.replacement,
            correction.confidence * 100.0
        );
    }

    // Apply high-confidence corrections
    let to_apply: Vec<_> = result.corrections.ranked()
        .iter()
        .filter(|c| c.confidence >= 0.85)
        .cloned()
        .collect();

    let fixed = pipeline.apply_corrections(source, &to_apply);

    println!("\n=== Fixed Source ===");
    println!("{}", fixed);

    Ok(fixed)
}

fn main() {
    let source = r#"
def calculate(x, y):
    reuslt = x + y
    retrun reuslt
"#;

    match analyze_and_fix(source) {
        Ok(fixed) => println!("Success!"),
        Err(e) => eprintln!("Error: {}", e),
    }
}
```

## Performance Considerations

### Phase Timing

| Phase | Complexity | Notes |
|-------|------------|-------|
| Parse | O(n) | Linear in source length |
| Tokenize | O(t) | t = number of tokens |
| CPG Build | O(n + e) | n = nodes, e = edges |
| Correct | O(t × c) | c = correction candidates |
| Rank | O(m log m) | m = total corrections |

### Optimization Tips

1. **Use `minimal()` for speed**: Skip CPG construction
2. **Increase `min_confidence`**: Reduce candidate processing
3. **Decrease `max_corrections`**: Limit sorting overhead
4. **Disable `full_semantic_analysis`**: Skip CPG when not needed

### Minimal vs Full Pipeline

| Feature | Minimal | Full |
|---------|---------|------|
| Lexical corrections | Yes | Yes |
| Grammar corrections | No | With grammar |
| Semantic corrections | No | Yes |
| CPG construction | No | Yes |
| Speed | Fast | Slower |
| Accuracy | Good | Best |

## Thread Safety

The pipeline is not `Sync` due to mutable parser state, but can be used across threads with proper synchronization:

```rust
use std::sync::Mutex;

let pipeline = Mutex::new(CorrectionPipeline::with_defaults(python, None)?);

// Lock for analysis
{
    let mut p = pipeline.lock().unwrap();
    let result = p.analyze(source)?;
}
```

For parallel analysis of multiple files, create separate pipeline instances:

```rust
use rayon::prelude::*;

let sources: Vec<&str> = vec![...];

let results: Vec<_> = sources.par_iter()
    .map(|source| {
        let python = Arc::new(Python::new());
        let mut pipeline = CorrectionPipeline::minimal(python).unwrap();
        pipeline.analyze(source)
    })
    .collect();
```

## See Also

- [Correctors Overview]correctors/overview.md - Correction architecture
- [Ensemble Corrector]correctors/ensemble.md - Multi-source correction
- [AST]ast.md - Tree-sitter parsing
- [Tokenizer]tokenizer.md - Token extraction
- [CPG]cpg.md - Code Property Graphs
- [Correction Framework]correction.md - Correction types