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
# Tokenizer Module

The tokenizer module provides code-aware tokenization that preserves semantic information from tree-sitter parsing, enabling type-aware correction.

## Overview

Unlike simple lexical tokenizers, the code tokenizer:

- **Preserves AST context**: Parent nodes, siblings, depth
- **Classifies tokens semantically**: Keywords, identifiers, types, literals
- **Tracks error regions**: Marks tokens inside parse errors
- **Filters selectively**: Include/exclude whitespace and comments

## Key Types

| Type | Description |
|------|-------------|
| `CodeToken` | Token with text, position, type, and context |
| `CodeTokenizer<L>` | Configurable tokenizer for a language |
| `TokenIterator<L>` | Iterator over tokens in source code |

## CodeToken

The `CodeToken` struct contains comprehensive token information:

```rust
pub struct CodeToken {
    /// The token text
    pub text: String,
    /// Byte offset in the source
    pub byte_offset: usize,
    /// Line number (0-indexed)
    pub line: usize,
    /// Column number (0-indexed)
    pub column: usize,
    /// Token type classification
    pub token_type: TokenType,
    /// Tree-sitter node kind
    pub node_kind: String,
    /// Contextual information
    pub context: TokenContext,
}
```

### Methods

```rust
impl CodeToken {
    /// Creates a new code token.
    pub fn new(
        text: impl Into<String>,
        byte_offset: usize,
        line: usize,
        column: usize,
        token_type: TokenType,
        node_kind: impl Into<String>,
    ) -> Self;

    /// Returns whether this token is inside an error region.
    pub fn is_in_error(&self) -> bool;

    /// Returns whether this token should be considered for correction.
    pub fn is_correctable(&self) -> bool;
}
```

### Example: Creating Tokens

```rust
use libgrammstein::code::{CodeToken, TokenType};

let token = CodeToken::new(
    "calculate_total",
    0,        // byte offset
    1,        // line (0-indexed)
    5,        // column (0-indexed)
    TokenType::Identifier,
    "identifier"
);

assert_eq!(token.text, "calculate_total");
assert_eq!(token.token_type, TokenType::Identifier);
assert!(token.is_correctable());  // Identifiers are correctable
assert!(!token.is_in_error());    // Not in error region
```

## TokenType

Tokens are classified into semantic categories:

```rust
pub enum TokenType {
    Keyword,         // if, while, fn, let
    Identifier,      // variable names, function names
    TypeName,        // int, String, Vec
    Operator,        // +, -, *, /
    Punctuation,     // ;, ,, (, )
    StringLiteral,   // "hello"
    NumericLiteral,  // 42, 3.14
    BooleanLiteral,  // true, false
    Comment,         // // comment
    Whitespace,      // spaces, tabs
    Special,         // language-specific
    Unknown,         // unclassified
}
```

### Correctability

Not all token types are correctable:

| Token Type | Correctable | Reason |
|------------|-------------|--------|
| `Keyword` | Yes | Can be misspelled (`retrun``return`) |
| `Identifier` | Yes | Can be misspelled or wrong |
| `TypeName` | Yes | Can be misspelled |
| `Operator` | Yes | Can be transposed (`=+``+=`) |
| `Punctuation` | Yes | Can be missing or extra |
| `StringLiteral` | Yes | Content can be spell-checked |
| `NumericLiteral` | No | Format validation only |
| `BooleanLiteral` | Yes | Limited vocabulary |
| `Comment` | No | Natural language, not code |
| `Whitespace` | No | Formatting only |

## TokenContext

The `TokenContext` struct provides structural information:

```rust
pub struct TokenContext {
    /// The token type classification
    pub token_type: TokenType,
    /// Parent node type in the AST (e.g., "function_definition")
    pub parent_node_type: Option<String>,
    /// Sibling node types for positional context
    pub sibling_types: Vec<String>,
    /// Depth in the AST (0 = root)
    pub depth: usize,
    /// Whether the token is inside an error node
    pub in_error_region: bool,
    /// Expected token types at this position (from grammar)
    pub expected_types: Vec<TokenType>,
}
```

### Context Example

