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
# Subtree Mining with TreeminerD

Discover frequent subtree patterns in code ASTs using the TreeminerD algorithm for idiom detection, clone finding, and pattern-based analysis.

## Overview

The subtree mining module provides:

- **TreeminerD algorithm**: Efficient frequent subtree mining
- **Flat tree encoding**: Depth-first tree representation
- **Pattern discovery**: Find common code structures
- **Parallel mining**: Multi-threaded pattern extraction

## Architecture

```
┌──────────────────────────────────────────────────────────────────┐
│                        TreeminerD Pipeline                       │
│                                                                  │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │                     Input: AST Forest                       │ │
│  │                                                             │ │
│  │  ParsedCode[1] ──► FlatTree[1]                             │ │
│  │  ParsedCode[2] ──► FlatTree[2]                             │ │
│  │  ParsedCode[n] ──► FlatTree[n]                             │ │
│  └────────────────────────────────────────────────────────────┘ │
│                              │                                   │
│                              ▼ Build vertical representation     │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │                   Vertical Index                            │ │
│  │                                                             │ │
│  │  "function_definition" → [(tree1, [0,5]), (tree2, [0])]   │ │
│  │  "identifier" → [(tree1, [1,6]), (tree2, [1,3])]          │ │
│  │  "parameters" → [(tree1, [2]), (tree2, [2])]              │ │
│  └────────────────────────────────────────────────────────────┘ │
│                              │                                   │
│                              ▼ Mine frequent patterns            │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │                 Equivalence Class Extension                 │ │
│  │                                                             │ │
│  │  1-patterns: {A}, {B}, {C}, ...                           │ │
│  │  2-patterns: {A→B}, {A→C}, {B→D}, ...                     │ │
│  │  k-patterns: extend with sibling/child nodes              │ │
│  └────────────────────────────────────────────────────────────┘ │
│                              │                                   │
│                              ▼                                   │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │               Output: SubtreePattern[]                      │ │
│  │                                                             │ │
│  │  Pattern 1: function → params → body (support: 85%)        │ │
│  │  Pattern 2: if → condition → block (support: 72%)          │ │
│  │  Pattern 3: for → init → condition → update (support: 45%) │ │
│  └────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────┘
```

## TreeminerConfig

Configuration for the TreeminerD algorithm:

```rust
pub struct TreeminerConfig {
    /// Minimum support threshold (0.0 - 1.0)
    pub min_support: f64,
    /// Maximum pattern size (number of nodes)
    pub max_pattern_size: usize,
    /// Maximum pattern depth
    pub max_depth: usize,
    /// Minimum pattern size to report
    pub min_pattern_size: usize,
    /// Whether to use parallel mining
    pub parallel: bool,
    /// Number of threads (0 = auto)
    pub num_threads: usize,
}
```

### Configuration Parameters

| Parameter | Default | Description |
|-----------|---------|-------------|
| `min_support` | 0.1 | Minimum fraction of trees containing pattern |
| `max_pattern_size` | 20 | Maximum nodes in a pattern |
| `max_depth` | 10 | Maximum pattern depth |
| `min_pattern_size` | 2 | Minimum nodes to report |
| `parallel` | true | Enable parallel mining |
| `num_threads` | 0 | Thread count (0 = auto) |

### Creating Configuration

```rust
use libgrammstein::code::subtree::TreeminerConfig;

// Default configuration
let config = TreeminerConfig::default();

// Custom configuration
let config = TreeminerConfig {
    min_support: 0.05,       // 5% minimum support
    max_pattern_size: 30,    // Larger patterns
    max_depth: 15,           // Deeper structures
    min_pattern_size: 3,     // At least 3 nodes
    parallel: true,
    num_threads: 8,          // 8 threads
};
```

## FlatNode

A node in a depth-first encoded tree:

```rust
pub struct FlatNode {
    /// Node label (e.g., "function_definition")
    pub label: Arc<str>,
    /// Depth in tree (root = 0)
    pub depth: usize,
    /// Scope (position in encoding)
    pub scope: usize,
}
```

### Creating FlatNodes

