is_it_slop_preprocessing/pre_processor/vectorizer/params.rs
1use std::ops::RangeInclusive;
2
3/// Default minimum n-gram size.
4pub const DEFAULT_MIN_NGRAM: usize = 2;
5
6/// Default maximum n-gram size.
7///
8/// **Important**: This value is also used to optimize `SmallVec` storage for n-gram keys.
9/// If you frequently use n-gram ranges larger than this, consider increasing this constant
10/// and recompiling for better performance.
11pub const DEFAULT_MAX_NGRAM: usize = 4;
12
13/// Configuration parameters for text vectorization.
14///
15/// Controls n-gram extraction, vocabulary filtering, and term frequency scaling.
16#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[derive(Clone, Debug)]
19pub struct VectorizerParams {
20 ngram_range: Vec<usize>,
21 /// Minimum document frequency for filtering vocabulary.
22 /// - If `min_df` is in (0.0, 1.0), it's a proportion of documents
23 /// - If `min_df` >= 1.0, it's an absolute document count
24 min_df: f32,
25 /// Maximum document frequency for filtering vocabulary.
26 /// - If `max_df` is in (0.0, 1.0], it's a proportion of documents
27 /// - If `max_df` > 1.0, it's an absolute document count
28 max_df: f32,
29 /// Apply sublinear tf scaling: replace term frequency `tf` with `1 + log(tf)`.
30 /// This reduces the impact of terms that occur many times in a document.
31 sublinear_tf: bool,
32}
33
34impl VectorizerParams {
35 /// Create new vectorizer parameters.
36 ///
37 /// # Arguments
38 /// * `ngram_range` - Range of n-gram sizes (e.g., `3..=5` for trigrams to 5-grams)
39 /// * `min_df` - Minimum document frequency (proportion or count)
40 /// * `max_df` - Maximum document frequency (proportion or count)
41 /// * `sublinear_tf` - Whether to apply log scaling to term frequencies
42 ///
43 /// # Panics
44 /// Panics if `min_df` or `max_df` are not positive, or if `ngram_range` is empty.
45 pub fn new(
46 ngram_range: impl Into<RangeInclusive<usize>>,
47 min_df: f32,
48 max_df: f32,
49 sublinear_tf: bool,
50 ) -> Self {
51 let n_sizes = ngram_range.into().collect::<Vec<_>>();
52 assert!(
53 !n_sizes.is_empty(),
54 "ngram_range must contain at least one value"
55 );
56 assert!(
57 min_df > 0.0,
58 "min_df must be positive (proportion in (0.0, 1.0) or absolute count >= 1.0)"
59 );
60 assert!(
61 max_df > 0.0,
62 "max_df must be positive (proportion in (0.0, 1.0] or absolute count > 1.0)"
63 );
64 Self {
65 ngram_range: n_sizes,
66 min_df,
67 max_df,
68 sublinear_tf,
69 }
70 }
71
72 /// Get all n-gram sizes as a slice.
73 #[must_use]
74 pub fn ngram_counts(&self) -> &[usize] {
75 &self.ngram_range
76 }
77
78 /// Get the n-gram range as a tuple `(min, max)`.
79 #[must_use]
80 pub fn ngram_range(&self) -> (usize, usize) {
81 (
82 *self.ngram_range.first().expect("ngram_range is not empty"),
83 *self.ngram_range.last().expect("ngram_range is not empty"),
84 )
85 }
86
87 /// Get the minimum document frequency threshold.
88 #[must_use]
89 pub fn min_df(&self) -> f32 {
90 self.min_df
91 }
92
93 /// Get the maximum document frequency threshold.
94 #[must_use]
95 pub fn max_df(&self) -> f32 {
96 self.max_df
97 }
98
99 /// Get whether sublinear TF scaling is enabled.
100 #[must_use]
101 pub fn sublinear_tf(&self) -> bool {
102 self.sublinear_tf
103 }
104}
105impl Default for VectorizerParams {
106 fn default() -> Self {
107 Self {
108 ngram_range: vec![DEFAULT_MIN_NGRAM, DEFAULT_MAX_NGRAM],
109 min_df: 10.0,
110 max_df: 1.0,
111 sublinear_tf: false,
112 }
113 }
114}
115
116impl From<((usize, usize), f32, f32, bool)> for VectorizerParams {
117 fn from(value: ((usize, usize), f32, f32, bool)) -> Self {
118 Self::new(value.0.0..=value.0.1, value.1, value.2, value.3)
119 }
120}