use std::collections::HashSet;
pub const ENGLISH_WORDS: &str = include_str!("validation/words_alpha.txt");
#[cfg(test)]
mod tests;
pub trait Validator {
fn validate(&self, text: &str) -> bool;
}
pub struct CribValidator {
crib: String,
}
impl CribValidator {
pub fn new<S: Into<String>>(crib: S) -> Self {
Self { crib: crib.into() }
}
}
impl Validator for CribValidator {
fn validate(&self, text: &str) -> bool {
text.contains(&self.crib)
}
}
pub struct WordListValidator<'a> {
list: WordList<'a>,
threshold: f32,
}
impl<'a> WordListValidator<'a> {
pub fn new(list: WordList<'a>) -> Self {
Self {
list,
threshold: 0.75,
}
}
pub fn with_threshold(list: WordList<'a>, threshold: f32) -> Self {
Self {
list,
threshold: if threshold > 0.0 && threshold <= 1.0 {
threshold
} else {
0.75
},
}
}
}
impl<'a> Validator for WordListValidator<'a> {
fn validate(&self, text: &str) -> bool {
let mut total_words = 0f32;
let mut valid_words = 0f32;
text.split(|char| match char {
'a'..='z' | 'A'..='Z' => false,
_ => true,
})
.for_each(|word| {
total_words += 1.0;
if self.list.contains(word) {
valid_words += 1.0;
}
});
valid_words / total_words >= self.threshold
}
}
pub struct WordList<'a> {
set: HashSet<&'a str>,
}
impl<'a> WordList<'a> {
pub fn new(raw_list: &'a str) -> Self {
let mut set = HashSet::new();
for word in raw_list.lines() {
set.insert(word);
}
Self { set }
}
fn contains(&self, word: &str) -> bool {
self.set.contains(word)
}
}