rustling 0.8.0

A blazingly fast library for computational linguistics
Documentation
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
//! N-gram counting.
//!
//! This module provides an n-gram counter for counting n-gram
//! frequencies from sequential data.

#[cfg(feature = "pyo3")]
mod py;

use crate::persistence::ModelError;
use crate::trie::CountTrie;

#[cfg(feature = "pyo3")]
pub use py::PyNgrams;
#[cfg(feature = "pyo3")]
pub(crate) use py::register_module;

// ---------------------------------------------------------------------------
// BaseNgrams
// ---------------------------------------------------------------------------

/// Core n-gram counting behavior with default implementations.
///
/// Implementors provide required methods that grant access to the
/// underlying storage. All counting and query logic is provided as defaults.
pub trait BaseNgrams: Sized + Clone {
    fn order(&self) -> usize;
    fn min_order(&self) -> usize;
    fn counts(&self) -> &CountTrie<String>;
    fn counts_mut(&mut self) -> &mut CountTrie<String>;
    fn totals(&self) -> &Vec<u64>;
    fn totals_mut(&mut self) -> &mut Vec<u64>;
    fn from_parts(
        order: usize,
        min_order: usize,
        counts: CountTrie<String>,
        totals: Vec<u64>,
    ) -> Self;

    // -----------------------------------------------------------------------
    // Counting
    // -----------------------------------------------------------------------

    /// Count n-grams from a single sequence.
    fn count(&mut self, seq: Vec<String>) {
        for k in self.min_order()..=self.order() {
            if seq.len() < k {
                continue;
            }
            let idx = k - self.min_order();
            for window in seq.windows(k) {
                self.counts_mut().increment(window.iter().cloned());
                self.totals_mut()[idx] += 1;
            }
        }
    }

    /// Count n-grams from multiple sequences.
    fn count_seqs(&mut self, seqs: Vec<Vec<String>>) {
        for seq in seqs {
            self.count(seq);
        }
    }

    // -----------------------------------------------------------------------
    // Queries
    // -----------------------------------------------------------------------

    /// Return the count for a specific n-gram.
    fn get(&self, ngram: Vec<String>) -> u64 {
        self.counts().get_count(ngram)
    }

    /// Validate that an order is within the valid range.
    fn validate_order(&self, order: Option<usize>) -> Result<(), ModelError> {
        if let Some(k) = order
            && (k < self.min_order() || k > self.order())
        {
            return Err(ModelError::ValidationError(format!(
                "order must be between {} and {}",
                self.min_order(),
                self.order()
            )));
        }
        Ok(())
    }

    /// Return the n most common n-grams with their counts.
    fn most_common_items(
        &self,
        n: Option<usize>,
        order: Option<usize>,
    ) -> Result<Vec<(Vec<String>, u64)>, ModelError> {
        self.validate_order(order)?;
        let mut pairs = self.counts().all_counts();
        if let Some(k) = order {
            pairs.retain(|(ngram, _)| ngram.len() == k);
        }
        pairs.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
        if let Some(limit) = n {
            pairs.truncate(limit);
        }
        Ok(pairs)
    }

    /// Return all (n-gram, count) pairs.
    fn items_list(&self, order: Option<usize>) -> Result<Vec<(Vec<String>, u64)>, ModelError> {
        self.validate_order(order)?;
        let pairs = self.counts().all_counts();
        match order {
            Some(k) => Ok(pairs
                .into_iter()
                .filter(|(ngram, _)| ngram.len() == k)
                .collect()),
            None => Ok(pairs),
        }
    }

    /// Return the total number of n-gram tokens counted.
    fn total(&self, order: Option<usize>) -> Result<u64, ModelError> {
        match order {
            None => Ok(self.totals().iter().sum()),
            Some(k) => {
                self.validate_order(Some(k))?;
                Ok(self.totals()[k - self.min_order()])
            }
        }
    }

    /// Number of unique n-grams.
    fn len(&self) -> usize {
        self.counts().len()
    }

    /// Whether no n-grams have been counted.
    fn is_empty(&self) -> bool {
        self.counts().len() == 0
    }

    /// Whether a specific n-gram has been observed.
    fn contains(&self, ngram: Vec<String>) -> bool {
        self.counts().get_count(ngram) > 0
    }

    /// Return all n-grams (without counts).
    fn all_ngrams(&self) -> Vec<Vec<String>> {
        self.counts()
            .all_counts()
            .into_iter()
            .map(|(ngram, _)| ngram)
            .collect()
    }

