LengthFilter

Struct LengthFilter 

Source
pub struct LengthFilter {
    pub min_length: usize,
    pub max_length: usize,
}
Expand description

Filter tokens by length

Fields§

§min_length: usize

Minimum token length

§max_length: usize

Maximum token length

Implementations§

Source§

impl LengthFilter

Source

pub fn new(_min_length: usize, maxlength: usize) -> Self

Create a new length filter

Examples found in repository?
examples/token_filtering_demo.rs (line 24)
8fn main() -> Result<()> {
9    println!("Token Filtering Demo");
10    println!("===================\n");
11
12    // Create a sample text
13    let text = "The quick brown fox jumps over the lazy dog. The fox is quick and brown.";
14    println!("Original text: {text}\n");
15
16    // Create a tokenizer
17    let tokenizer = WordTokenizer::default();
18    let tokens = tokenizer.tokenize(text)?;
19    println!("Tokenized: {tokens:?}\n");
20
21    // 1. Filter by length
22    println!("1. Length Filtering");
23    println!("------------------");
24    let length_filter = LengthFilter::new(4, 6);
25    let filtered = length_filter.apply(&tokens);
26    println!("Tokens with length 4-6: {filtered:?}");
27
28    let filteredtext = length_filter.filtertext(text, &tokenizer)?;
29    println!("Filtered text: {filteredtext}\n");
30
31    // 2. Filter by frequency
32    println!("2. Frequency Filtering");
33    println!("---------------------");
34
35    // Create token counts
36    let mut counts = HashMap::new();
37    for token in &tokens {
38        *counts.entry(token.clone()).or_insert(0) += 1;
39    }
40
41    // Print counts
42    println!("Token counts:");
43    for (token, count) in &counts {
44        println!("  {token} : {count}");
45    }
46
47    // Filter tokens that appear more than once
48    let freq_filter = FrequencyFilter::from_counts(counts.clone(), 2);
49    let filtered = freq_filter.apply(&tokens);
50    println!("\nTokens that appear 2+ times: {filtered:?}");
51
52    let filteredtext = freq_filter.filtertext(text, &tokenizer)?;
53    println!("Filtered text: {filteredtext}\n");
54
55    // 3. Filter by regex pattern
56    println!("3. Regex Filtering");
57    println!("-----------------");
58
59    // Keep only tokens that contain a vowel followed by 'w' or 'r'
60    let regex_filter = RegexFilter::new("[aeiou][wr]", true)?;
61    let filtered = regex_filter.apply(&tokens);
62    println!("Tokens containing a vowel followed by 'w' or 'r': {filtered:?}");
63
64    let filteredtext = regex_filter.filtertext(text, &tokenizer)?;
65    println!("Filtered text: {filteredtext}\n");
66
67    // 4. Stopwords filtering
68    println!("4. Stopwords Filtering");
69    println!("---------------------");
70
71    // Define common stopwords
72    let stopwords = vec![
73        "the".to_string(),
74        "is".to_string(),
75        "and".to_string(),
76        "over".to_string(),
77        "a".to_string(),
78        "an".to_string(),
79    ];
80
81    let stopwords_filter = StopwordsFilter::new(stopwords, true);
82    let filtered = stopwords_filter.apply(&tokens);
83    println!("Tokens with stopwords removed: {filtered:?}");
84
85    let filteredtext = stopwords_filter.filtertext(text, &tokenizer)?;
86    println!("Filtered text: {filteredtext}\n");
87
88    // 5. Composite filtering
89    println!("5. Composite Filtering");
90    println!("---------------------");
91
92    // Create separate filters
93    let length_filter = LengthFilter::new(3, 5);
94    let regex_filter = RegexFilter::new("^[a-z]", true)?;
95
96    // Apply filters sequentially
97    let filtered_by_length = length_filter.apply(&tokens);
98    let filtered = regex_filter.apply(&filtered_by_length);
99    println!("Tokens with length 3-5 AND starting with lowercase letter: {filtered:?}");
100
101    // First filter by length
102    let text_with_length = length_filter.filtertext(text, &tokenizer)?;
103
104    // Then apply regex filter to the already filtered text
105    let filteredtext = regex_filter.filtertext(&text_with_length, &tokenizer)?;
106
107    // We should see only words that are 3-5 chars AND start with lowercase
108    println!("Filtered text: {filteredtext}\n");
109
110    Ok(())
111}
Source

pub fn with_min_length(self, minlength: usize) -> Self

Set minimum token length

Source

pub fn with_max_length(self, maxlength: usize) -> Self

Set maximum token length

Trait Implementations§

Source§

impl Clone for LengthFilter

Source§

fn clone(&self) -> LengthFilter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LengthFilter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LengthFilter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TokenFilter for LengthFilter

Source§

fn apply(&self, tokens: &[String]) -> Vec<String>

Filter tokens based on the strategy
Source§

fn filtertext(&self, text: &str, tokenizer: &dyn Tokenizer) -> Result<String>

Apply the filter directly to text

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V