```rust
use libgrammstein::code::subtree::FlatNode;

let node = FlatNode::new("function_definition", 0, 0);
println!("Label: {}", node.label);  // function_definition
println!("Depth: {}", node.depth);  // 0

// Create child node
let child = FlatNode::new("identifier", 1, 1);
```

## FlatTree

A tree represented in depth-first encoding:

```rust
pub struct FlatTree {
    /// Nodes in depth-first order
    pub nodes: Vec<FlatNode>,
    /// Unique tree identifier
    pub tree_id: u64,
    /// Optional metadata
    pub metadata: Option<TreeMetadata>,
}
```

### Depth-First Encoding

The encoding represents a tree as a sequence of nodes in depth-first order:

```
       A              DFS encoding: A(0) B(1) D(2) C(1)
      / \
     B   C            With implicit backtrack markers:
    /                 A B D -1 -1 C -1 -1
   D
```

### Creating FlatTrees

```rust
use libgrammstein::code::subtree::{FlatTree, FlatNode, TreeMetadata};

// Create from nodes
let nodes = vec![
    FlatNode::new("function", 0, 0),
    FlatNode::new("params", 1, 1),
    FlatNode::new("body", 1, 2),
    FlatNode::new("return", 2, 3),
];
let tree = FlatTree::new(nodes, 1);

// With metadata
let metadata = TreeMetadata {
    path: Some("src/main.rs".to_string()),
    language: Some("rust".to_string()),
    source: None,
};
let tree = FlatTree::with_metadata(nodes, 1, metadata);

// From AST node
let ast_node = parse_code("def foo(): pass")?;
let tree = FlatTree::from_ast_node(&ast_node, 42);
```

### FlatTree Operations

```rust
// Tree properties
println!("Nodes: {}", tree.len());
println!("Empty: {}", tree.is_empty());

// Get label positions
let positions = tree.label_positions();
for (label, pos) in positions {
    println!("{} at positions: {:?}", label, pos);
}

// Extract subtree starting at position
if let Some(subtree) = tree.extract_subtree(1) {
    println!("Subtree has {} nodes", subtree.len());
}
```

## PatternNode

A node in a subtree pattern:

```rust
pub struct PatternNode {
    /// Node label
    pub label: Arc<str>,
    /// Depth in pattern (root = 0)
    pub depth: usize,
}
```

### Creating PatternNodes

```rust
use libgrammstein::code::subtree::PatternNode;

// Direct creation
let node = PatternNode::new("if_statement", 0);

// From FlatNode with relative depth
let flat_node = FlatNode::new("block", 3, 5);
let pattern_node = PatternNode::from_flat(&flat_node, 2);  // base_depth=2
// pattern_node.depth = 3 - 2 = 1
```

## SubtreePattern

A discovered frequent subtree pattern:

```rust
pub struct SubtreePattern {
    /// Nodes in depth-first order
    pub nodes: Vec<PatternNode>,
    /// Support count (trees containing pattern)
    pub support: usize,
    /// Support ratio (support / total_trees)
    pub support_ratio: f64,
    /// Tree IDs where pattern occurs
    pub occurrences: Vec<u64>,
    /// Pattern ID
    pub pattern_id: u64,
}
```

### Pattern Properties

```rust
let pattern: SubtreePattern = /* ... */;

// Size and depth
println!("Size: {} nodes", pattern.size());
println!("Max depth: {}", pattern.max_depth());

// Support
println!("Support: {} trees", pattern.support);
println!("Support ratio: {:.1}%", pattern.support_ratio * 100.0);

// Root label
if let Some(root) = pattern.root_label() {
    println!("Root: {}", root);
}

// Human-readable representation
println!("Pattern:\n{}", pattern.to_string_repr());
// Output:
// function
//   params
//   body
```

### Pattern Containment

```rust
// Check if one pattern contains another
let larger_pattern: SubtreePattern = /* A -> B -> C */;
let smaller_pattern: SubtreePattern = /* A -> B */;

if larger_pattern.contains(&smaller_pattern) {
    println!("Larger pattern is a superset");
}
```

## TreeminerD

Main algorithm for frequent subtree mining:

```rust
pub struct TreeminerD {
    config: TreeminerConfig,
    // ...
}
```

### Creating a Miner

