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
# Architecture Overview

This document provides a high-level view of libgrammstein's architecture, explaining how components fit together and the design principles behind them.

## Design Goals

libgrammstein is designed with four primary goals:

1. **Hybrid Scoring**: Combine the strengths of N-gram models (precise local context) with embeddings (semantic similarity, OOV handling)

2. **WFST Integration**: Seamlessly integrate with lling-llang's lattice-based text correction pipeline

3. **Efficiency**: Handle large corpora (10GB+) and serve millions of queries per second

4. **Modularity**: Each component is independently usable and testable

## System Architecture

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                           libgrammstein                                      │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                        Hybrid Layer                                  │   │
│  │  ┌─────────────────────────────────────────────────────────────────┐│   │
│  │  │               HybridLanguageModel                               ││   │
│  │  │   - Interpolates N-gram and embedding scores                    ││   │
│  │  │   - Implements LanguageModel trait                              ││   │
│  │  │   - LRU cache for hot queries                                   ││   │
│  │  └─────────────────────────────────────────────────────────────────┘│   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│           │                                     │                           │
│           ▼                                     ▼                           │
│  ┌─────────────────────────┐     ┌─────────────────────────────────────┐   │
│  │     N-gram Layer        │     │       Embedding Layer               │   │
│  │                         │     │                                     │   │
│  │  ┌───────────────────┐  │     │  ┌─────────────────────────────┐   │   │
│  │  │    NgramModel     │  │     │  │    SubwordEmbedding         │   │   │
│  │  │ - Modified KN     │  │     │  │ - Word embeddings           │   │   │
│  │  │ - Order 3-5       │  │     │  │ - Subword (char n-gram)     │   │   │
│  │  │ - Backoff chain   │  │     │  │ - BPE tokenizer             │   │   │
│  │  └────────┬──────────┘  │     │  └────────────┬────────────────┘   │   │
│  │           │             │     │               │                     │   │
│  │  ┌────────▼──────────┐  │     │  ┌────────────▼────────────────┐   │   │
│  │  │ KneserNeySmooth   │  │     │  │   Skip-gram Trainer         │   │   │
│  │  │ - D1, D2, D3+     │  │     │  │ - Negative sampling         │   │   │
│  │  │ - Continuation    │  │     │  │ - Window size               │   │   │
│  │  └───────────────────┘  │     │  └─────────────────────────────┘   │   │
│  └─────────────────────────┘     └─────────────────────────────────────┘   │
│           │                                                                 │
│           ▼                                                                 │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                      Storage Layer                                   │   │
│  │  ┌───────────────────┐  ┌───────────────────┐  ┌─────────────────┐  │   │
│  │  │  DynamicDawgChar  │  │ PathMapDictionary │  │ DoubleArrayTrie │  │   │
│  │  │  (mutable, serde) │  │ (distributed)     │  │ (static, fast)  │  │   │
│  │  └───────────────────┘  └───────────────────┘  └─────────────────┘  │   │
│  │                           liblevenshtein-rust                        │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
           │ implements LanguageModel trait
┌─────────────────────────────────────────────────────────────────────────────┐
│                              lling-llang                                     │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                      LanguageModelLayer                               │   │
│  │    Wraps LanguageModel trait for use in CorrectionLayer pipelines    │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘
```

### Neural, RAG, and Topic Layers

The following layers provide advanced neural capabilities, document retrieval, and topic modeling:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                           Neural Layer                                       │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
│  │ ModernBertModel │──│ ModernBert      │──│   ModernBertRescorer        │  │
│  │ (149M params)   │  │ Embedder        │  │   (beam search rescoring)   │  │
│  │ - 8K context    │  │ - 768-dim       │  │   - pseudo-perplexity       │  │
│  │ - MLM head      │  │ - CLS/Mean pool │  │   - embedding coherence     │  │
│  └────────┬────────┘  └────────┬────────┘  └─────────────────────────────┘  │
│           │                    │                                             │
│           │           ┌────────┴────────┐                                   │
│           │           │   Summarizer    │                                   │
│           │           │ - MMR selection │                                   │
│           │           │ - Extractive    │                                   │
│           │           └─────────────────┘                                   │
└───────────┼─────────────────────┼───────────────────────────────────────────┘
            │                     │
            ▼                     ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                              RAG Layer                                       │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
│  │  IndexBuilder   │──│   RagIndex<B>   │──│       Retriever<B>          │  │
│  │ - auto-embed    │  │ - storage+query │  │ - text→embedding→results   │  │
│  │ - auto-synopsis │  │ - topic model   │  │ - batch retrieval          │  │
│  └─────────────────┘  └────────┬────────┘  └─────────────────────────────┘  │
│                                │                                             │
│  ┌─────────────────────────────┼────────────────────────────────────────┐   │
│  │            RetrievalBackend │                                         │   │
│  │  ┌──────────────────────┐  └────┐  ┌─────────────────────────────┐   │   │
│  │  │ ExactCosineBackend   │       │  │       HnswBackend           │   │   │
│  │  │ - BLAS-accelerated   │       │  │ - approx NN, O(log n)       │   │   │
│  │  │ - O(n) query, <1M    │       │  │ - scalable, >1M docs        │   │   │
│  │  └──────────────────────┘       │  └─────────────────────────────┘   │   │
│  └─────────────────────────────────┴────────────────────────────────────┘   │
└─────────────────────────────────────┬───────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│                             Topic Layer                                      │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
│  │ TopicExtractor  │──│   TopicModel    │──│         Topic               │  │
│  │ - HAC cluster   │  │ - topics map    │  │ - id, keywords, desc        │  │
│  │ - c-TF-IDF      │  │ - assignments   │  │ - document_count            │  │
│  └────────┬────────┘  └─────────────────┘  └─────────────────────────────┘  │
│           │                                                                  │
│  ┌────────┴────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
│  │ Hierarchical    │  │     CtfIdf      │  │       Dendrogram            │  │
│  │ Clustering      │  │ - keywords/topic│  │ - scipy-compatible          │  │
│  │ - Ward/Complete │  │ - min_df/max_df │  │ - cut_tree/cut_by_distance  │  │
│  └─────────────────┘  └─────────────────┘  └─────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────────────────┘
```

### Code Correction Layer

The Code Correction layer provides multi-language code error detection and correction:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                           Code Correction Layer                              │
│                                                                             │
│  ┌────────────────────────────────────────────────────────────────────────┐ │
│  │                        CorrectionPipeline<L>                            │ │
│  │    Parse → Tokenize → Analyze → Correct → Rank → AnalysisResult        │ │
│  └───────────────────────────────────┬────────────────────────────────────┘ │
│                                      │                                       │
│  ┌───────────────────────────────────┴───────────────────────────────────┐  │
│  │                        EnsembleCorrector<L>                            │  │
│  │                                                                        │  │
│  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐                 │  │
│  │  │   Lexical    │  │   Grammar    │  │   Semantic   │                 │  │
│  │  │  Corrector   │  │  Corrector   │  │  Corrector   │                 │  │
│  │  │              │  │              │  │              │                 │  │
│  │  │ Levenshtein  │  │ PCFG/Earley  │  │  GNN + CPG   │                 │  │
│  │  └──────────────┘  └──────────────┘  └──────────────┘                 │  │
│  └────────────────────────────────────────────────────────────────────────┘  │
│                                      │                                       │
│  ┌───────────────────────────────────┴───────────────────────────────────┐  │
│  │                       Code Analysis Layer                              │  │
│  │                                                                        │  │
│  │  ┌─────────────────┐  ┌─────────────────┐  ┌──────────────────────┐   │  │
│  │  │   CodeParser    │  │  CodeTokenizer  │  │ CodePropertyGraph    │   │  │
│  │  │   (tree-sitter) │  │  (token types)  │  │ (AST + CFG + DFG)    │   │  │
│  │  └─────────────────┘  └─────────────────┘  └──────────────────────┘   │  │
│  │                                                                        │  │
│  │  ┌─────────────────────────────────────────────────────────────────┐  │  │
│  │  │                    CodeLanguage Trait                            │  │  │
│  │  │  Python │ Rust │ JavaScript │ Rholang │ MeTTa │ Custom...       │  │  │
│  │  └─────────────────────────────────────────────────────────────────┘  │  │
│  └────────────────────────────────────────────────────────────────────────┘  │
│                                      │                                       │
│  ┌───────────────────────────────────┴───────────────────────────────────┐  │
│  │                      Advanced Components                               │  │
│  │                                                                        │  │
│  │  ┌───────────────┐  ┌───────────────┐  ┌────────────────────────┐     │  │
│  │  │   WeightedCFG │  │ GnnSemantic   │  │     CodeEmbedder       │     │  │
│  │  │   + Trainer   │  │    Scorer     │  │ (UniXcoder/GraphCode)  │     │  │
│  │  └───────────────┘  └───────────────┘  └────────────────────────┘     │  │
│  │                                                                        │  │
│  │  ┌───────────────┐  ┌───────────────────────────────────────────┐     │  │
│  │  │ GrammarConstr │  │              WFST Export                   │     │  │
│  │  │ (Earley)      │  │  (lling-llang integration)                │     │  │
│  │  └───────────────┘  └───────────────────────────────────────────┘     │  │
│  └────────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

