1use std::collections::HashSet;
4use std::sync::LazyLock;
5
6use crate::TokenFilter;
7
8#[derive(Debug, Default, Clone)]
10pub struct LowercaseFilter;
11
12impl TokenFilter for LowercaseFilter {
13 fn apply(&self, mut token: String) -> Option<String> {
14 if token.is_ascii() {
15 token.make_ascii_lowercase();
16 Some(token)
17 } else {
18 Some(token.to_lowercase())
19 }
20 }
21}
22
23#[derive(Debug, Clone)]
25pub struct MinLengthFilter(pub usize);
26
27impl TokenFilter for MinLengthFilter {
28 fn apply(&self, token: String) -> Option<String> {
29 if token.chars().count() >= self.0 {
30 Some(token)
31 } else {
32 None
33 }
34 }
35}
36
37#[derive(Debug, Clone)]
39pub struct MaxLengthFilter(pub usize);
40
41impl Default for MaxLengthFilter {
42 fn default() -> Self {
43 Self(40)
44 }
45}
46
47impl TokenFilter for MaxLengthFilter {
48 fn apply(&self, token: String) -> Option<String> {
49 if token.chars().count() <= self.0 {
50 Some(token)
51 } else {
52 None
53 }
54 }
55}
56
57#[derive(Debug, Default, Clone)]
61pub struct StopWordFilter;
62
63static EN_STOP_WORDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
64 [
65 "a",
66 "about",
67 "above",
68 "after",
69 "again",
70 "against",
71 "all",
72 "am",
73 "an",
74 "and",
75 "any",
76 "are",
77 "as",
78 "at",
79 "be",
80 "because",
81 "been",
82 "before",
83 "being",
84 "below",
85 "between",
86 "both",
87 "but",
88 "by",
89 "can",
90 "could",
91 "did",
92 "do",
93 "does",
94 "doing",
95 "don't",
96 "down",
97 "during",
98 "each",
99 "few",
100 "for",
101 "from",
102 "further",
103 "get",
104 "got",
105 "had",
106 "has",
107 "have",
108 "having",
109 "he",
110 "her",
111 "here",
112 "hers",
113 "herself",
114 "him",
115 "himself",
116 "his",
117 "how",
118 "i",
119 "if",
120 "in",
121 "into",
122 "is",
123 "it",
124 "its",
125 "itself",
126 "just",
127 "me",
128 "more",
129 "most",
130 "my",
131 "myself",
132 "no",
133 "nor",
134 "not",
135 "now",
136 "of",
137 "off",
138 "on",
139 "once",
140 "only",
141 "or",
142 "other",
143 "our",
144 "ours",
145 "ourselves",
146 "out",
147 "over",
148 "own",
149 "same",
150 "she",
151 "should",
152 "so",
153 "some",
154 "such",
155 "than",
156 "that",
157 "the",
158 "their",
159 "theirs",
160 "them",
161 "themselves",
162 "then",
163 "there",
164 "these",
165 "they",
166 "this",
167 "those",
168 "through",
169 "to",
170 "too",
171 "under",
172 "until",
173 "up",
174 "us",
175 "very",
176 "was",
177 "we",
178 "were",
179 "what",
180 "when",
181 "where",
182 "which",
183 "while",
184 "who",
185 "whom",
186 "why",
187 "will",
188 "with",
189 "would",
190 "you",
191 "your",
192 "yours",
193 "yourself",
194 "yourselves",
195 ]
196 .into_iter()
197 .collect()
198});
199
200impl TokenFilter for StopWordFilter {
201 fn apply(&self, token: String) -> Option<String> {
202 if EN_STOP_WORDS.contains(token.as_str()) {
203 None
204 } else {
205 Some(token)
206 }
207 }
208}
209
210#[derive(Debug, Default, Clone)]
214pub struct Bm25StopWordFilter;
215
216static BM25_STOP_WORDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
217 [
218 "a", "an", "and", "are", "as", "at", "be", "been", "being", "but", "by", "can", "did",
219 "do", "does", "doing", "done", "for", "from", "had", "has", "have", "having", "he", "her",
220 "here", "hers", "him", "his", "how", "i", "if", "in", "into", "is", "it", "its", "just",
221 "may", "me", "might", "my", "no", "nor", "not", "of", "on", "or", "our", "out", "own",
222 "say", "she", "should", "so", "some", "such", "than", "that", "the", "their", "them",
223 "then", "there", "these", "they", "this", "those", "through", "to", "too", "up", "us",
224 "very", "was", "we", "were", "what", "when", "where", "which", "while", "who", "whom",
225 "why", "will", "with", "would", "you", "your",
226 ]
227 .into_iter()
228 .collect()
229});
230
231impl TokenFilter for Bm25StopWordFilter {
232 fn apply(&self, token: String) -> Option<String> {
233 if BM25_STOP_WORDS.contains(token.as_str()) {
234 None
235 } else {
236 Some(token)
237 }
238 }
239}
240
241#[cfg(feature = "stem")]
245pub struct SnowballStemmer(rust_stemmers::Stemmer);
246
247#[cfg(feature = "stem")]
248impl SnowballStemmer {
249 pub fn english() -> Self {
251 Self(rust_stemmers::Stemmer::create(
252 rust_stemmers::Algorithm::English,
253 ))
254 }
255
256 pub fn for_algorithm(algo: rust_stemmers::Algorithm) -> Self {
258 Self(rust_stemmers::Stemmer::create(algo))
259 }
260}
261
262#[cfg(feature = "stem")]
263impl TokenFilter for SnowballStemmer {
264 fn apply(&self, token: String) -> Option<String> {
265 if token.chars().all(|c| c.is_ascii_alphabetic()) {
266 Some(self.0.stem(&token).into_owned())
267 } else {
268 Some(token)
269 }
270 }
271}
272
273#[cfg(test)]
274#[path = "filter/tests.rs"]
275mod tests;