1use std::collections::HashMap;
2
3pub fn mount_index(dict: String) -> HashMap<String, Vec<String>> {
4 let mut index = HashMap::new();
5 let mut term = String::new();
6
7 for line in dict.split("\n") {
8 if (!line.starts_with("\t")) && (line != "") {
9 term = line.to_lowercase();
10 index.insert(term.clone(), Vec::new());
11 }
12
13 index
14 .get_mut(&term)
15 .unwrap()
16 .push(line.trim_start().to_string());
17 }
18
19 index
20}
21
22pub fn find_matches<'a>(key: &String, index: &'a HashMap<String, Vec<String>>) -> Vec<&'a String> {
23 let mut matches = Vec::new();
24
25 for term in index.keys() {
26 let (new_key, new_term) = normalize_terms(&key, &term);
27 let prob = evaluate_match_probability(&new_key, &new_term);
28 if prob > 0.50 {
29 matches.push(term);
30 }
31 }
32
33 matches
34}
35
36fn normalize_terms(key: &String, term: &String) -> (String, String) {
37 let mut new_key = key.clone();
38 let mut new_term = term.clone();
39
40 let key_len = key.len();
41 let term_len = term.len();
42
43 if key_len > term_len {
44 let diff = key_len - term_len;
45 new_term.push_str(" ".repeat(diff).as_str());
46 } else {
47 let diff = term_len - key_len;
48 new_key.push_str(" ".repeat(diff).as_str());
49 }
50
51 (new_key, new_term)
52}
53
54fn count_match(key: &String, term: &String) -> u8 {
55 let mut total_match = 0;
56 for i in 0..key.len() {
57 if key.get(..i) == term.get(..i) {
58 total_match += 1;
59 }
60 }
61
62 total_match
63}
64
65fn evaluate_match_probability(key: &String, term: &String) -> f32 {
66 let total_match = count_match(key, term);
67 total_match as f32 / key.len() as f32
68}