Skip to content
Snippets Groups Projects
Commit 5ebce499 authored by Phil Höfer's avatar Phil Höfer
Browse files

Implement Exclusion List For Suggested Terms

parent 1f44ba21
Branches
No related tags found
1 merge request!7Resolve "Implement Filters for Trait-based Predictors"
auth = "12345"
term_frequency_threshold = 2
max_predict_count = 5
\ No newline at end of file
max_predict_count = 5
blocked_words = ""
\ No newline at end of file
......@@ -34,8 +34,18 @@ impl Predictor for MarkovChainPredictor {
}
fn update(&mut self, query: &str) -> Result<(), Box<dyn std::error::Error>> {
let blocklist: Vec<&str> = match self.configuration.get("blocked_words") {
Some(list) => {
list.split_whitespace().collect()
},
_ => Vec::new()
};
//println!("blocklist:{:?}",self.configuration);
let lowercase_query = query.to_lowercase();
let words: Vec<&str> = lowercase_query.split_whitespace().collect();
let words: Vec<&str> = lowercase_query.split_whitespace().filter(|&x| !blocklist.contains(&x)).collect();
for window in words.windows(2) {
if let [first, second] = window {
self.chain
......@@ -107,14 +117,22 @@ pub fn from_file_path(file_path: &str) -> Result<MarkovChainPredictor, std::io::
pub fn from_file_path_and_config(file_path: &str, config: HashMap<String, impl Into<String>>) -> Result<MarkovChainPredictor, std::io::Error> {
let mut markov_chain: MarkovChainPredictor = from_file_path(file_path)?;
let mut configuration = HashMap::new();
for (key, value) in config {
configuration.insert(key, value.into());
}
let file = File::open(file_path)?;
let mut reader = ReaderBuilder::new().from_reader(file);
let mut markov_chain: MarkovChainPredictor = MarkovChainPredictor::new();
markov_chain.configuration = configuration;
println!("{}",markov_chain.configuration.get("term_frequency_threshold").unwrap());
for result in reader.records() {
let record = result?;
if let Some(query) = record.get(5) {
markov_chain.update(query);
}
}
Ok(markov_chain)
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment