How to remove stop words from a string in Rust

1 Answer

0 votes
fn main() {
    let input = "a rust and java to python a we if c# then a and aa";
    println!("Original: {}", input);

    let words: Vec<&str> = split_words(input);
    let filtered = remove_stop_words(&words);

    println!("Filtered: {}", filtered);
}

// A stopwords list is a collection of commonly used words in a language
// that are often removed during text processing tasks.

fn stop_words() -> &'static [&'static str] {
    &[
        "i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your",
        "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her",
        "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs",
        "themselves", "what", "which", "who", "whom", "this", "that", "these", "those",
        "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had",
        "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or",
        "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against",
        "between", "into", "through", "to", "from", "in", "out", "on", "off", "over", "further",
        "then", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each",
        "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own",
        "so", "than", "too", "very", "can", "will", "just", "don", "should", "now",
    ]
}

fn is_stop_word(word: &str) -> bool {
    stop_words().contains(&word)
}

fn split_words(input: &str) -> Vec<&str> {
    input.trim().split_whitespace().collect()
}

fn remove_stop_words(words: &[&str]) -> String {
    words
        .iter()
        .filter(|word| !is_stop_word(word))
        .cloned()
        .collect::<Vec<&str>>()
        .join(" ")
}


   
     
/*
run:

Original: a rust and java to python a we if c# then a and aa
Filtered: rust java python c# aa
   
*/
  
 

 



answered Jul 18 by avibootz
...