How to find keyword matching between multiple text blocks in Rust

1 Answer

0 votes
use std::collections::HashSet;

/*
    Tokenize text into words.
    - Keeps only letters and digits
    - Splits on punctuation and spaces
*/
fn tokenize(text: &str) -> HashSet<String> {
    let mut words: HashSet<String> = HashSet::new();
    let mut word = String::new();

    for c in text.chars() {
        if c.is_ascii_alphanumeric() {
            word.push(c.to_ascii_lowercase());
        } else if !word.is_empty() {
            words.insert(word.clone());
            word.clear();
        }
    }

    if !word.is_empty() {
        words.insert(word);
    }

    words
}

/*
    // Find keyword matches across THREE OR MORE texts
    // -------------------------------------------------------------
    This function receives a vector of sets.
    It returns the intersection of ALL sets.
*/
fn find_matches_multiple(all_sets: &Vec<HashSet<String>>) -> HashSet<String> {
    if all_sets.is_empty() {
        return HashSet::new();
    }

    // Start with the first set
    let mut result: HashSet<String> = all_sets[0].clone();

    // Intersect with each remaining set
    for set in &all_sets[1..] {
        let mut temp: HashSet<String> = HashSet::new();

        for w in &result {
            if set.contains(w) {
                temp.insert(w.clone());
            }
        }

        result = temp;
    }

    result
}

fn main() {
    // -------------------------------------------------------------
    // Three text blocks to compare
    // -------------------------------------------------------------
    let text1 =
        "Machine learning allows computers to learn from data. \
         It is widely used in modern applications.";

    let text2 =
        "Data science uses machine learning techniques. \
         Applications rely on data-driven models.";

    let text3 =
        "Modern applications of machine learning include data analysis, \
         automation, and intelligent systems.";

    // -------------------------------------------------------------
    // Tokenize all texts
    // -------------------------------------------------------------
    let words1 = tokenize(text1);
    let words2 = tokenize(text2);
    let words3 = tokenize(text3);

    // Put them into a vector for multi-text comparison
    let all_sets = vec![words1, words2, words3];

    // -------------------------------------------------------------
    // Find keyword matches across ALL texts
    // -------------------------------------------------------------
    let matches = find_matches_multiple(&all_sets);

    // -------------------------------------------------------------
    // Output results
    // -------------------------------------------------------------
    println!("Matched Keywords Across ALL Texts:");
    for w in &matches {
        print!("{} ", w);
    }
}



/*
run:

Matched Keywords Across ALL Texts:
learning data machine applications 

*/

 



answered May 22 by avibootz

Related questions

...