Skip to content
Snippets Groups Projects
Unverified Commit 3584e3fe authored by Joschua Kesper's avatar Joschua Kesper
Browse files

Removed quotes appearing in source from markov generated

parent 13bcae88
Branches
No related tags found
No related merge requests found
lorem
ipsum
\ No newline at end of file
a a b
b a c
c a b c
......@@ -47,7 +47,10 @@ impl Api {
#[oai(path = "/markov", method = "get")]
async fn markov(&self) -> PlainText<String> {
PlainText(self.markov_model.generage_message())
match self.markov_model.generage_message() {
Ok(quote) => PlainText(quote),
Err(err) => PlainText(err.to_string()),
}
}
}
......
use std::{collections::HashMap, hash::Hash};
use std::{collections::{HashMap, HashSet}, hash::Hash, result::Result};
use rand::Rng;
#[derive(Hash, Clone, PartialEq, Eq, Debug)]
......@@ -42,18 +42,21 @@ impl TokenSampler {
#[derive(Clone)]
pub struct Model {
messages: HashSet<String>,
token_to_token_sampler: HashMap<Token, TokenSampler>
}
impl Model {
pub fn new() -> Model {
Model {
messages: HashSet::new(),
token_to_token_sampler: HashMap::new()
}
}
pub fn add_messages(&mut self, messages: &Vec<String>) {
for message in messages {
self.messages.insert(message.to_string());
self.add_tokenized_message(&Model::tokenize(message));
}
}
......@@ -73,17 +76,23 @@ impl Model {
}
}
pub fn generage_message(&self) -> String {
pub fn generage_message(&self) -> Result<String, &'static str> {
let mut current_token = Token::Start;
let mut current_message = "".to_owned();
for _ in 1..5 {
loop {
let next_token = self.token_to_token_sampler[&current_token].sample();
match &next_token {
Token::Start => unreachable!("Start token should not be reachable inside a message :("),
Token::End => return current_message,
Token::End => break,
Token::Word(current_word) => current_message = current_message + " " + &current_word
}
current_token = next_token;
}
if !self.messages.contains(current_message.trim()) {
return Ok(current_message);
}
}
Err("\"Too many matches with source quotes\" ~λ")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment