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
# Example: Domain Adaptation

This example demonstrates adapting a general language model to a specific domain for improved performance.

## Why Domain Adaptation?

General-purpose models trained on broad corpora may perform poorly on specialized text:

| Domain | Challenge |
|--------|-----------|
| Medical | Technical terminology, drug names |
| Legal | Formal language, Latin phrases |
| Technical | Code snippets, API names |
| Social Media | Slang, abbreviations, hashtags |

Domain adaptation improves model performance by:
1. Training on domain-specific data
2. Interpolating domain and general models
3. Fine-tuning existing models

## Setup

```toml
[dependencies]
libgrammstein = { version = "0.1", features = ["serde-extras"] }
liblevenshtein = "0.6"
```

## Implementation

```rust
use libgrammstein::ngram::{NgramModel, TrainerBuilder, NgramEntry};
use libgrammstein::embedding::EmbeddingTrainerBuilder;
use libgrammstein::hybrid::{HybridLanguageModel, HybridConfig, InterpolationStrategy};
use libgrammstein::corpus::PlaintextReader;
use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;

/// Domain-adapted language model
struct DomainAdaptedModel<D>
where
    D: liblevenshtein::dictionary::MutableMappedDictionary<Value = NgramEntry> + Send + Sync + Clone,
{
    general_model: HybridLanguageModel<D>,
    domain_model: HybridLanguageModel<D>,
    lambda: f64,  // Interpolation weight (0 = general, 1 = domain)
}

impl<D> DomainAdaptedModel<D>
where
    D: liblevenshtein::dictionary::MutableMappedDictionary<Value = NgramEntry> + Send + Sync + Clone,
{
    /// Create a domain-adapted model
    fn new(
        general_model: HybridLanguageModel<D>,
        domain_model: HybridLanguageModel<D>,
        lambda: f64,
    ) -> Self {
        Self {
            general_model,
            domain_model,
            lambda: lambda.clamp(0.0, 1.0),
        }
    }

    /// Score with domain adaptation
    fn score(&self, word: &str, context: &[&str]) -> f64 {
        let general_score = self.general_model.score(word, context);
        let domain_score = self.domain_model.score(word, context);

        // Linear interpolation in log space
        let general_prob = general_score.exp();
        let domain_prob = domain_score.exp();

        let combined = (1.0 - self.lambda) * general_prob + self.lambda * domain_prob;
        combined.ln()
    }

    /// Compute sentence log probability
    fn sentence_log_prob(&self, tokens: &[&str]) -> f64 {
        let order = self.domain_model.ngram_model().order();
        let mut total = 0.0;

        for (i, token) in tokens.iter().enumerate() {
            let context_start = i.saturating_sub(order - 1);
            let context = &tokens[context_start..i];
            total += self.score(token, context);
        }

        total
    }

    /// Tune lambda on held-out data
    fn tune_lambda(&mut self, dev_set: &[Vec<String>], steps: usize) -> f64 {
        let mut best_lambda = self.lambda;
        let mut best_perplexity = f64::INFINITY;

        for i in 0..=steps {
            self.lambda = i as f64 / steps as f64;
            let ppl = self.evaluate_perplexity(dev_set);

            if ppl < best_perplexity {
                best_perplexity = ppl;
                best_lambda = self.lambda;
            }
        }

        self.lambda = best_lambda;
        best_lambda
    }

    /// Evaluate perplexity on a dataset
    fn evaluate_perplexity(&self, sentences: &[Vec<String>]) -> f64 {
        let mut total_log_prob = 0.0;
        let mut total_tokens = 0usize;

        for sentence in sentences {
            let tokens: Vec<&str> = sentence.iter().map(|s| s.as_str()).collect();
            total_log_prob += self.sentence_log_prob(&tokens);
            total_tokens += tokens.len();
        }

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

fn main() -> libgrammstein::Result<()> {
    // ========================================
    // Step 1: Prepare Corpora
    // ========================================
    println!("=== Step 1: Preparing Corpora ===\n");

    // General corpus (news-like)
    let general_corpus = r#"
        The company announced quarterly earnings today.
        Stock prices rose after the news release.
        The market showed strong performance this week.
        Economic indicators suggest continued growth.
        Analysts predict favorable conditions ahead.
        The chairman addressed shareholders at the meeting.
        Revenue increased compared to last quarter.
        The board approved the new strategic plan.
        Consumer spending remains robust.
        International trade agreements were signed.
    "#;

    // Domain corpus (medical)
    let medical_corpus = r#"
        The patient presented with acute symptoms.
        Blood pressure was elevated at admission.
        Laboratory results indicated elevated glucose levels.
        The diagnosis confirmed type 2 diabetes.
        Treatment protocol includes insulin therapy.
        The patient responded well to medication.
        Follow-up examination showed improvement.
        Vital signs stabilized after intervention.
        The physician recommended lifestyle changes.
        Prognosis is favorable with continued treatment.
    "#;

    // Test corpus (medical domain)
    let test_corpus = r#"
        The patient's blood pressure normalized.
        Treatment showed positive results.
        Laboratory values improved significantly.
    "#;

    // Development set for tuning
    let dev_corpus = r#"
        Blood glucose levels decreased.
        The patient reported reduced symptoms.
    "#;

    println!("General corpus: ~{} sentences", general_corpus.lines().filter(|l| !l.trim().is_empty()).count());
    println!("Domain corpus:  ~{} sentences", medical_corpus.lines().filter(|l| !l.trim().is_empty()).count());

    // ========================================
    // Step 2: Train General Model
    // ========================================
    println!("\n=== Step 2: Training General Model ===\n");

    let reader = PlaintextReader::from_string(general_corpus);
    let general_ngram = TrainerBuilder::new(DynamicDawgChar::new())
        .order(3)
        .min_word_freq(1)
        .train(&reader)?;

    let reader = PlaintextReader::from_string(general_corpus);
    let general_embedding = EmbeddingTrainerBuilder::new()
        .dim(50)
        .min_count(1)
        .epochs(10)
        .train(&reader)?;

    let general_config = HybridConfig {
        strategy: InterpolationStrategy::Linear { alpha: 0.7 },
        ..Default::default()
    };
    let general_model = HybridLanguageModel::new(
        general_ngram,
        general_embedding,
        general_config
    );

    println!("General model vocabulary: {}", general_model.ngram_model().vocab_size());

    // ========================================
    // Step 3: Train Domain Model
    // ========================================
    println!("\n=== Step 3: Training Domain Model ===\n");

    let reader = PlaintextReader::from_string(medical_corpus);
    let domain_ngram = TrainerBuilder::new(DynamicDawgChar::new())
        .order(3)
        .min_word_freq(1)
        .train(&reader)?;

    let reader = PlaintextReader::from_string(medical_corpus);
    let domain_embedding = EmbeddingTrainerBuilder::new()
        .dim(50)
        .min_count(1)
        .epochs(10)
        .train(&reader)?;

    let domain_config = HybridConfig {
        strategy: InterpolationStrategy::Linear { alpha: 0.7 },
        ..Default::default()
    };
    let domain_model = HybridLanguageModel::new(
        domain_ngram,
        domain_embedding,
        domain_config
    );

    println!("Domain model vocabulary: {}", domain_model.ngram_model().vocab_size());

    // ========================================
    // Step 4: Create Domain-Adapted Model
    // ========================================
    println!("\n=== Step 4: Creating Domain-Adapted Model ===\n");

    let mut adapted_model = DomainAdaptedModel::new(
        general_model.clone(),
        domain_model.clone(),
        0.5,  // Initial lambda
    );

    println!("Initial lambda: {}", adapted_model.lambda);

    // ========================================
    // Step 5: Tune Interpolation Weight
    // ========================================
    println!("\n=== Step 5: Tuning Lambda ===\n");

    let reader = PlaintextReader::from_string(dev_corpus);
    let dev_sentences: Vec<Vec<String>> = reader.sentences()
        .map(|s| s.split_whitespace().map(|w| w.to_lowercase()).collect())
        .collect();

    let optimal_lambda = adapted_model.tune_lambda(&dev_sentences, 10);
    println!("Optimal lambda: {:.2}", optimal_lambda);

    // ========================================
    // Step 6: Evaluate Models
    // ========================================
    println!("\n=== Step 6: Model Comparison ===\n");

    let reader = PlaintextReader::from_string(test_corpus);
    let test_sentences: Vec<Vec<String>> = reader.sentences()
        .map(|s| s.split_whitespace().map(|w| w.to_lowercase()).collect())
        .collect();

    // Evaluate each model
    let general_ppl = evaluate_model(&general_model, &test_sentences);
    let domain_ppl = evaluate_model(&domain_model, &test_sentences);
    let adapted_ppl = adapted_model.evaluate_perplexity(&test_sentences);

    println!("{:<25} {:>12}", "Model", "Perplexity");
    println!("{}", "-".repeat(39));
    println!("{:<25} {:>12.2}", "General only", general_ppl);
    println!("{:<25} {:>12.2}", "Domain only", domain_ppl);
    println!(
        "{:<25} {:>12.2}",
        format!("Adapted (λ={:.2})", adapted_model.lambda),
        adapted_ppl
    );

    // Show improvement
    let improvement = (general_ppl - adapted_ppl) / general_ppl * 100.0;
    println!("\nImprovement over general: {:.1}%", improvement);

    // ========================================
    // Step 7: Per-Word Analysis
    // ========================================
    println!("\n=== Step 7: Per-Word Analysis ===\n");

    let domain_words = ["patient", "blood", "treatment", "symptoms", "diagnosis"];
    let general_words = ["company", "market", "stock", "revenue", "growth"];

    println!("{:<15} {:>12} {:>12} {:>12}", "Word", "General", "Domain", "Adapted");
    println!("{}", "-".repeat(53));

    for word in domain_words.iter().chain(general_words.iter()) {
        let context = ["the"];
        let g_score = general_model.score(word, &context);
        let d_score = domain_model.score(word, &context);
        let a_score = adapted_model.score(word, &context);

        println!(
            "{:<15} {:>12.4} {:>12.4} {:>12.4}",
            word, g_score, d_score, a_score
        );
    }

    // ========================================
    // Step 8: Vocabulary Coverage Analysis
    // ========================================
    println!("\n=== Step 8: Vocabulary Coverage ===\n");

    let test_words: Vec<&str> = test_sentences.iter()
        .flat_map(|s| s.iter().map(|w| w.as_str()))
        .collect();

    let unique_words: std::collections::HashSet<&str> = test_words.iter().copied().collect();

    let general_coverage = unique_words.iter()
        .filter(|w| general_model.ngram_model().in_vocabulary(w))
        .count();

    let domain_coverage = unique_words.iter()
        .filter(|w| domain_model.ngram_model().in_vocabulary(w))
        .count();

    println!("Unique test words: {}", unique_words.len());
    println!(
        "General model coverage: {} ({:.1}%)",
        general_coverage,
        general_coverage as f64 / unique_words.len() as f64 * 100.0
    );
    println!(
        "Domain model coverage:  {} ({:.1}%)",
        domain_coverage,
        domain_coverage as f64 / unique_words.len() as f64 * 100.0
    );

    println!("\n=== Domain Adaptation Complete ===");

    Ok(())
}

/// Evaluate perplexity for a hybrid model
fn evaluate_model<D>(
    model: &HybridLanguageModel<D>,
    sentences: &[Vec<String>],
) -> f64
where
    D: liblevenshtein::dictionary::MutableMappedDictionary<Value = NgramEntry> + Send + Sync,
{
    let mut total_log_prob = 0.0;
    let mut total_tokens = 0usize;

    for sentence in sentences {
        let tokens: Vec<&str> = sentence.iter().map(|s| s.as_str()).collect();
        total_log_prob += model.sentence_log_prob(&tokens);
        total_tokens += tokens.len();
    }

    (-total_log_prob / total_tokens as f64).exp()
}
```