    /// Return a string representation.
    fn repr_string(&self) -> String {
        let total: u64 = self.totals().iter().sum();
        if self.min_order() == self.order() {
            format!(
                "Ngrams(n={}, unique={}, total={})",
                self.order(),
                self.counts().len(),
                total
            )
        } else {
            format!(
                "Ngrams(n={}, min_n={}, unique={}, total={})",
                self.order(),
                self.min_order(),
                self.counts().len(),
                total
            )
        }
    }

    /// Add two n-gram counters together, returning a new counter.
    fn add(&self, other: &Self) -> Result<Self, ModelError> {
        if self.order() != other.order() || self.min_order() != other.min_order() {
            return Err(ModelError::ValidationError(format!(
                "Cannot add Ngrams with different orders \
                 (n={}, min_n={}) vs (n={}, min_n={})",
                self.order(),
                self.min_order(),
                other.order(),
                other.min_order()
            )));
        }
        let mut result = self.clone();
        for (ngram, count) in other.counts().all_counts() {
            let idx = ngram.len() - self.min_order();
            for _ in 0..count {
                result.counts_mut().increment(ngram.iter().cloned());
            }
            result.totals_mut()[idx] += count;
        }
        Ok(result)
    }

    /// Add another n-gram counter into this one in-place.
    fn iadd(&mut self, other: &Self) -> Result<(), ModelError> {
        if self.order() != other.order() || self.min_order() != other.min_order() {
            return Err(ModelError::ValidationError(format!(
                "Cannot add Ngrams with different orders \
                 (n={}, min_n={}) vs (n={}, min_n={})",
                self.order(),
                self.min_order(),
                other.order(),
                other.min_order()
            )));
        }
        for (ngram, count) in other.counts().all_counts() {
            let idx = ngram.len() - self.min_order();
            for _ in 0..count {
                self.counts_mut().increment(ngram.iter().cloned());
            }
            self.totals_mut()[idx] += count;
        }
        Ok(())
    }

    /// Clear all counts.
    fn clear(&mut self) {
        self.counts_mut().clear();
        for t in self.totals_mut() {
            *t = 0;
        }
    }
}

// ---------------------------------------------------------------------------
// Ngrams (pure Rust)
// ---------------------------------------------------------------------------

/// An n-gram counter for counting n-gram frequencies.
///
/// Accumulates n-gram counts from sequences of elements. N-grams
/// do not cross sequence boundaries.
///
/// For Python, use [`PyNgrams`].
#[derive(Clone, Debug)]
pub struct Ngrams {
    order: usize,
    min_order: usize,
    pub(crate) counts: CountTrie<String>,
    pub(crate) totals: Vec<u64>,
}

impl BaseNgrams for Ngrams {
    fn order(&self) -> usize {
        self.order
    }
    fn min_order(&self) -> usize {
        self.min_order
    }
    fn counts(&self) -> &CountTrie<String> {
        &self.counts
    }
    fn counts_mut(&mut self) -> &mut CountTrie<String> {
        &mut self.counts
    }
    fn totals(&self) -> &Vec<u64> {
        &self.totals
    }
    fn totals_mut(&mut self) -> &mut Vec<u64> {
        &mut self.totals
    }
    fn from_parts(
        order: usize,
        min_order: usize,
        counts: CountTrie<String>,
        totals: Vec<u64>,
    ) -> Self {
        Self {
            order,
            min_order,
            counts,
            totals,
        }
    }
}

impl Ngrams {
    /// Create a new empty Ngrams.
    ///
    /// # Arguments
    ///
    /// * `n` - The n-gram order (1 for unigrams, 2 for bigrams, etc.). Must be >= 1.
    /// * `min_n` - Minimum n-gram order. Must be >= 1 and <= n. Defaults to n.
    pub fn new(n: usize, min_n: Option<usize>) -> Result<Self, ModelError> {
        if n < 1 {
            return Err(ModelError::ValidationError("n must be >= 1".to_string()));
        }
        let min_order = min_n.unwrap_or(n);
        if min_order < 1 {
            return Err(ModelError::ValidationError(
                "min_n must be >= 1".to_string(),
            ));
        }
        if min_order > n {
            return Err(ModelError::ValidationError(
                "min_n must be <= n".to_string(),
            ));
        }
        let num_orders = n - min_order + 1;
        Ok(Self {
            order: n,
            min_order,
            counts: CountTrie::new(),
            totals: vec![0u64; num_orders],
        })
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn total(counter: &Ngrams) -> u64 {
        counter.totals.iter().sum()
    }

    #[test]
    fn test_new_valid() {
        let counter = Ngrams::new(1, None).unwrap();
        assert_eq!(counter.order, 1);
        assert_eq!(counter.min_order, 1);
        assert_eq!(total(&counter), 0);

        let counter = Ngrams::new(3, None).unwrap();
        assert_eq!(counter.order, 3);
        assert_eq!(counter.min_order, 3);
    }

    #[test]
    fn test_new_invalid() {
        let result = Ngrams::new(0, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_new_with_min_n() {
        let counter = Ngrams::new(3, Some(1)).unwrap();
        assert_eq!(counter.order, 3);
        assert_eq!(counter.min_order, 1);
        assert_eq!(counter.totals.len(), 3);
    }

    #[test]
    fn test_new_min_n_defaults_to_n() {
        let counter = Ngrams::new(3, None).unwrap();
        assert_eq!(counter.min_order, 3);
        assert_eq!(counter.totals.len(), 1);
    }

    #[test]
    fn test_new_min_n_invalid() {
        assert!(Ngrams::new(3, Some(0)).is_err());
        assert!(Ngrams::new(3, Some(4)).is_err());
    }

    #[test]
    fn test_count_unigrams() {
        let mut counter = Ngrams::new(1, None).unwrap();
        counter.count(vec!["the".into(), "cat".into(), "sat".into(), "the".into()]);

        assert_eq!(counter.get(vec!["the".into()]), 2);
        assert_eq!(counter.get(vec!["cat".into()]), 1);
        assert_eq!(counter.get(vec!["sat".into()]), 1);
        assert_eq!(total(&counter), 4);
        assert_eq!(counter.counts.len(), 3);
    }

    #[test]
    fn test_count_bigrams() {
        let mut counter = Ngrams::new(2, None).unwrap();
        counter.count(vec![
            "the".into(),
            "cat".into(),
            "sat".into(),
            "the".into(),
            "cat".into(),
        ]);

        assert_eq!(counter.get(vec!["the".into(), "cat".into()]), 2);
        assert_eq!(counter.get(vec!["cat".into(), "sat".into()]), 1);
        assert_eq!(counter.get(vec!["sat".into(), "the".into()]), 1);
        assert_eq!(total(&counter), 4);
    }

    #[test]
    fn test_count_sentence_too_short() {
        let mut counter = Ngrams::new(3, None).unwrap();
        counter.count(vec!["the".into(), "cat".into()]);

        assert_eq!(total(&counter), 0);
        assert_eq!(counter.counts.len(), 0);
    }

    #[test]
    fn test_count_seqs() {
        let mut counter = Ngrams::new(1, None).unwrap();
        counter.count_seqs(vec![
            vec!["the".into(), "cat".into()],
            vec!["the".into(), "dog".into()],
        ]);

        assert_eq!(counter.get(vec!["the".into()]), 2);
        assert_eq!(counter.get(vec!["cat".into()]), 1);
        assert_eq!(counter.get(vec!["dog".into()]), 1);
        assert_eq!(total(&counter), 4);
    }

    #[test]
    fn test_count_no_cross_boundary() {
        let mut counter = Ngrams::new(2, None).unwrap();
        counter.count(vec!["a".into(), "b".into()]);
        counter.count(vec!["c".into(), "d".into()]);

        // "b c" should NOT exist since they come from separate count() calls
        assert_eq!(counter.get(vec!["b".into(), "c".into()]), 0);
        assert_eq!(counter.get(vec!["a".into(), "b".into()]), 1);
        assert_eq!(counter.get(vec!["c".into(), "d".into()]), 1);
    }

    #[test]
    fn test_get_missing() {
        let counter = Ngrams::new(1, None).unwrap();
        assert_eq!(counter.get(vec!["nonexistent".into()]), 0);
    }

    #[test]
    fn test_len() {
        let mut counter = Ngrams::new(1, None).unwrap();
        assert_eq!(counter.counts.len(), 0);

        counter.count(vec!["a".into(), "b".into(), "a".into()]);
        assert_eq!(counter.counts.len(), 2); // "a" and "b"
    }

    #[test]
    fn test_clear() {
        let mut counter = Ngrams::new(1, None).unwrap();
        counter.count(vec!["a".into(), "b".into()]);
        assert_eq!(total(&counter), 2);

        counter.clear();
        assert_eq!(total(&counter), 0);
        assert_eq!(counter.counts.len(), 0);
        assert_eq!(counter.get(vec!["a".into()]), 0);
    }

    #[test]
    fn test_merge_same_order() {
        let mut c1 = Ngrams::new(1, None).unwrap();
        c1.count(vec!["a".into(), "b".into()]);

        let mut c2 = Ngrams::new(1, None).unwrap();
        c2.count(vec!["b".into(), "c".into()]);

        let merged = c1.add(&c2).unwrap();
        assert_eq!(merged.get(vec!["a".into()]), 1);
        assert_eq!(merged.get(vec!["b".into()]), 2);
        assert_eq!(merged.get(vec!["c".into()]), 1);
        assert_eq!(total(&merged), 4);
    }

    #[test]
    fn test_merge_different_order_fails() {
        let c1 = Ngrams::new(1, None).unwrap();
        let c2 = Ngrams::new(2, None).unwrap();
        assert!(c1.add(&c2).is_err());
    }

    #[test]
    fn test_iadd() {
        let mut c1 = Ngrams::new(1, None).unwrap();
        c1.count(vec!["a".into()]);

        let mut c2 = Ngrams::new(1, None).unwrap();
        c2.count(vec!["a".into(), "b".into()]);

        c1.iadd(&c2).unwrap();
        assert_eq!(c1.get(vec!["a".into()]), 2);
        assert_eq!(c1.get(vec!["b".into()]), 1);
        assert_eq!(total(&c1), 3);
    }

    // All ngram tests

    #[test]
    fn test_count_all_ngrams() {
        let mut counter = Ngrams::new(3, Some(1)).unwrap();
        counter.count(vec!["a".into(), "b".into(), "c".into()]);

        // Unigrams
        assert_eq!(counter.get(vec!["a".into()]), 1);
        assert_eq!(counter.get(vec!["b".into()]), 1);
        assert_eq!(counter.get(vec!["c".into()]), 1);
        // Bigrams
        assert_eq!(counter.get(vec!["a".into(), "b".into()]), 1);
        assert_eq!(counter.get(vec!["b".into(), "c".into()]), 1);
        // Trigrams
        assert_eq!(counter.get(vec!["a".into(), "b".into(), "c".into()]), 1);

        // Per-order totals: 3 unigrams + 2 bigrams + 1 trigram
        assert_eq!(counter.totals[0], 3);
        assert_eq!(counter.totals[1], 2);
        assert_eq!(counter.totals[2], 1);
        assert_eq!(total(&counter), 6);
        assert_eq!(counter.counts.len(), 6);
    }

    #[test]
    fn test_count_all_ngrams_short_sequence() {
        let mut counter = Ngrams::new(3, Some(1)).unwrap();
        counter.count(vec!["a".into()]);

        assert_eq!(counter.get(vec!["a".into()]), 1);
        assert_eq!(counter.totals[0], 1); // unigrams
        assert_eq!(counter.totals[1], 0); // bigrams
        assert_eq!(counter.totals[2], 0); // trigrams
    }

    #[test]
    fn test_count_all_ngrams_min_n_equals_n() {
        // Should behave identically to single-order
        let mut counter = Ngrams::new(2, Some(2)).unwrap();
        counter.count(vec!["a".into(), "b".into(), "c".into()]);

        assert_eq!(counter.get(vec!["a".into()]), 0); // no unigrams
        assert_eq!(counter.get(vec!["a".into(), "b".into()]), 1);
        assert_eq!(counter.get(vec!["b".into(), "c".into()]), 1);
        assert_eq!(total(&counter), 2);
    }

    #[test]
    fn test_merge_all_ngrams() {
        let mut c1 = Ngrams::new(2, Some(1)).unwrap();
        c1.count(vec!["a".into(), "b".into()]);

        let mut c2 = Ngrams::new(2, Some(1)).unwrap();
        c2.count(vec!["b".into(), "c".into()]);

        let merged = c1.add(&c2).unwrap();
        assert_eq!(merged.get(vec!["b".into()]), 2);
        assert_eq!(merged.get(vec!["a".into(), "b".into()]), 1);
        assert_eq!(merged.get(vec!["b".into(), "c".into()]), 1);
        assert_eq!(merged.totals[0], 4); // unigram totals
        assert_eq!(merged.totals[1], 2); // bigram totals
    }

    #[test]
    fn test_merge_different_min_order_fails() {
        let c1 = Ngrams::new(3, Some(1)).unwrap();
        let c2 = Ngrams::new(3, Some(2)).unwrap();
        assert!(c1.add(&c2).is_err());
    }

    #[test]
    fn test_clear_all_ngrams() {
        let mut counter = Ngrams::new(3, Some(1)).unwrap();
        counter.count(vec!["a".into(), "b".into(), "c".into()]);
        assert_eq!(total(&counter), 6);

        counter.clear();
        assert_eq!(total(&counter), 0);
        assert_eq!(counter.totals, vec![0, 0, 0]);
        assert_eq!(counter.counts.len(), 0);
    }
}