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
//! Dictionary extraction from n-gram models.
//!
//! Extracts a `DoubleArrayTrieChar` dictionary from an existing n-gram model's 1-grams.
//! This is a post-processing step that can be run anytime after LM training.
//!
//! The dictionary can be used with `liblevenshtein` for fast lexical correction
//! and spelling suggestions.

use std::io::Write;
use std::path::Path;

use libdictenstein::double_array_trie_char::DoubleArrayTrieChar;
use libdictenstein::persistent_artrie_char::PersistentARTrieChar;

/// Check if a term is a unigram (single word without whitespace).
///
/// Also filters out MKN metadata entries (they start with \x00).
#[inline]
fn is_unigram(term: &str) -> bool {
    !term.starts_with('\x00') && !term.contains(char::is_whitespace)
}

/// Statistics from dictionary extraction.
#[derive(Clone, Debug, Default)]
pub struct ExtractionStats {
    /// Total 1-grams examined.
    pub total_unigrams: u64,

    /// 1-grams meeting frequency threshold.
    pub filtered_unigrams: u64,

    /// Final vocabulary size in dictionary.
    pub vocabulary_size: u64,

    /// Source model size in bytes.
    pub source_size_bytes: u64,

    /// Output dictionary size in bytes.
    pub dictionary_size_bytes: u64,

    /// Extraction time in seconds.
    pub elapsed_seconds: f64,

    /// Words extracted (alias for vocabulary_size for CLI compatibility).
    pub words_extracted: u64,

    /// Words filtered out (alias for filtered_unigrams for CLI compatibility).
    pub words_filtered: u64,

    /// Dictionary size in bytes (alias for dictionary_size_bytes for CLI compatibility).
    pub dict_size_bytes: u64,
}

/// Errors that can occur during extraction.
#[derive(Debug, thiserror::Error)]
pub enum ExtractionError {
    /// Model file not found.
    #[error("Model file not found: {0}")]
    ModelNotFound(String),

    /// No 1-grams found in model.
    #[error("No 1-grams found in model")]
    NoUnigrams,

    /// I/O error.
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// Serialization error.
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Dictionary construction error.
    #[error("Dictionary construction error: {0}")]
    Construction(String),
}

/// Dictionary extractor for building lexical dictionaries from n-gram models.
///
/// The extractor reads 1-grams from a trained PersistentARTrie model and builds
/// a `DoubleArrayTrieChar` for fast read-only lexical lookup.
///
/// # Use Cases
///
/// - Spelling correction with Levenshtein automata
/// - Word completion and suggestions
/// - Vocabulary validation
///
/// # Example
///
/// ```ignore
/// use libgrammstein::sources::google_books::DictionaryExtractor;
///
/// // Extract dictionary with minimum frequency threshold
/// let stats = DictionaryExtractor::extract_to_file(
///     "english.artrie",
///     "english.dict",
///     100,  // Only include words with count >= 100
/// )?;
///
/// println!("Extracted {} words", stats.vocabulary_size);
/// ```
pub struct DictionaryExtractor;

impl DictionaryExtractor {
    /// Extract dictionary from n-gram model's 1-grams.
    ///
    /// # Arguments
    ///
    /// * `model_path` - Path to PersistentARTrie n-gram model
    /// * `min_count` - Minimum frequency threshold (filters rare/misspelled words)
    ///
    /// # Returns
    ///
    /// A vector of words meeting the threshold, sorted alphabetically.
    pub fn extract_words<P: AsRef<Path>>(
        model_path: P,
        min_count: u64,
    ) -> Result<(Vec<String>, ExtractionStats), ExtractionError> {
        use std::time::Instant;

        let start = Instant::now();
        let model_path = model_path.as_ref();

        if !model_path.exists() {
            return Err(ExtractionError::ModelNotFound(
                model_path.display().to_string(),
            ));
        }

        let source_size = std::fs::metadata(model_path)?.len();

        log::info!(
            "Extracting dictionary from {:?} with min_count={}",
            model_path,
            min_count
        );

        // Open the disk-backed trie
        let trie: PersistentARTrieChar<u64> =
            PersistentARTrieChar::open(model_path).map_err(|e| {
                ExtractionError::Io(std::io::Error::other(format!("Failed to open trie: {}", e)))
            })?;

        // Collect unigrams using the public iteration API
        let mut words = Vec::new();
        let mut total_unigrams = 0u64;
        let mut filtered_unigrams = 0u64;

        // Use iter_with_values to iterate all entries
        for (term, count) in trie.iter_with_values() {
            // Check if this is a unigram (no whitespace = single word)
            if is_unigram(&term) {
                total_unigrams += 1;

                if count >= min_count {
                    words.push(term);
                } else {
                    filtered_unigrams += 1;
                }
            }
        }

        // Sort words alphabetically
        words.sort();

        log::info!(
            "Extracted {} words from {} unigrams ({} filtered) in {:.2}s",
            words.len(),
            total_unigrams,
            filtered_unigrams,
            start.elapsed().as_secs_f64()
        );

        let stats = ExtractionStats {
            total_unigrams,
            filtered_unigrams,
            vocabulary_size: words.len() as u64,
            source_size_bytes: source_size,
            dictionary_size_bytes: 0,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_extracted: words.len() as u64,
            words_filtered: filtered_unigrams,
            dict_size_bytes: 0,
        };

        Ok((words, stats))
    }

    /// Extract dictionary and save to disk.
    ///
    /// # Arguments
    ///
    /// * `model_path` - Path to PersistentARTrie n-gram model
    /// * `output_path` - Output path for dictionary file
    /// * `min_count` - Minimum frequency threshold
    pub fn extract_to_file<P: AsRef<Path>>(
        model_path: P,
        output_path: P,
        min_count: u64,
    ) -> Result<ExtractionStats, ExtractionError> {
        use std::time::Instant;

        let start = Instant::now();
        let output_path = output_path.as_ref();

        // Extract words from the model
        let (words, mut stats) = Self::extract_words(&model_path, min_count)?;

        if words.is_empty() {
            return Err(ExtractionError::NoUnigrams);
        }

        log::info!("Building DoubleArrayTrieChar from {} words", words.len());

        // Build DoubleArrayTrieChar from the sorted word list
        // DoubleArrayTrieChar::from_terms handles sorting and deduplication internally
        let dict: DoubleArrayTrieChar<()> = DoubleArrayTrieChar::from_terms(&words);

        log::info!("Serializing dictionary to {:?}", output_path);

        // Serialize to bytes using bincode
        let bytes = bincode::serialize(&dict).map_err(|e| {
            ExtractionError::Serialization(format!("Failed to serialize dictionary: {}", e))
        })?;

        // Write to file
        let mut file = std::fs::File::create(output_path)?;
        file.write_all(&bytes)?;
        file.flush()?;

        // Update stats with dictionary size
        let dictionary_size = bytes.len() as u64;
        stats.dictionary_size_bytes = dictionary_size;
        stats.dict_size_bytes = dictionary_size;
        stats.elapsed_seconds = start.elapsed().as_secs_f64();

        log::info!(
            "Dictionary extraction complete: {} words, {} bytes in {:.2}s",
            stats.vocabulary_size,
            stats.dictionary_size_bytes,
            stats.elapsed_seconds
        );

        Ok(stats)
    }

    /// Extract dictionary with progress callback.
    ///
    /// # Arguments
    ///
    /// * `model_path` - Path to PersistentARTrie n-gram model
    /// * `output_path` - Output path for dictionary file
    /// * `min_count` - Minimum frequency threshold
    /// * `progress` - Callback for progress updates
    pub fn extract_with_progress<P1, P2, F>(
        model_path: P1,
        output_path: P2,
        min_count: u64,
        mut progress: F,
    ) -> Result<ExtractionStats, ExtractionError>
    where
        P1: AsRef<Path>,
        P2: AsRef<Path>,
        F: FnMut(ExtractionProgress),
    {
        use std::time::Instant;

        let start = Instant::now();
        let model_path = model_path.as_ref();
        let output_path = output_path.as_ref();

        if !model_path.exists() {
            return Err(ExtractionError::ModelNotFound(
                model_path.display().to_string(),
            ));
        }

        let source_size = std::fs::metadata(model_path)?.len();

        // Phase 1: Loading
        progress(ExtractionProgress {
            phase: ExtractionPhase::Loading,
            items_processed: 0,
            items_total: None,
            items_accepted: 0,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_processed: 0,
        });

        // Open the disk-backed trie
        let trie: PersistentARTrieChar<u64> =
            PersistentARTrieChar::open(model_path).map_err(|e| {
                ExtractionError::Io(std::io::Error::other(format!("Failed to open trie: {}", e)))
            })?;

        // Phase 2: Filtering - iterate trie and collect unigrams with progress
        let mut words = Vec::new();
        let mut total_unigrams = 0u64;
        let mut filtered_unigrams = 0u64;
        let mut last_progress = 0u64;

        // Use iter_with_values to iterate all entries
        for (term, count) in trie.iter_with_values() {
            // Check if this is a unigram (no whitespace = single word)
            if is_unigram(&term) {
                total_unigrams += 1;

                if count >= min_count {
                    words.push(term);
                } else {
                    filtered_unigrams += 1;
                }

                // Report progress every 100k words
                if total_unigrams - last_progress >= 100_000 {
                    last_progress = total_unigrams;
                    progress(ExtractionProgress {
                        phase: ExtractionPhase::Filtering,
                        items_processed: total_unigrams,
                        items_total: None,
                        items_accepted: words.len() as u64,
                        elapsed_seconds: start.elapsed().as_secs_f64(),
                        words_processed: total_unigrams,
                    });
                }
            }
        }

        // Sort words alphabetically
        words.sort();

        if words.is_empty() {
            return Err(ExtractionError::NoUnigrams);
        }

        // Phase 3: Building
        progress(ExtractionProgress {
            phase: ExtractionPhase::Building,
            items_processed: total_unigrams,
            items_total: Some(total_unigrams),
            items_accepted: words.len() as u64,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_processed: total_unigrams,
        });

        // Build DoubleArrayTrieChar from the sorted word list
        let dict: DoubleArrayTrieChar<()> = DoubleArrayTrieChar::from_terms(&words);

        // Phase 4: Saving
        progress(ExtractionProgress {
            phase: ExtractionPhase::Saving,
            items_processed: total_unigrams,
            items_total: Some(total_unigrams),
            items_accepted: words.len() as u64,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_processed: total_unigrams,
        });

        // Serialize and write to file
        let bytes = bincode::serialize(&dict).map_err(|e| {
            ExtractionError::Serialization(format!("Failed to serialize dictionary: {}", e))
        })?;

        let mut file = std::fs::File::create(output_path)?;
        file.write_all(&bytes)?;
        file.flush()?;

        let dictionary_size = bytes.len() as u64;

        // Phase 5: Complete
        progress(ExtractionProgress {
            phase: ExtractionPhase::Complete,
            items_processed: total_unigrams,
            items_total: Some(total_unigrams),
            items_accepted: words.len() as u64,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_processed: total_unigrams,
        });

        let stats = ExtractionStats {
            total_unigrams,
            filtered_unigrams,
            vocabulary_size: words.len() as u64,
            source_size_bytes: source_size,
            dictionary_size_bytes: dictionary_size,
            elapsed_seconds: start.elapsed().as_secs_f64(),
            words_extracted: words.len() as u64,
            words_filtered: filtered_unigrams,
            dict_size_bytes: dictionary_size,
        };

        Ok(stats)
    }

    /// Extract dictionary to file with progress callback.
    ///
    /// Alias for `extract_with_progress` for CLI compatibility.
    pub fn extract_to_file_with_progress<P1, P2, F>(
        model_path: P1,
        output_path: P2,
        min_count: u64,
        progress: F,
    ) -> Result<ExtractionStats, ExtractionError>
    where
        P1: AsRef<Path>,
        P2: AsRef<Path>,
        F: FnMut(ExtractionProgress),
    {
        Self::extract_with_progress(model_path, output_path, min_count, progress)
    }
}

/// Progress information for dictionary extraction.
#[derive(Clone, Debug)]
pub struct ExtractionProgress {
    /// Current extraction phase.
    pub phase: ExtractionPhase,

    /// Items processed so far.
    pub items_processed: u64,

    /// Total items (if known).
    pub items_total: Option<u64>,

    /// Items accepted (meeting threshold).
    pub items_accepted: u64,

    /// Elapsed time in seconds.
    pub elapsed_seconds: f64,

    /// Words processed (alias for items_processed for CLI compatibility).
    pub words_processed: u64,
}

/// Extraction phase.
#[derive(Clone, Debug, PartialEq)]
pub enum ExtractionPhase {
    /// Loading the n-gram model.
    Loading,
    /// Filtering 1-grams by frequency.
    Filtering,
    /// Building the DoubleArrayTrieChar.
    Building,
    /// Saving to disk.
    Saving,
    /// Complete.
    Complete,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extraction_stats_default() {
        let stats = ExtractionStats::default();
        assert_eq!(stats.total_unigrams, 0);
        assert_eq!(stats.vocabulary_size, 0);
    }

    #[test]
    fn test_extraction_model_not_found() {
        let result = DictionaryExtractor::extract_words("/nonexistent/path.artrie", 100);
        assert!(matches!(result, Err(ExtractionError::ModelNotFound(_))));
    }

    #[test]
    fn test_extraction_progress() {
        let progress = ExtractionProgress {
            phase: ExtractionPhase::Filtering,
            items_processed: 1000,
            items_total: Some(10000),
            items_accepted: 500,
            elapsed_seconds: 1.5,
            words_processed: 1000,
        };

        assert_eq!(progress.phase, ExtractionPhase::Filtering);
        assert_eq!(progress.items_processed, 1000);
        assert_eq!(progress.items_accepted, 500);
    }

    #[test]
    fn test_is_unigram() {
        // Valid unigrams
        assert!(is_unigram("hello"));
        assert!(is_unigram("world123"));
        assert!(is_unigram("café"));

        // Invalid: contains whitespace (n-grams)
        assert!(!is_unigram("hello world"));
        assert!(!is_unigram("the quick"));

        // Invalid: metadata entries
        assert!(!is_unigram("\x00metadata"));
        assert!(!is_unigram("\x00__checkpoint__"));
    }
}