## Expected Output

```
=== Step 1: Preparing Corpora ===

General corpus: ~10 sentences
Domain corpus:  ~10 sentences

=== Step 2: Training General Model ===

General model vocabulary: 42

=== Step 3: Training Domain Model ===

Domain model vocabulary: 38

=== Step 4: Creating Domain-Adapted Model ===

Initial lambda: 0.5

=== Step 5: Tuning Lambda ===

Optimal lambda: 0.80

=== Step 6: Model Comparison ===

Model                      Perplexity
---------------------------------------
General only                     89.45
Domain only                      28.67
Adapted (λ=0.80)                 25.34

Improvement over general: 71.7%

=== Step 7: Per-Word Analysis ===

Word              General       Domain      Adapted
-----------------------------------------------------
patient           -7.2345      -2.3456      -3.4567
blood             -6.8901      -2.1234      -3.2345
treatment         -6.5678      -1.8901      -2.9012
symptoms          -7.1234      -2.4567      -3.5678
diagnosis         -7.4567      -2.6789      -3.7890
company           -2.3456      -6.7890      -5.6789
market            -2.1234      -6.5678      -5.4567
stock             -2.4567      -6.8901      -5.7890
revenue           -2.6789      -7.1234      -6.0123
growth            -2.5678      -7.0123      -5.9012

=== Step 8: Vocabulary Coverage ===

Unique test words: 15
General model coverage: 8 (53.3%)
Domain model coverage:  13 (86.7%)

=== Domain Adaptation Complete ===
```

## Adaptation Strategies

### 1. Linear Interpolation (Shown Above)

```rust
P(w|c) = (1-λ) * P_general(w|c) + λ * P_domain(w|c)
```

Best for: Balanced adaptation when domain and general corpora are comparable.

### 2. Backoff Adaptation

```rust
fn score_backoff(&self, word: &str, context: &[&str]) -> f64 {
    // Use domain model if word is in domain vocabulary
    if self.domain_model.ngram_model().in_vocabulary(word) {
        self.domain_model.score(word, context)
    } else {
        // Fall back to general model for OOV
        self.general_model.score(word, context)
    }
}
```

Best for: Domain corpus has good coverage of domain-specific terms.

### 3. Dynamic Lambda

```rust
fn score_dynamic(&self, word: &str, context: &[&str]) -> f64 {
    // Compute domain confidence based on context
    let domain_confidence = self.estimate_domain_confidence(context);

    let general_prob = self.general_model.score(word, context).exp();
    let domain_prob = self.domain_model.score(word, context).exp();

    (domain_confidence * domain_prob + (1.0 - domain_confidence) * general_prob).ln()
}

fn estimate_domain_confidence(&self, context: &[&str]) -> f64 {
    // Count how many context words are domain-specific
    let domain_count = context.iter()
        .filter(|w| self.is_domain_word(w))
        .count();

    (domain_count as f64 / context.len().max(1) as f64).min(0.9)
}
```