```rust
use libgrammstein::code::subtree::{TreeminerD, TreeminerConfig};

// With minimum support
let miner = TreeminerD::new(0.1);  // 10% support

// With full configuration
let config = TreeminerConfig {
    min_support: 0.05,
    max_pattern_size: 25,
    parallel: true,
    ..Default::default()
};
let miner = TreeminerD::with_config(config);
```

### Mining Patterns

```rust
let miner = TreeminerD::new(0.1);
let trees: Vec<FlatTree> = prepare_trees(&sources);

let result = miner.mine(&trees);

println!("Mining Statistics:");
println!("  Trees: {}", result.num_trees);
println!("  Min support count: {}", result.min_support_count);
println!("  Patterns found: {}", result.patterns.len());
println!("  Candidates generated: {}", result.candidates_generated);
println!("  Patterns pruned: {}", result.patterns_pruned);
println!("  Time: {}ms", result.mining_time_ms);

// Access patterns
for pattern in &result.patterns {
    println!(
        "Pattern {} (support: {:.1}%):\n{}",
        pattern.pattern_id,
        pattern.support_ratio * 100.0,
        pattern.to_string_repr()
    );
}
```

## MiningResult

Result of a mining operation:

```rust
pub struct MiningResult {
    /// Discovered frequent patterns
    pub patterns: Vec<SubtreePattern>,
    /// Total input trees
    pub num_trees: usize,
    /// Minimum support count used
    pub min_support_count: usize,
    /// Candidates generated
    pub candidates_generated: usize,
    /// Patterns pruned (below support)
    pub patterns_pruned: usize,
    /// Mining time in milliseconds
    pub mining_time_ms: u64,
}
```

## Pattern Encoding

Utilities for encoding and comparing patterns:

```rust
use libgrammstein::code::subtree::encoding;

let nodes = vec![
    PatternNode::new("A", 0),
    PatternNode::new("B", 1),
    PatternNode::new("C", 1),
];

// Encode to string
let encoded = encoding::encode_pattern(&nodes);
println!("Encoded: {}", encoded);  // "0:A|1:B|1:C"

// Decode from string
let decoded = encoding::decode_pattern(&encoded);
assert_eq!(decoded, nodes);

// Compute hash
let hash = encoding::pattern_hash(&nodes);
println!("Hash: {}", hash);
```

## Algorithm Details

### Phase 1: Vertical Representation

Build an index mapping labels to their positions:

```rust
// For each label, track (tree_id, [positions])
// "function" → [(tree1, [0, 5]), (tree2, [0])]
// "params"   → [(tree1, [1]), (tree2, [1])]
```

### Phase 2: Frequent 1-Subtrees

Find single-node patterns meeting support threshold:

```rust
// Count occurrences across trees
// "function" appears in 95% of trees → frequent
// "generator" appears in 2% of trees → pruned
```

### Phase 3: Equivalence Class Extension

Iteratively extend patterns:

```rust
// Start with frequent 1-patterns: {function}, {params}, ...
// Generate 2-patterns: {function→params}, {function→body}, ...
// Continue until no more frequent patterns found
```

### Phase 4: Support Filtering

Prune patterns below threshold:

```rust
// Keep: function→params (support: 80%)
// Prune: function→params→generator (support: 3%)
```

## Integration Example

Complete workflow for mining code idioms:

```rust
use libgrammstein::code::{
    CodeParser,
    subtree::{TreeminerD, TreeminerConfig, FlatTree}
};

fn mine_code_patterns(sources: &[&str]) -> Result<Vec<SubtreePattern>, Box<dyn Error>> {
    // Parse sources to ASTs
    let parser = CodeParser::<Python>::new()?;
    let mut trees = Vec::with_capacity(sources.len());

    for (i, source) in sources.iter().enumerate() {
        let parsed = parser.parse(source)?;
        if let Some(ast) = &parsed.ast {
            trees.push(FlatTree::from_ast_node(ast, i as u64));
        }
    }

    // Configure miner
    let config = TreeminerConfig {
        min_support: 0.1,        // 10% minimum
        max_pattern_size: 15,    // Up to 15 nodes
        max_depth: 8,            // Max depth 8
        min_pattern_size: 3,     // At least 3 nodes
        parallel: true,
        num_threads: 0,          // Auto-detect
    };

    let miner = TreeminerD::with_config(config);
    let result = miner.mine(&trees);

    println!("Found {} patterns in {}ms",
        result.patterns.len(),
        result.mining_time_ms
    );

    // Sort by support
    let mut patterns = result.patterns;
    patterns.sort_by(|a, b| {
        b.support_ratio.partial_cmp(&a.support_ratio).unwrap()
    });

    Ok(patterns)
}

fn main() -> Result<(), Box<dyn Error>> {
    let sources = load_python_files("./src")?;
    let patterns = mine_code_patterns(&sources)?;

    println!("\nTop 10 Code Patterns:");
    for (i, pattern) in patterns.iter().take(10).enumerate() {
        println!(
            "\n{}. {} ({:.1}% of files):",
            i + 1,
            pattern.root_label().unwrap_or("unknown"),
            pattern.support_ratio * 100.0
        );
        println!("{}", pattern.to_string_repr());
    }

    Ok(())
}
```