```rust
// For token "result" in: def calculate(x): result = x + 1
let context = token.context;

assert_eq!(context.parent_node_type, Some("assignment".into()));
assert!(context.sibling_types.contains(&"=".into()));
assert!(context.sibling_types.contains(&"binary_operator".into()));
assert_eq!(context.depth, 3);  // module → function → block → assignment
assert!(!context.in_error_region);
```

## CodeTokenizer

The `CodeTokenizer<L>` extracts tokens from parsed code:

```rust
pub struct CodeTokenizer<'a, L: CodeLanguage> {
    language: &'a L,
    include_whitespace: bool,
    include_comments: bool,
}
```

### Builder Pattern

```rust
use libgrammstein::code::{CodeTokenizer, Python};

let python = Python::new();

// Default: no whitespace, no comments
let tokenizer = CodeTokenizer::new(&python);

// Include whitespace (for indentation-sensitive languages)
let tokenizer = CodeTokenizer::new(&python)
    .with_whitespace(true);

// Include comments (for documentation analysis)
let tokenizer = CodeTokenizer::new(&python)
    .with_comments(true);

// Include both
let tokenizer = CodeTokenizer::new(&python)
    .with_whitespace(true)
    .with_comments(true);
```

### Tokenization Methods

```rust
impl<'a, L: CodeLanguage> CodeTokenizer<'a, L> {
    /// Extracts all tokens from a parsed tree.
    pub fn tokenize(&self, tree: &Tree, source: &str) -> Vec<CodeToken>;

    /// Extracts tokens only from error regions.
    pub fn tokenize_errors(&self, tree: &Tree, source: &str) -> Vec<CodeToken>;
}
```

### Example: Basic Tokenization

```rust
use libgrammstein::code::{CodeParser, CodeTokenizer, Python};
use std::sync::Arc;

let python = Arc::new(Python::new());
let mut parser = CodeParser::new(python.clone())?;

let source = r#"
def greet(name):
    print(f"Hello, {name}!")
"#;

let parsed = parser.parse(source)?;
let tokenizer = CodeTokenizer::new(python.as_ref());
let tokens = tokenizer.tokenize(&parsed.tree, source);

for token in &tokens {
    println!(
        "{:15} {:15} line {} col {}",
        token.text,
        format!("{:?}", token.token_type),
        token.line + 1,
        token.column
    );
}
```

Output:
```
def             Keyword         line 2 col 0
greet           Identifier      line 2 col 4
(               Punctuation     line 2 col 9
name            Identifier      line 2 col 10
)               Punctuation     line 2 col 14
:               Punctuation     line 2 col 15
print           Identifier      line 3 col 4
...
```

### Example: Error-Focused Tokenization

```rust
let source = "def foo(\n    retrun 42";  // Missing ) and misspelled return
let parsed = parser.parse(source)?;

let tokenizer = CodeTokenizer::new(python.as_ref());
let error_tokens = tokenizer.tokenize_errors(&parsed.tree, source);

println!("Tokens in error regions:");
for token in error_tokens {
    println!(
        "  '{}' ({:?}) at line {}",
        token.text,
        token.token_type,
        token.line + 1
    );
}
```

## Token Filtering

The tokenizer automatically filters based on configuration:

### Default (No Whitespace/Comments)

```rust
let tokenizer = CodeTokenizer::new(&python);
let tokens = tokenizer.tokenize(&tree, source);

// Tokens: def, foo, (, x, ), :, return, x, +, 1
// No whitespace or comments
```

### With Whitespace (Python)

For Python, whitespace is significant for indentation:

```rust
let tokenizer = CodeTokenizer::new(&python)
    .with_whitespace(true);

let tokens = tokenizer.tokenize(&tree, source);
// Now includes indentation tokens
```

### With Comments

For documentation extraction or comment spell-checking:

```rust
let tokenizer = CodeTokenizer::new(&python)
    .with_comments(true);

let tokens = tokenizer.tokenize(&tree, source);
// Now includes comment tokens
```

## Using TokenContext

The token context enables grammar-aware correction:

### Example: Context-Aware Correction

