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> {
Some(n) if n.parse::<usize>().unwrap_or_default() > 0 => n.parse::<usize>().unwrap_or(5),
_ => 5
};
let prediction = &markov_chain.predict(&query, predict_count);
let predictions = &markov_chain.predict(&query, predict_count);
//println!("Query: {}, Prediction:{}", query, prediction);
let response = Response::from_string(prediction);
let response = Response::from_string(
format!("[\"{}\",[{}]]", query, predictions.join(",")));
request.respond(response);
let now = std::time::SystemTime::now();
let elapsed = now.duration_since(last_update).expect("Time went backwards");
......
......@@ -64,7 +64,7 @@ impl Predictor for MarkovChainPredictor {
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) =
get_top_following_words(
&self.chain,
......@@ -77,9 +77,9 @@ impl Predictor for MarkovChainPredictor {
.into_iter()
.map(|(word, _)| format!("\"{} {}\"", query, word))
.collect();
return format!("[\"{}\",[{}]]", query, predictions.join(","));
return predictions;
}
String::new()
Vec::<String>::new()
}
}
......
......@@ -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) =
get_top_completions(
&self,
......@@ -99,9 +99,9 @@ impl Predictor for SetPredictor {
.into_iter()
.map(|(word, _)| format!("\"{}\"", format!("{} {}",query_prefix, word).trim()))
.collect();
return format!("[\"{}\",[{}]]", query, predictions.join(","));
return predictions;
}
String::new()
Vec::<String>::new()
}
}
......
......@@ -41,13 +41,13 @@ impl Predictor for CompositePredictor {
}
fn predict(&self, query: &str, n: usize) -> String {
let markov_prediction = self.markov_predictor.predict(query, n);
if markov_prediction.len() > 7+query.len() {
return markov_prediction;
fn predict(&self, query: &str, n: usize) -> Vec<String> {
let mut prediction = self.markov_predictor.predict(query, n);
if prediction.len() < n {
prediction.append(&mut self.set_predictor.predict(query, n));
prediction.truncate(n);
}
let set_prediction = self.set_predictor.predict(query, n);
set_prediction
prediction
}
}
......
......@@ -6,7 +6,7 @@ pub mod composite;
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 decay(&mut self) -> ();
......
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