## Component Relationships

### Training Pipeline

```
Corpus Files                                 Trained Models
    │                                              ▲
    ▼                                              │
┌─────────────────┐                       ┌────────┴────────┐
│  CorpusReader   │                       │   save()/load() │
│  - Wikipedia    │                       │                 │
│  - Gutenberg    │                       │  ┌───────────┐  │
│  - Plaintext    │                       │  │ NgramModel│  │
└────────┬────────┘                       │  └───────────┘  │
         │                                │                 │
         │ sentences()                    │  ┌───────────┐  │
         ▼                                │  │ Embedding │  │
┌─────────────────┐                       │  └───────────┘  │
│   Tokenizer     │                       │                 │
│  - Sentence     │                       │  ┌───────────┐  │
│  - Word         │                       │  │  Hybrid   │  │
└────────┬────────┘                       │  └───────────┘  │
         │                                └─────────────────┘
         │ tokens                                 ▲
         ▼                                        │
┌─────────────────────────────────────────────────┤
│                                                 │
│  ┌─────────────────┐     ┌─────────────────┐   │
│  │  NgramTrainer   │     │ EmbeddingTrainer│   │
│  │                 │     │                 │   │
│  │  - Count ngrams │     │  - Skip-gram    │   │
│  │  - Compute MKN  │     │  - Neg sampling │   │
│  │  - Store in trie│     │  - Subwords     │   │
│  └────────┬────────┘     └────────┬────────┘   │
│           │                       │            │
│           └───────────┬───────────┘            │
│                       │                        │
│              ┌────────▼────────┐               │
│              │ HybridLanguage  │               │
│              │     Model       │───────────────┘
│              └─────────────────┘
│
│                    Training
└─────────────────────────────────────────────────
```

### Inference Pipeline

```
Input: ["the", "quick", "brown", "fox"]
┌─────────────────────────────────────────────────────────────────────────┐
│                     HybridLanguageModel.score_sequence()                 │
│                                                                         │
│  ┌─────────────────────────┐     ┌─────────────────────────────────┐   │
│  │  Check LRU Cache        │────►│  Cache Hit: Return cached score │   │
│  └───────────┬─────────────┘     └─────────────────────────────────┘   │
│              │                                                          │
│              │ Cache Miss                                               │
│              ▼                                                          │
│  ┌─────────────────────────────────────────────────────────────────┐   │
│  │  For each token in sequence:                                     │   │
│  │                                                                  │   │
│  │  ┌───────────────────────────────────────────────────────────┐  │   │
│  │  │  log P(token | context) =                                  │  │   │
│  │  │    λ₁ * ngram.log_prob(token, context) +                   │  │   │
│  │  │    λ₂ * embedding.similarity_score(token, context)         │  │   │
│  │  └───────────────────────────────────────────────────────────┘  │   │
│  │                                                                  │   │
│  └─────────────────────────────────────────────────────────────────┘   │
│              │                                                          │
│              ▼                                                          │
│  ┌─────────────────────────┐                                           │
│  │  Sum log probabilities  │                                           │
│  │  Cache result           │                                           │
│  │  Return total           │                                           │
│  └─────────────────────────┘                                           │
│                                                                         │
└─────────────────────────────────────────────────────────────────────────┘
Output: log P(sequence) = -23.5
```

## Key Abstractions

### MutableMappedDictionary (from liblevenshtein)

libgrammstein stores N-grams in trie dictionaries provided by liblevenshtein:

```rust
pub trait MutableMappedDictionary<Value>: Dictionary {
    /// Insert a key-value pair
    fn insert_with_value(&mut self, key: &str, value: Value);

    /// Update existing or insert new
    fn update_or_insert<F>(&mut self, key: &str, default: Value, f: F)
    where F: FnOnce(&mut Value);

    /// Get value by key
    fn get_value(&self, key: &str) -> Option<&Value>;
}
```

N-grams are stored as pipe-separated strings: `"the|quick|brown"` → `NgramEntry { count: 42, ... }`

### LanguageModel Trait (from lling-llang)

libgrammstein implements lling-llang's `LanguageModel` trait for integration:

