[−][src]Function text_analysis::words_near
pub fn words_near(
word: &(String, u32),
index_rang: usize,
content_vec: &Vec<String>,
words_sorted: &Vec<(String, u32)>
) -> Result<HashMap<String, HashMap<String, u32>>>
Search for words +-5 around given word. Returns result.
Example
use std::{collections::HashMap, hash::Hash}; use text_analysis::{count_words, sort_map_to_vec, words_near}; //function to allow uncomplete comparison of 2 HashMaps fn keys_match<T: Eq + Hash, U, V>(map1: &HashMap<T, U>, map2: &HashMap<T, V>) -> bool { map1.len() == map2.len() && map1.keys().all(|k| map2.contains_key(k)) } //create Vec<Strings>. Would be normally read from file as String and then "trim_to_words(content_of_file_as_string)" to obtain Vec<String> let words = vec![ "one".to_string(), "two".to_string(), "three".to_string(), "four".to_string(), "four".to_string(), "five".to_string(), ]; //define word we will be searching for other words near let word = ("two".to_string(), 2 as u32); //create sorted Vector of words according to their frequency let words_sorted: Vec<(String, u32)> = sort_map_to_vec(count_words(&words).unwrap()).unwrap(); let index_rang: usize = 1; //do the actual search for words +-5 near the word "two" let words_near_map = words_near(&word, index_rang, &words, &words_sorted).unwrap(); //create the expected result to compare to words_near_map let mut hashmap_inner = HashMap::new(); hashmap_inner.insert("four".to_string(), 2 as u32); hashmap_inner.insert("one".to_string(), 1 as u32); hashmap_inner.insert("three".to_string(), 1 as u32); hashmap_inner.insert("five".to_string(), 1 as u32); let mut expected_map = HashMap::new(); expected_map.insert("two".to_string(), hashmap_inner); assert!(keys_match(&words_near_map, &expected_map));