use std::collections::HashSet;
use std::path::PathBuf;
use spellbook::Dictionary;
use crate::dict::{canonical, strip_possessive, WorkingDict};
const MIN_SUGGEST_LEN: usize = 3;
const CUSTOM_SUGGEST_LIMIT: usize = 3;
pub struct Engine {
dict: Dictionary,
custom_dict: Dictionary,
working_path: PathBuf,
working_ci: HashSet<String>,
working_cs: HashSet<String>,
working_phrases: HashSet<String>,
session_ignore: HashSet<String>,
phrase_bigrams: HashSet<String>,
}
impl Engine {
pub fn new(dict: Dictionary, working: WorkingDict, working_path: PathBuf) -> Self {
let phrase_bigrams = crate::dict::build_phrase_bigrams(&working.phrases);
let custom_dict = build_custom_dict(&working.ci, &working.cs);
Self {
dict,
custom_dict,
working_path,
working_ci: working.ci,
working_cs: working.cs,
working_phrases: working.phrases,
session_ignore: HashSet::new(),
phrase_bigrams,
}
}
pub fn working_path(&self) -> &PathBuf {
&self.working_path
}
pub fn working_ci_count(&self) -> usize {
self.working_ci.len()
}
pub fn working_cs_count(&self) -> usize {
self.working_cs.len()
}
pub fn check(&self, word: &str) -> bool {
if self.user_layer_has(word) {
return true;
}
if strip_possessive(word).is_some_and(|s| self.user_layer_has(s)) {
return true;
}
self.dict.check(word)
}
fn user_layer_has(&self, word: &str) -> bool {
if self.session_ignore.contains(&word.to_lowercase()) {
return true;
}
if self.working_cs.contains(word) {
return true;
}
if self.working_ci.contains(&word.to_lowercase()) {
return true;
}
false
}
pub fn suggest(&self, word: &str) -> Vec<String> {
let mut custom = Vec::new();
self.custom_dict
.checker()
.into_suggester()
.suggest(word, &mut custom);
custom.retain(|s| s.chars().count() >= MIN_SUGGEST_LEN);
let mut out = custom
.into_iter()
.take(CUSTOM_SUGGEST_LIMIT)
.collect::<Vec<_>>();
let mut system = Vec::new();
self.dict
.checker()
.into_suggester()
.suggest(word, &mut system);
system.retain(|s| s.chars().count() >= MIN_SUGGEST_LEN);
for suggestion in system {
if !out.contains(&suggestion) {
out.push(suggestion);
}
}
out
}
pub fn ignore_session(&mut self, word: &str) {
self.session_ignore.insert(canonical(word).to_lowercase());
}
pub fn add_ci(&mut self, word: &str) {
let word = canonical(word).to_lowercase();
self.working_ci.insert(word.clone());
let _ = self.custom_dict.add(&word);
}
pub fn add_cs(&mut self, word: &str) {
let word = canonical(word);
self.working_cs.insert(word.clone());
let _ = self.custom_dict.add(&word);
}
#[allow(dead_code)]
pub fn remove(&mut self, word: &str) -> bool {
let c = canonical(word);
let a = self.working_ci.remove(&c.to_lowercase());
let b = self.working_cs.remove(&c);
let removed = a || b;
if removed {
self.custom_dict = build_custom_dict(&self.working_ci, &self.working_cs);
}
removed
}
pub fn phrase_bigrams(&self) -> &HashSet<String> {
&self.phrase_bigrams
}
pub fn save_working(&self) -> anyhow::Result<()> {
let dict = WorkingDict {
ci: self.working_ci.clone(),
cs: self.working_cs.clone(),
phrases: self.working_phrases.clone(),
};
crate::dict::save(&self.working_path, &dict)
}
}
fn build_custom_dict(ci: &HashSet<String>, cs: &HashSet<String>) -> Dictionary {
let mut dict = Dictionary::new("", "0\n").expect("empty dictionary is valid");
for word in ci.iter().chain(cs.iter()) {
let _ = dict.add(word);
}
dict
}
pub fn load_dictionary(aff: &str, dic: &str) -> anyhow::Result<Dictionary> {
Dictionary::new(aff, dic).map_err(|e| anyhow::anyhow!("failed to parse dictionary: {e:?}"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dict::WorkingDict;
use std::path::PathBuf;
fn engine_with_ci(word: &str) -> Engine {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let mut wd = WorkingDict::default();
wd.add_ci(word);
Engine::new(dict, wd, PathBuf::from("/dev/null"))
}
#[test]
fn possessive_accepted_when_base_added() {
let e = engine_with_ci("thorne");
assert!(e.check("Thorne"));
assert!(e.check("thorne"));
assert!(e.check("Thorne's")); assert!(e.check("Thorne\u{2019}s")); assert!(!e.check("Thornex"));
}
#[test]
fn add_via_possessive_form_registers_stem() {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let wd = WorkingDict::default();
let mut e = Engine::new(dict, wd, PathBuf::from("/dev/null"));
e.add_ci("Atrax's");
assert!(e.check("Atrax"));
assert!(e.check("Atrax's"));
assert!(e.check("atrax"));
}
#[test]
fn ignore_via_possessive_suppresses_stem() {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let wd = WorkingDict::default();
let mut e = Engine::new(dict, wd, PathBuf::from("/dev/null"));
e.ignore_session("Atrax's");
assert!(e.check("Atrax"));
assert!(e.check("Atrax's"));
}
#[test]
fn vendored_dict_accepts_else_possessive() {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let e = Engine::new(dict, WorkingDict::default(), PathBuf::from("/dev/null"));
assert!(e.check("else"));
assert!(e.check("else's"), "local patch else->else/M missing?");
assert!(e.check("anyone else's".split(' ').nth(1).unwrap()));
}
#[test]
fn suggestions_drop_very_short() {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let e = Engine::new(dict, WorkingDict::default(), PathBuf::from("/dev/null"));
let sugs = e.suggest("se");
assert!(
sugs.iter().all(|s| s.chars().count() >= 3),
"short leak: {sugs:?}"
);
assert!(sugs.contains(&"see".to_string()));
}
#[test]
fn suggestions_include_working_dictionary() {
let e = engine_with_ci("Thorne");
let sugs = e.suggest("Thorn");
assert!(
sugs.contains(&"Thorne".to_string()),
"custom suggestion missing: {sugs:?}"
);
}
#[test]
fn suggestions_include_entries_added_after_startup() {
let sys = crate::sysdict::resolve_embedded();
let dict = load_dictionary(&sys.aff, &sys.dic).unwrap();
let mut e = Engine::new(dict, WorkingDict::default(), PathBuf::from("/dev/null"));
e.add_cs("Gondor");
let sugs = e.suggest("gondr");
assert!(
sugs.contains(&"Gondor".to_string()),
"custom suggestion missing: {sugs:?}"
);
}
}