Best for: Mixed-domain text where domain shifts within document.

## Training Strategies

### Small Domain Corpus

When domain data is limited:

```rust
// Use higher n-gram orders from general model
let general_ngram = TrainerBuilder::new(DynamicDawgChar::new())
    .order(4)  // Higher order
    .train(&general_reader)?;

let domain_ngram = TrainerBuilder::new(DynamicDawgChar::new())
    .order(2)  // Lower order for sparse data
    .train(&domain_reader)?;

// Weight toward general model
let adapted = DomainAdaptedModel::new(general, domain, 0.3);
```

### Large Domain Corpus

When domain data is abundant:

```rust
// Can use same order for both
let general_ngram = TrainerBuilder::new(DynamicDawgChar::new())
    .order(3)
    .train(&general_reader)?;

let domain_ngram = TrainerBuilder::new(DynamicDawgChar::new())
    .order(3)
    .min_word_freq(2)  // Can filter rare words
    .train(&domain_reader)?;

// Weight toward domain model
let adapted = DomainAdaptedModel::new(general, domain, 0.8);
```

### Incremental Adaptation

For streaming domain data:

```rust
// Start with general model only
let mut adapted = DomainAdaptedModel::new(general.clone(), general.clone(), 0.0);

// As domain data arrives, retrain domain model
for batch in domain_batches {
    let domain_model = train_on_batch(batch)?;
    adapted.domain_model = domain_model;
    adapted.lambda = adapted.tune_lambda(&dev_set, 10);

    println!("Batch processed, lambda: {:.2}", adapted.lambda);
}
```

## Evaluation Metrics

### Perplexity Reduction

```rust
let reduction = (general_ppl - adapted_ppl) / general_ppl * 100.0;
println!("Perplexity reduction: {:.1}%", reduction);
```

### OOV Rate Improvement

```rust
let general_oov = count_oov(&general_model, &test_words);
let adapted_oov = count_oov_combined(&adapted_model, &test_words);
println!("OOV reduction: {} -> {}", general_oov, adapted_oov);
```

### Domain-Specific Accuracy

```rust
// For domain terms, how often is domain model correct?
let domain_terms = ["patient", "diagnosis", "treatment"];
for term in domain_terms {
    let g_rank = get_prediction_rank(&general_model, term, context);
    let d_rank = get_prediction_rank(&adapted_model, term, context);
    println!("{}: rank {} -> {}", term, g_rank, d_rank);
}
```

## Best Practices

1. **Development Set**: Always use held-out domain data for tuning lambda.

2. **Vocabulary Analysis**: Check coverage before choosing adaptation strategy.

3. **Start Conservative**: Begin with low lambda (0.3) and increase based on evaluation.

4. **Monitor OOV**: Track out-of-vocabulary rate as a diagnostic.

5. **Regular Re-tuning**: Re-tune lambda as domain corpus grows.

## See Also

- [Train and Evaluate]train-and-evaluate.md - Basic workflow
- [Perplexity Scoring]perplexity-scoring.md - Evaluation metrics
- [Hyperparameters]../training/hyperparameters.md - Tuning guide