## Applications

### Idiom Discovery

Find common coding patterns:

```rust
let config = TreeminerConfig {
    min_support: 0.3,  // Common patterns (30%+)
    min_pattern_size: 4,
    ..Default::default()
};

let miner = TreeminerD::with_config(config);
let result = miner.mine(&project_trees);

// Filter to specific structures
let function_idioms: Vec<_> = result.patterns
    .iter()
    .filter(|p| p.root_label() == Some("function_definition"))
    .collect();
```

### Clone Detection

Find duplicated code structures:

```rust
let config = TreeminerConfig {
    min_support: 0.02,  // Rare patterns (2%+)
    min_pattern_size: 10,  // Larger structures
    ..Default::default()
};

let miner = TreeminerD::with_config(config);
let result = miner.mine(&all_trees);

// Patterns occurring in few files might be clones
let potential_clones: Vec<_> = result.patterns
    .iter()
    .filter(|p| p.support >= 2 && p.support <= 5)
    .filter(|p| p.size() >= 15)  // Substantial size
    .collect();
```

### Pattern-Based Completion

Build suggestions from frequent patterns:

```rust
// Mine common continuations
let result = miner.mine(&corpus_trees);

// Index by prefix
let mut completions: HashMap<String, Vec<&SubtreePattern>> = HashMap::new();
for pattern in &result.patterns {
    if let Some(root) = pattern.root_label() {
        completions.entry(root.to_string())
            .or_default()
            .push(pattern);
    }
}

// Suggest completions
fn suggest(context: &str, completions: &HashMap<String, Vec<&SubtreePattern>>) -> Vec<String> {
    completions.get(context)
        .map(|patterns| {
            patterns.iter()
                .map(|p| p.to_string_repr())
                .collect()
        })
        .unwrap_or_default()
}
```

## Performance

| Operation | Complexity | Notes |
|-----------|------------|-------|
| Build vertical | O(n × m) | n = trees, m = avg nodes |
| Find 1-patterns | O(l) | l = unique labels |
| Extend patterns | O(p × t × e) | p = patterns, t = trees, e = extensions |
| Total mining | O(k × n × m) | k = max pattern size |

### Memory Usage

```
Vertical index: O(l × n) where l = labels, n = trees
Patterns: O(p × s) where p = patterns, s = avg size
Working set: O(current_level_size)
```

### Optimization Tips

1. **Use parallelism**: Enable `parallel: true` for large datasets
2. **Limit depth/size**: Reduce `max_depth` and `max_pattern_size` for speed
3. **Higher support**: Increase `min_support` to reduce search space
4. **Batch processing**: Process files in batches for memory efficiency

## Thread Safety

`TreeminerD` is thread-safe:

```rust
use std::sync::Arc;

let miner = Arc::new(TreeminerD::new(0.1));

// Mining uses internal parallelism (rayon)
let result = miner.mine(&trees);

// Safe to share across threads
let miner_clone = Arc::clone(&miner);
std::thread::spawn(move || {
    let other_result = miner_clone.mine(&other_trees);
});
```

## See Also

- [AST]ast.md - Tree-sitter parsing
- [CPG]cpg.md - Code Property Graphs
- [Pipeline]pipeline.md - End-to-end workflow
- [Semantic Corrector]correctors/semantic.md - Pattern-based correction