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

Refactor Internal Response API

parent 674f93f0
No related branches found
No related tags found
No related merge requests found
Pipeline #9981 passed
...@@ -100,9 +100,10 @@ fn main() -> Result<(), io::Error> { ...@@ -100,9 +100,10 @@ fn main() -> Result<(), io::Error> {
Some(n) if n.parse::<usize>().unwrap_or_default() > 0 => n.parse::<usize>().unwrap_or(5), Some(n) if n.parse::<usize>().unwrap_or_default() > 0 => n.parse::<usize>().unwrap_or(5),
_ => 5 _ => 5
}; };
let prediction = &markov_chain.predict(&query, predict_count); let predictions = &markov_chain.predict(&query, predict_count);
//println!("Query: {}, Prediction:{}", query, prediction); //println!("Query: {}, Prediction:{}", query, prediction);
let response = Response::from_string(prediction); let response = Response::from_string(
format!("[\"{}\",[{}]]", query, predictions.join(",")));
request.respond(response); request.respond(response);
let now = std::time::SystemTime::now(); let now = std::time::SystemTime::now();
let elapsed = now.duration_since(last_update).expect("Time went backwards"); let elapsed = now.duration_since(last_update).expect("Time went backwards");
......
...@@ -64,7 +64,7 @@ impl Predictor for MarkovChainPredictor { ...@@ -64,7 +64,7 @@ impl Predictor for MarkovChainPredictor {
self.chain = HashMap::new(); self.chain = HashMap::new();
} }
fn predict(&self, query: &str, n: usize) -> String { fn predict(&self, query: &str, n: usize) -> Vec<String> {
if let Some(top_words) = if let Some(top_words) =
get_top_following_words( get_top_following_words(
&self.chain, &self.chain,
...@@ -77,9 +77,9 @@ impl Predictor for MarkovChainPredictor { ...@@ -77,9 +77,9 @@ impl Predictor for MarkovChainPredictor {
.into_iter() .into_iter()
.map(|(word, _)| format!("\"{} {}\"", query, word)) .map(|(word, _)| format!("\"{} {}\"", query, word))
.collect(); .collect();
return format!("[\"{}\",[{}]]", query, predictions.join(",")); return predictions;
} }
String::new() Vec::<String>::new()
} }
} }
......
...@@ -85,7 +85,7 @@ impl Predictor for SetPredictor { ...@@ -85,7 +85,7 @@ impl Predictor for SetPredictor {
} }
fn predict(&self, query: &str, n: usize) -> String { fn predict(&self, query: &str, n: usize) -> Vec<String> {
if let Some(top_words) = if let Some(top_words) =
get_top_completions( get_top_completions(
&self, &self,
...@@ -99,9 +99,9 @@ impl Predictor for SetPredictor { ...@@ -99,9 +99,9 @@ impl Predictor for SetPredictor {
.into_iter() .into_iter()
.map(|(word, _)| format!("\"{}\"", format!("{} {}",query_prefix, word).trim())) .map(|(word, _)| format!("\"{}\"", format!("{} {}",query_prefix, word).trim()))
.collect(); .collect();
return format!("[\"{}\",[{}]]", query, predictions.join(",")); return predictions;
} }
String::new() Vec::<String>::new()
} }
} }
......
...@@ -41,13 +41,13 @@ impl Predictor for CompositePredictor { ...@@ -41,13 +41,13 @@ impl Predictor for CompositePredictor {
} }
fn predict(&self, query: &str, n: usize) -> String { fn predict(&self, query: &str, n: usize) -> Vec<String> {
let markov_prediction = self.markov_predictor.predict(query, n); let mut prediction = self.markov_predictor.predict(query, n);
if markov_prediction.len() > 7+query.len() { if prediction.len() < n {
return markov_prediction; prediction.append(&mut self.set_predictor.predict(query, n));
prediction.truncate(n);
} }
let set_prediction = self.set_predictor.predict(query, n); prediction
set_prediction
} }
} }
......
...@@ -6,7 +6,7 @@ pub mod composite; ...@@ -6,7 +6,7 @@ pub mod composite;
pub trait Predictor { pub trait Predictor {
fn predict(&self, query: &str, n: usize) -> String; fn predict(&self, query: &str, n: usize) -> Vec<String>;
fn update(&mut self, query: &str) -> Result<(),Box<dyn std::error::Error>>; fn update(&mut self, query: &str) -> Result<(),Box<dyn std::error::Error>>;
fn decay(&mut self) -> (); fn decay(&mut self) -> ();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment