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

Add Basic Decay

parent 133cf670
No related branches found
No related tags found
1 merge request!11Resolve "Implement Decay for Training Data"
......@@ -49,6 +49,7 @@ fn main() -> Result<(), io::Error> {
.unwrap_or(basic_set::SetPredictor::new());
markov_chain = read_from_db(config.clone(), markov_chain);
markov_chain.decay();
// let term_frequency_threshold = match config.get("term_frequency_threshold") {
// Some(toml::Value::Integer(n)) if *n >= 0 => *n as usize,
......@@ -106,6 +107,7 @@ fn main() -> Result<(), io::Error> {
let now = std::time::SystemTime::now();
let elapsed = now.duration_since(last_update).expect("Time went backwards");
if elapsed >= std::time::Duration::from_secs(24 * 60 * 60) {
markov_chain.decay();
markov_chain = read_from_db(config.clone(), markov_chain);
last_update = now;
}
......@@ -128,7 +130,7 @@ fn read_from_db(config: HashMap<String, String>, mut predictor: SetPredictor) ->
match client {
Ok(mut c) => {
let mut count = 0;
match c.query("SELECT query FROM public.logs_partitioned ORDER BY time DESC LIMIT 5000", &[]) {
match c.query("SELECT query FROM public.logs_partitioned ORDER BY time DESC LIMIT 100000", &[]) {
Ok(rows) => {
for row in rows {
let query: &str = row.get(0);
......
......@@ -60,6 +60,10 @@ impl Predictor for MarkovChainPredictor {
Ok(())
}
fn decay(&mut self) -> () {
//TODO
}
fn predict(&self, query: &str, n: usize) -> String {
if let Some(top_words) =
get_top_following_words(
......
......@@ -61,6 +61,28 @@ impl Predictor for SetPredictor {
Ok(())
}
fn decay(&mut self) -> () {
let keys_to_remove: Vec<String> = self.set.iter_mut()
.filter_map(|(key, value)| {
if *value > 0 {
*value -= 1;
if *value < 1 {
Some(key.clone())
} else {
None
}
} else {
Some(key.clone())
}
})
.collect();
for key in keys_to_remove {
self.set.remove(&key);
}
}
fn predict(&self, query: &str, n: usize) -> String {
if let Some(top_words) =
get_top_completions(
......
......@@ -7,6 +7,7 @@ pub trait Predictor {
fn predict(&self, query: &str, n: usize) -> String;
fn update(&mut self, query: &str) -> Result<(),Box<dyn std::error::Error>>;
fn decay(&mut self) -> ();
fn new() -> Self where Self: Sized;
fn new_from_config(config: HashMap<String, impl Into<String>>) -> Self where Self:Sized;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment