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
# N-gram Training Guide

This guide covers the complete workflow for training n-gram language models with libgrammstein.

## Overview

N-gram models estimate word probabilities based on preceding context. Training involves:

1. **Corpus preparation** - Loading and preprocessing text
2. **N-gram counting** - Counting word sequences in parallel
3. **Smoothing** - Computing Modified Kneser-Ney parameters
4. **Serialization** - Saving the trained model

## Quick Start

```rust
use libgrammstein::ngram::{TrainerBuilder, NgramEntry};
use libgrammstein::corpus::PlaintextReader;
use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;

// Load corpus
let reader = PlaintextReader::from_file("corpus.txt")?;

// Train 5-gram model
let dictionary = DynamicDawgChar::new();
let model = TrainerBuilder::new(dictionary)
    .order(5)
    .train(&reader)?;

// Save model
model.save("model.bin")?;
```

## Corpus Preparation

### Supported Formats

| Format | Reader | Best For |
|--------|--------|----------|
| Plain text | `PlaintextReader` | Simple text files |
| Wikipedia | `WikipediaReader` | Large-scale training |
| Gutenberg | `GutenbergReader` | Book corpora |

### Plain Text

```rust
use libgrammstein::corpus::PlaintextReader;

// Single file
let reader = PlaintextReader::from_file("corpus.txt")?;

// Directory of files
let reader = PlaintextReader::from_directory("corpus/")?;

// In-memory string
let text = "The quick brown fox. The lazy dog.";
let reader = PlaintextReader::from_string(text);
```

### Wikipedia

```rust
use libgrammstein::corpus::{WikipediaReader, WikipediaConfig};

// Basic usage
let reader = WikipediaReader::from_dump("enwiki-latest.xml.bz2")?;

// With configuration
let config = WikipediaConfig {
    max_articles: Some(100_000),  // Limit articles
    skip_redirects: true,
    skip_disambiguation: true,
    ..Default::default()
};
let reader = WikipediaReader::from_dump_with_config("enwiki.xml.bz2", config)?;

// HTTP streaming (large dumps without downloading)
let reader = WikipediaReader::from_url(
    "https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2"
)?;
```

### Preprocessing Pipeline

Apply quality filtering and normalization:

```rust
use libgrammstein::corpus::{
    PlaintextReader, QualityFilterBuilder, DeduplicatorBuilder,
    TextPreprocessorBuilder, PreprocessingPipelineBuilder
};

// Build pipeline
let filter = QualityFilterBuilder::new()
    .min_words(5)
    .max_word_repetition(0.3)
    .build();

let dedup = DeduplicatorBuilder::new()
    .mode(DeduplicationMode::Exact)
    .build();

let preprocessor = TextPreprocessorBuilder::new()
    .normalize_numbers(true)
    .normalize_urls(true)
    .build();

// Apply to corpus (use filtered sentences for training)
let reader = PlaintextReader::from_file("corpus.txt")?;
let filtered: Vec<String> = reader.sentences()
    .filter(|s| filter.accept(s))
    .filter(|s| dedup.is_unique(s))
    .map(|s| preprocessor.process(&s))
    .collect();
```

## Training Configuration

### N-gram Order

The order determines the maximum context length:

| Order | Name | Context | Example |
|-------|------|---------|---------|
| 1 | Unigram | 0 words | P(fox) |
| 2 | Bigram | 1 word | P(fox\|brown) |
| 3 | Trigram | 2 words | P(fox\|quick brown) |
| 5 | 5-gram | 4 words | P(fox\|the quick brown) |

**Recommendation:** Order 5 is a good default. Higher orders need more data.

### Minimum Word Frequency

Filter rare words to reduce model size:

```rust
let model = TrainerBuilder::new(dictionary)
    .order(5)
    .min_word_freq(5)  // Ignore words appearing < 5 times
    .train(&reader)?;
```

### Batch Size

Control parallel processing granularity:

```rust
let model = TrainerBuilder::new(dictionary)
    .order(5)
    .batch_size(10000)  // Process 10k sentences per batch
    .train(&reader)?;
```

**Recommendation:** Larger batches (10k-100k) are more efficient.

## Dictionary Backend Selection

| Backend | Memory | Speed | Updates | Best For |
|---------|--------|-------|---------|----------|
| `DynamicDawgChar` | Low | Good | Yes | General use |
| `PathMapDictionary` | High | Fast | Yes | Small models |
| `DoubleArrayTrieChar` | Low | Fastest | No | Production |

### DynamicDawgChar (Recommended)

```rust
use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;

let dictionary = DynamicDawgChar::<NgramEntry>::new();
```

Good compression, supports incremental updates.

### PathMapDictionary

```rust
use liblevenshtein::dictionary::pathmap::PathMapDictionary;

let dictionary = PathMapDictionary::<NgramEntry>::new();
```

Simple hash-based storage. Good for debugging.

### DoubleArrayTrieChar

```rust
use liblevenshtein::dictionary::double_array_trie_char::DoubleArrayTrieChar;

let dictionary = DoubleArrayTrieChar::<NgramEntry>::new();
```

Fastest lookups but no updates after construction.

## Progress Monitoring

### Console Progress

```rust
use crossbeam_channel::bounded;
use std::thread;

let (tx, rx) = bounded(100);

// Progress monitor thread
thread::spawn(move || {
    while let Ok(progress) = rx.recv() {
        println!(
            "\rSentences: {} | N-grams: {} | Time: {:.1}s",
            progress.sentences_processed,
            progress.ngrams_counted,
            progress.elapsed_secs
        );
    }
    println!();
});

// Train with progress
let trainer = NgramTrainer::new(dictionary, TrainingConfig::new(5));
let model = trainer.train_with_progress(&reader, tx)?;
```

### With Progress Bar (indicatif)

```rust
use indicatif::{ProgressBar, ProgressStyle};

let pb = ProgressBar::new(total_sentences as u64);
pb.set_style(ProgressStyle::default_bar()
    .template("{spinner:.green} [{bar:40.cyan/blue}] {pos}/{len} ({eta})")
    .progress_chars("#>-"));

thread::spawn(move || {
    while let Ok(progress) = rx.recv() {
        pb.set_position(progress.sentences_processed);
    }
    pb.finish_with_message("Training complete");
});
```

## Checkpointing

For long training runs, save periodic checkpoints:

```rust
use std::time::{Duration, Instant};

let checkpoint_interval = Duration::from_secs(300);  // Every 5 minutes
let mut last_checkpoint = Instant::now();

// During training callback
if last_checkpoint.elapsed() > checkpoint_interval {
    model.save("checkpoint.bin")?;
    last_checkpoint = Instant::now();
    log::info!("Checkpoint saved");
}
```

## Model Evaluation

### Perplexity

```rust
fn evaluate_perplexity(model: &NgramModel<D>, test_corpus: &impl CorpusReader) -> f64 {
    let mut total_log_prob = 0.0;
    let mut total_words = 0usize;

    for sentence in test_corpus.sentences() {
        let tokens: Vec<&str> = sentence.split_whitespace().collect();
        total_log_prob += model.sentence_log_prob(&tokens);
        total_words += tokens.len();
    }

    (-total_log_prob / total_words as f64).exp()
}

let test_reader = PlaintextReader::from_file("test.txt")?;
let ppl = evaluate_perplexity(&model, &test_reader);
println!("Test perplexity: {:.2}", ppl);
```

### Coverage

```rust
fn vocabulary_coverage(model: &NgramModel<D>, test_corpus: &impl CorpusReader) -> f64 {
    let mut known = 0usize;
    let mut total = 0usize;

    for sentence in test_corpus.sentences() {
        for word in sentence.split_whitespace() {
            total += 1;
            if model.in_vocabulary(word) {
                known += 1;
            }
        }
    }

    known as f64 / total as f64
}
```

## Memory Optimization

### For Large Corpora

1. **Stream the corpus** instead of loading all into memory:
   ```rust
   // WikipediaReader streams by default
   let reader = WikipediaReader::from_dump("enwiki.xml.bz2")?;
   ```

2. **Use memory-efficient dictionary**:
   ```rust
   let dictionary = DynamicDawgChar::new();  // Best compression
   ```

3. **Filter rare words**:
   ```rust
   .min_word_freq(5)  // Removes rare n-grams
   ```

4. **Lower n-gram order**:
   ```rust
   .order(3)  // Trigrams use less memory than 5-grams
   ```

### Memory Estimates

| Corpus Size | Order | Approx. Memory |
|-------------|-------|----------------|
| 1M sentences | 3 | ~500 MB |
| 1M sentences | 5 | ~1.5 GB |
| 10M sentences | 5 | ~10 GB |
| 100M sentences | 5 | ~50+ GB |

## Serialization

### Binary Format

Fast, compact, requires same dictionary type:

```rust
// Save
model.save("model.bin")?;

// Load (must specify dictionary type)
let loaded: NgramModel<DynamicDawgChar<NgramEntry>> =
    NgramModel::load("model.bin")?;
```

### Portable Format

Works with any dictionary backend:

```rust
// Save portable
model.save_portable("model.portable.bin")?;

// Load with different backend
let loaded = NgramModel::load_portable(
    "model.portable.bin",
    || DoubleArrayTrieChar::new()  // Different backend!
)?;
```

## CLI Training

Use the grammstein CLI for quick training:

```bash
# Train 5-gram model
grammstein train ngram corpus.txt model.bin --order 5

# With checkpoints
grammstein train ngram large-corpus.txt model.bin \
    --order 5 \
    --checkpoint ./checkpoints \
    --checkpoint-interval 100000

# Resume from checkpoint
grammstein train ngram large-corpus.txt model.bin \
    --resume ./checkpoints/latest.ckpt

# From Wikipedia dump
grammstein train ngram enwiki.xml.bz2 model.bin --order 5
```

## Complete Example

```rust
use libgrammstein::ngram::{NgramModel, TrainerBuilder, NgramEntry};
use libgrammstein::corpus::{WikipediaReader, WikipediaConfig};
use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;

fn main() -> libgrammstein::Result<()> {
    // Configure Wikipedia reader
    let config = WikipediaConfig {
        max_articles: Some(100_000),
        skip_redirects: true,
        ..Default::default()
    };
    let reader = WikipediaReader::from_dump_with_config("enwiki.xml.bz2", config)?;

    // Train with progress
    let dictionary = DynamicDawgChar::new();
    let model = TrainerBuilder::new(dictionary)
        .order(5)
        .min_word_freq(5)
        .batch_size(50_000)
        .train(&reader)?;

    println!("Vocabulary size: {}", model.vocab_size());
    println!("N-gram count: {}", model.ngram_count());

    // Evaluate
    let test = ["the", "quick", "brown", "fox"];
    let ppl = (-model.sentence_log_prob(&test) / test.len() as f64).exp();
    println!("Test perplexity: {:.2}", ppl);

    // Save
    model.save("wikipedia-5gram.bin")?;
    println!("Model saved");

    Ok(())
}
```

## See Also

- [NgramModel API]../api/ngram.md - Complete API reference
- [Hyperparameter Tuning]hyperparameters.md - Tuning guide
- [Large Corpora]large-corpora.md - Memory optimization