use std::time::Instant;
use rusqlite::Connection;
use super::score::Part;
use crate::Result;
use crate::sqlite::sql_error;
use crate::typos::Dictionary;
const READ_WORDS: &str = "select part, term from _tinystore_search_keys where index_id = ?1 and documents > 0";
const MOST: usize = 8;
pub(crate) struct Corrector {
pub(crate) version: i64,
pub(crate) built: Instant,
scripts: [Dictionary; 3],
}
impl std::fmt::Debug for Corrector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Corrector").field("version", &self.version).finish_non_exhaustive()
}
}
impl Corrector {
pub(crate) fn build(connection: &Connection, index: i64, version: i64, parts: &[Part]) -> Result<Corrector> {
let failed = |error| sql_error("read an index's words", error);
let mut select = connection.prepare_cached(READ_WORDS).map_err(failed)?;
let mut rows = select.query([index]).map_err(failed)?;
let mut words: [Vec<String>; 3] = Default::default();
while let Some(row) = rows.next().map_err(failed)? {
let (part, term): (u32, String) = (row.get(0).map_err(failed)?, row.get(1).map_err(failed)?);
if parts.contains(&Part(part)) {
words[script(&term)].push(term);
}
}
Ok(Corrector { version, built: Instant::now(), scripts: words.map(Dictionary::new) })
}
pub(crate) fn candidates(&self, word: &str) -> Vec<String> {
let edits = match word.chars().count() {
0..5 => return Vec::new(),
5..9 => 1,
_ => 2,
};
let near = self.scripts[script(word)].within(word, edits);
let nearest = near.first().map_or(0, |near| near.edits);
near.into_iter().take_while(|near| near.edits == nearest).take(MOST).map(|near| near.word).collect()
}
}
fn script(word: &str) -> usize {
match word.chars().next().map_or(0, |c| c as u32) {
0x0000..=0x024F | 0x1E00..=0x1EFF => 0,
0x0400..=0x052F => 1,
_ => 2,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn corrector(words: &[&str]) -> Corrector {
let mut scripts: [Vec<&str>; 3] = Default::default();
words.iter().for_each(|word| scripts[script(word)].push(word));
Corrector { version: 0, built: Instant::now(), scripts: scripts.map(Dictionary::new) }
}
#[test]
fn a_word_is_read_with_one_edit_from_five_letters_and_two_from_nine() {
let words = corrector(&["hello", "help", "replication", "репликация"]);
assert_eq!(words.candidates("hell"), Vec::<String>::new());
assert_eq!(words.candidates("helo"), Vec::<String>::new());
assert_eq!(words.candidates("hellp"), ["hello", "help"]);
assert_eq!(words.candidates("replicaiton"), ["replication"]);
assert_eq!(words.candidates("rplicatin"), ["replication"]);
assert_eq!(words.candidates("реплиакция"), ["репликация"]);
}
#[test]
fn only_the_nearest_are_candidates() {
let words = corrector(&["kitten", "kittens", "mitten"]);
assert_eq!(words.candidates("kitten"), ["kittens", "mitten"]);
}
}