```rust
pub trait LanguageModel: Send + Sync {
    /// Score a complete sequence: log P(w₁, w₂, ..., wₙ)
    fn score_sequence(&self, tokens: &[&str]) -> f64;

    /// Score a continuation: log P(next | prefix)
    fn score_continuation(&self, prefix: &[&str], next: &str) -> f64;
}
```

This trait uses `&[&str]` (strings), not vocabulary IDs, ensuring libgrammstein doesn't need to know about lling-llang's lattice internals.

## Design Decisions

### Why Hybrid Models?

| Model Type | Strengths | Weaknesses |
|------------|-----------|------------|
| N-gram | Precise local context, fast lookup, well-understood | OOV problem, sparse data for long contexts |
| Embeddings | Semantic similarity, handles OOV via subwords | Ignores word order, slower |
| **Hybrid** | Best of both: local precision + semantic coverage | Slightly more complex |

### Why liblevenshtein for Storage?

1. **Trie Structure**: Natural fit for N-gram prefix matching
2. **Multiple Backends**: DynamicDawg (mutable), PathMap (distributed), DoubleArray (fast)
3. **DictZipper**: Efficient traversal for backoff computation
4. **Existing Integration**: Already used by lling-llang for spelling correction

### Why Rayon for Parallelism?

Language model training is CPU-bound and embarrassingly parallel:
- Sentence processing is independent
- Skip-gram windows are independent
- Batch inference is independent

Rayon's work-stealing scheduler optimally utilizes all cores without the overhead of async runtimes.

### Why `&[&str]` in the Trait Interface?

The `LanguageModel` trait uses strings rather than vocabulary IDs because:
1. **Decoupling**: LM doesn't need to know about lattice vocabulary
2. **Simplicity**: No ID translation layer needed
3. **Flexibility**: Works with any tokenization scheme
4. **Testing**: Easier to test with literal strings

## Thread Safety

libgrammstein is designed for concurrent access:

| Component | Thread Safety Mechanism |
|-----------|------------------------|
| `NgramModel<D>` | `Arc<D>` where `D: Send + Sync` |
| `SubwordEmbedding` | Immutable + `Arc<DashMap>` cache |
| `HybridLanguageModel` | `Mutex<LruCache>` for hot cache |
| `HybridConfig` | Plain data (Copy) |

All models implement `Send + Sync`, satisfying lling-llang's requirements.

## Performance Characteristics

| Operation | Time Complexity | Notes |
|-----------|-----------------|-------|
| N-gram lookup | O(n) | n = N-gram order |
| Embedding lookup | O(d + s) | d = dimension, s = subwords |
| Hybrid score | O(n + d + s) | Cache amortizes repeated queries |
| Training (N-gram) | O(C × n) | C = corpus tokens |
| Training (Embedding) | O(C × w × d × e) | w = window, e = epochs |

## Memory Layout

```
NgramModel
├── dictionary: Arc<D>          # Trie with NgramEntry values
│   ├── "the" → NgramEntry { count: 1M, ... }
│   ├── "the|quick" → NgramEntry { count: 50K, ... }
│   └── "the|quick|brown" → NgramEntry { count: 5K, ... }
├── smoothing: KneserNeySmoothing
│   ├── d1: f64                 # Discount for count=1
│   ├── d2: f64                 # Discount for count=2
│   └── d3_plus: f64            # Discount for count>=3
└── vocab_size: usize

SubwordEmbedding
├── word_embeddings: Array2<f32>      # [vocab_size × dim]
├── subword_embeddings: Array2<f32>   # [bucket_count × dim]
├── word_to_idx: HashMap<String, usize>
├── idx_to_word: Vec<String>
└── cache: Arc<DashMap<String, Array1<f32>>>

HybridLanguageModel
├── ngram: NgramModel<D>
├── embedding: SubwordEmbedding
├── config: HybridConfig
│   ├── ngram_weight: f64       # λ₁
│   └── embedding_weight: f64   # λ₂
└── cache: Mutex<LruCache<CacheKey, f64>>
```

## Next Steps

- [Data Flow]data-flow.md: Detailed data flow through the system
- [Threading Model]threading-model.md: Concurrency patterns
- [N-gram Overview]../components/ngram/overview.md: N-gram model details
- [Neural Overview]../components/neural/overview.md: ModernBERT-based neural components
- [RAG Overview]../components/rag/overview.md: Document indexing and retrieval
- [Topic Overview]../components/topic/overview.md: BERTopic-style topic modeling
- [lling-llang Integration]../integration/lling-llang/overview.md: Integration architecture