|
| 1 | +use crate::{FeatureExtractor, SimStringDB}; |
| 2 | +use std::collections::{HashMap, HashSet}; |
| 3 | + |
| 4 | +pub struct HashDB<TExtractor> |
| 5 | +where |
| 6 | + TExtractor: FeatureExtractor, |
| 7 | +{ |
| 8 | + pub feature_extractor: TExtractor, |
| 9 | + pub string_collection: Vec<String>, |
| 10 | + pub string_size_map: HashMap<usize, HashSet<String>>, |
| 11 | + pub string_feature_map: HashMap<usize, HashMap<(String, i32), HashSet<String>>>, |
| 12 | + pub lookup_cache: HashMap<(usize, (String, i32)), HashSet<String>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<TExtractor> HashDB<TExtractor> |
| 16 | +where |
| 17 | + TExtractor: FeatureExtractor, |
| 18 | +{ |
| 19 | + pub fn new(feature_extractor: TExtractor) -> Self { |
| 20 | + HashDB { |
| 21 | + feature_extractor, |
| 22 | + string_collection: Vec::new(), |
| 23 | + string_size_map: HashMap::new(), |
| 24 | + string_feature_map: HashMap::new(), |
| 25 | + lookup_cache: HashMap::new(), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn lookup_feature_set_by_size_feature( |
| 30 | + &mut self, |
| 31 | + size: usize, |
| 32 | + feature: &(String, i32), |
| 33 | + ) -> &HashSet<String> { |
| 34 | + let cache_key = (size, feature.clone()); |
| 35 | + |
| 36 | + self.lookup_cache |
| 37 | + .entry(cache_key.clone()) |
| 38 | + .or_insert_with(|| { |
| 39 | + // If not in cache, retrieve from string_feature_map or return an empty set |
| 40 | + self.string_feature_map |
| 41 | + .get(&size) |
| 42 | + .and_then(|feature_map| feature_map.get(feature)) |
| 43 | + .cloned() |
| 44 | + .unwrap_or_else(HashSet::new) |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<TExtractor> SimStringDB for HashDB<TExtractor> |
| 50 | +where |
| 51 | + TExtractor: FeatureExtractor, |
| 52 | +{ |
| 53 | + fn get_max_feature_size(&self) -> usize { |
| 54 | + *self.string_feature_map.keys().max().unwrap_or(&0) |
| 55 | + } |
| 56 | + |
| 57 | + fn insert(&mut self, s: String) { |
| 58 | + // Add the string to the collection |
| 59 | + self.string_collection.push(s.clone()); |
| 60 | + |
| 61 | + // Extract features from the string |
| 62 | + let features = self.feature_extractor.extract(&s); |
| 63 | + |
| 64 | + // Determine the size (number of features) |
| 65 | + let size = features.len(); |
| 66 | + |
| 67 | + // Update string_size_map |
| 68 | + self.string_size_map |
| 69 | + .entry(size) |
| 70 | + .or_default() |
| 71 | + .insert(s.clone()); |
| 72 | + |
| 73 | + // Update string_feature_map |
| 74 | + let feature_map = self.string_feature_map.entry(size).or_default(); |
| 75 | + |
| 76 | + for (feature, count) in features { |
| 77 | + let key = (feature.clone(), count); |
| 78 | + |
| 79 | + feature_map.entry(key).or_default().insert(s.clone()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn describe_collection(&self) -> (usize, f64, usize) { |
| 84 | + let total_collection = self.string_collection.len(); |
| 85 | + |
| 86 | + let total_sizes: usize = self |
| 87 | + .string_size_map |
| 88 | + .iter() |
| 89 | + .map(|(size, strings)| size * strings.len()) |
| 90 | + .sum(); |
| 91 | + let total_strings: usize = self |
| 92 | + .string_size_map |
| 93 | + .values() |
| 94 | + .map(|strings| strings.len()) |
| 95 | + .sum(); |
| 96 | + let avg_size_ngrams = if total_strings == 0 { |
| 97 | + 0.0 |
| 98 | + } else { |
| 99 | + total_sizes as f64 / total_strings as f64 |
| 100 | + }; |
| 101 | + |
| 102 | + let total_ngrams: usize = self |
| 103 | + .string_feature_map |
| 104 | + .values() |
| 105 | + .map(|feature_map| feature_map.len()) |
| 106 | + .sum(); |
| 107 | + |
| 108 | + (total_collection, avg_size_ngrams, total_ngrams) |
| 109 | + } |
| 110 | +} |
0 commit comments