```rust
for token in tokens {
    if token.is_in_error() {
        println!("Error token: '{}'", token.text);

        // Use context for better correction
        if let Some(parent) = &token.context.parent_node_type {
            match parent.as_str() {
                "function_definition" => {
                    // Likely a keyword or parameter
                    suggest_from(&["def", "async", "return"]);
                }
                "assignment" => {
                    // Likely an identifier
                    suggest_from_corpus();
                }
                _ => {}
            }
        }
    }
}
```

### Example: Identifying Declaration Contexts

```rust
fn is_function_parameter(token: &CodeToken) -> bool {
    token.context.parent_node_type.as_deref() == Some("parameters")
}

fn is_type_annotation(token: &CodeToken) -> bool {
    token.context.parent_node_type.as_deref() == Some("type")
}

fn is_import_statement(token: &CodeToken) -> bool {
    token.context.parent_node_type.as_deref() == Some("import_statement")
        || token.context.parent_node_type.as_deref() == Some("import_from_statement")
}
```

## TokenIterator

For streaming tokenization:

```rust
pub struct TokenIterator<'a, L: CodeLanguage> {
    tokenizer: CodeTokenizer<'a, L>,
    tree: Tree,
    source: String,
    tokens: Vec<CodeToken>,
    position: usize,
}

impl<L: CodeLanguage> Iterator for TokenIterator<'_, L> {
    type Item = CodeToken;
    fn next(&mut self) -> Option<Self::Item>;
}
```

### Example: Iterator Usage

```rust
let iterator = TokenIterator::new(tokenizer, tree, source);

// Find all identifiers
let identifiers: Vec<_> = iterator
    .filter(|t| t.token_type == TokenType::Identifier)
    .collect();

// Find correctable tokens in errors
let correctable_errors: Vec<_> = iterator
    .filter(|t| t.is_in_error() && t.is_correctable())
    .collect();
```

## Integration with Correctors

The tokenizer output feeds directly into correctors:

```rust
use libgrammstein::code::{
    CodeParser, CodeTokenizer, LexicalCorrector, Python
};
use std::sync::Arc;

let python = Arc::new(Python::new());
let mut parser = CodeParser::new(python.clone())?;
let tokenizer = CodeTokenizer::new(python.as_ref());

// Parse code with errors
let source = "def calcluate(x):\n    retrun x";
let parsed = parser.parse(source)?;

// Extract tokens from error regions
let error_tokens = tokenizer.tokenize_errors(&parsed.tree, source);

// Create corrector
let mut corrector = LexicalCorrector::with_defaults(python);

// Correct each error token
for token in error_tokens {
    let corrections = corrector.correct_token(&token, &token.context);
    for correction in corrections.top(3) {
        println!("{} -> {} ({:.2})",
            token.text,
            correction.replacement,
            correction.confidence
        );
    }
}
```

## Performance

| Operation | Complexity | Notes |
|-----------|------------|-------|
| Full tokenization | O(n) | n = leaf nodes in AST |
| Error tokenization | O(e) | e = nodes in error regions |
| Token creation | O(1) | Constant per token |
| Context extraction | O(s) | s = number of siblings |

### Optimization Tips

1. **Use `tokenize_errors()`** for correction - don't tokenize entire file
2. **Disable whitespace/comments** unless needed
3. **Cache tokenizer** - create once per language
4. **Batch processing** - tokenize multiple error regions together

## Thread Safety

`CodeTokenizer` is `Send` but not `Sync` (holds language reference):

```rust
use std::thread;

let python = Python::new();

// Move tokenizer to thread
thread::spawn(move || {
    let tokenizer = CodeTokenizer::new(&python);
    // Use tokenizer in this thread
});
```

For parallel tokenization, create separate tokenizers per thread:

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

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

let results: Vec<_> = sources.par_iter()
    .map(|source| {
        let python = Python::new();
        let tokenizer = CodeTokenizer::new(&python);
        let mut parser = CodeParser::new(Arc::new(python.clone())).unwrap();
        let parsed = parser.parse(source).unwrap();
        tokenizer.tokenize(&parsed.tree, source)
    })
    .collect();
```

## See Also

- [Language Framework]language.md - `TokenType` and `TokenContext`
- [AST]ast.md - Tree-sitter parsing
- [Correction]correction.md - Using tokens for correction
- [Correctors]correctors/overview.md - Token-level correction