2019-08-08 11:24:50 -04:00
|
|
|
use std::borrow::Cow;
|
2020-10-24 22:17:16 -04:00
|
|
|
use std::collections::HashMap;
|
2019-08-08 11:24:50 -04:00
|
|
|
|
|
|
|
use unicase::UniCase;
|
|
|
|
|
|
|
|
use typos::tokens::Case;
|
2020-10-24 22:17:16 -04:00
|
|
|
use typos::Status;
|
2019-08-08 11:24:50 -04:00
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-05-27 21:46:41 -04:00
|
|
|
pub struct BuiltIn {
|
2021-04-30 22:31:20 -04:00
|
|
|
locale: Option<varcon_core::Category>,
|
2020-05-27 21:46:41 -04:00
|
|
|
}
|
2019-08-08 11:24:50 -04:00
|
|
|
|
|
|
|
impl BuiltIn {
|
2021-03-01 13:27:07 -05:00
|
|
|
pub const fn new(locale: crate::config::Locale) -> Self {
|
2020-05-27 21:46:41 -04:00
|
|
|
Self {
|
|
|
|
locale: locale.category(),
|
|
|
|
}
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn correct_ident<'s, 'w>(
|
|
|
|
&'s self,
|
|
|
|
_ident: typos::tokens::Identifier<'w>,
|
2020-10-24 22:17:16 -04:00
|
|
|
) -> Option<Status<'s>> {
|
|
|
|
None
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:46:41 -04:00
|
|
|
pub fn correct_word<'s, 'w>(
|
|
|
|
&'s self,
|
|
|
|
word_token: typos::tokens::Word<'w>,
|
2020-10-24 22:17:16 -04:00
|
|
|
) -> Option<Status<'s>> {
|
2020-11-11 19:24:51 -05:00
|
|
|
if word_token.case() == typos::tokens::Case::None {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:46:41 -04:00
|
|
|
let word = word_token.token();
|
2021-06-30 11:12:17 -04:00
|
|
|
let word_case = unicase::UniCase::new(word);
|
|
|
|
let mut corrections = if let Some(corrections) = self.correct_with_dict(word_case) {
|
2021-05-15 20:06:04 -04:00
|
|
|
if corrections.is_empty() {
|
|
|
|
Status::Invalid
|
|
|
|
} else {
|
2021-05-15 20:06:14 -04:00
|
|
|
self.chain_with_vars(corrections)
|
2021-05-17 22:23:03 -04:00
|
|
|
}
|
2020-05-27 21:46:41 -04:00
|
|
|
} else {
|
2021-06-30 11:12:17 -04:00
|
|
|
self.correct_with_vars(word_case)?
|
2020-05-27 21:46:41 -04:00
|
|
|
};
|
|
|
|
corrections
|
2020-10-24 22:17:16 -04:00
|
|
|
.corrections_mut()
|
2021-12-14 16:21:47 -05:00
|
|
|
.for_each(|s| case_correct(s, word_token.case()));
|
2020-10-24 22:17:16 -04:00
|
|
|
Some(corrections)
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
2021-05-18 20:44:50 -04:00
|
|
|
}
|
2020-05-27 21:46:41 -04:00
|
|
|
|
2021-05-18 20:44:50 -04:00
|
|
|
#[cfg(feature = "dict")]
|
|
|
|
impl BuiltIn {
|
2020-10-24 22:17:16 -04:00
|
|
|
// Not using `Status` to avoid the allocations
|
2021-06-30 11:12:17 -04:00
|
|
|
fn correct_with_dict(&self, word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> {
|
2021-07-01 09:27:45 -04:00
|
|
|
typos_dict::WORD_TRIE.find(&word).copied()
|
2020-05-27 21:46:41 -04:00
|
|
|
}
|
2021-05-18 20:44:50 -04:00
|
|
|
}
|
2020-05-27 21:46:41 -04:00
|
|
|
|
2021-05-18 20:44:50 -04:00
|
|
|
#[cfg(not(feature = "dict"))]
|
|
|
|
impl BuiltIn {
|
2021-06-30 11:12:17 -04:00
|
|
|
fn correct_with_dict(&self, _word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> {
|
2021-04-30 22:31:20 -04:00
|
|
|
None
|
|
|
|
}
|
2021-05-18 20:44:50 -04:00
|
|
|
}
|
2021-04-30 22:31:20 -04:00
|
|
|
|
2021-05-18 20:44:50 -04:00
|
|
|
#[cfg(feature = "vars")]
|
|
|
|
impl BuiltIn {
|
2021-05-15 20:06:14 -04:00
|
|
|
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
|
2021-05-18 22:05:18 -04:00
|
|
|
if self.is_vars_enabled() {
|
|
|
|
let mut chained: Vec<_> = corrections
|
|
|
|
.iter()
|
2021-06-30 11:12:17 -04:00
|
|
|
.flat_map(|c| match self.correct_with_vars(unicase::UniCase::new(c)) {
|
2021-05-18 22:05:18 -04:00
|
|
|
Some(Status::Valid) | None => vec![Cow::Borrowed(*c)],
|
|
|
|
Some(Status::Corrections(vars)) => vars,
|
|
|
|
Some(Status::Invalid) => {
|
|
|
|
unreachable!("correct_with_vars should always have valid suggestions")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
if chained.len() != 1 {
|
|
|
|
chained.sort_unstable();
|
|
|
|
chained.dedup();
|
|
|
|
}
|
|
|
|
debug_assert!(!chained.is_empty());
|
|
|
|
Status::Corrections(chained)
|
|
|
|
} else {
|
|
|
|
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
|
2021-05-15 20:06:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:12:17 -04:00
|
|
|
fn correct_with_vars(&self, word: unicase::UniCase<&str>) -> Option<Status<'static>> {
|
|
|
|
if self.is_vars_enabled() {
|
2021-06-30 17:03:09 -04:00
|
|
|
typos_vars::VARS_TRIE
|
2021-06-30 11:12:17 -04:00
|
|
|
.find(&word)
|
|
|
|
.map(|variants| self.select_variant(variants))
|
2020-11-10 21:50:10 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-05-27 21:46:41 -04:00
|
|
|
}
|
|
|
|
|
2021-05-18 22:05:18 -04:00
|
|
|
fn is_vars_enabled(&self) -> bool {
|
|
|
|
#![allow(clippy::assertions_on_constants)]
|
|
|
|
debug_assert!(typos_vars::NO_INVALID);
|
|
|
|
self.locale.is_some()
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:46:41 -04:00
|
|
|
fn select_variant(
|
|
|
|
&self,
|
|
|
|
vars: &'static [(u8, &'static typos_vars::VariantsMap)],
|
2020-10-24 22:17:16 -04:00
|
|
|
) -> Status<'static> {
|
2020-05-27 21:46:41 -04:00
|
|
|
let var = vars[0];
|
|
|
|
let var_categories = unsafe {
|
|
|
|
// Code-genned from a checked category-set, so known to be safe
|
2021-05-13 11:20:15 -04:00
|
|
|
typos_vars::CategorySet::from_bits_unchecked(var.0)
|
2020-05-27 21:46:41 -04:00
|
|
|
};
|
|
|
|
if let Some(locale) = self.locale {
|
|
|
|
if var_categories.contains(locale) {
|
|
|
|
// Already valid for the current locale.
|
2020-10-24 22:17:16 -04:00
|
|
|
Status::Valid
|
2020-05-27 21:46:41 -04:00
|
|
|
} else {
|
2020-10-24 22:17:16 -04:00
|
|
|
Status::Corrections(
|
2020-05-27 21:46:41 -04:00
|
|
|
typos_vars::corrections(locale, *var.1)
|
|
|
|
.iter()
|
|
|
|
.copied()
|
2020-10-24 22:17:16 -04:00
|
|
|
.map(Cow::Borrowed)
|
2020-05-27 21:46:41 -04:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// All locales are valid
|
|
|
|
if var_categories.is_empty() {
|
|
|
|
// But the word is never valid.
|
2020-10-24 22:17:16 -04:00
|
|
|
let mut unique: Vec<_> = var
|
|
|
|
.1
|
|
|
|
.iter()
|
|
|
|
.flat_map(|v| v.iter())
|
|
|
|
.copied()
|
|
|
|
.map(Cow::Borrowed)
|
|
|
|
.collect();
|
2020-05-27 21:46:41 -04:00
|
|
|
unique.sort_unstable();
|
|
|
|
unique.dedup();
|
2020-10-24 22:17:16 -04:00
|
|
|
Status::Corrections(unique)
|
2020-05-27 21:46:41 -04:00
|
|
|
} else {
|
2020-10-24 22:17:16 -04:00
|
|
|
Status::Valid
|
2020-05-27 21:46:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:44:50 -04:00
|
|
|
#[cfg(not(feature = "vars"))]
|
|
|
|
impl BuiltIn {
|
|
|
|
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
|
|
|
|
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:12:17 -04:00
|
|
|
fn correct_with_vars(&self, _word: unicase::UniCase<&str>) -> Option<Status<'static>> {
|
2021-05-18 20:44:50 -04:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:50 -04:00
|
|
|
impl typos::Dictionary for BuiltIn {
|
2020-10-24 22:17:16 -04:00
|
|
|
fn correct_ident<'s, 'w>(&'s self, ident: typos::tokens::Identifier<'w>) -> Option<Status<'s>> {
|
2019-08-08 11:24:50 -04:00
|
|
|
BuiltIn::correct_ident(self, ident)
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:17:16 -04:00
|
|
|
fn correct_word<'s, 'w>(&'s self, word: typos::tokens::Word<'w>) -> Option<Status<'s>> {
|
2019-08-08 11:24:50 -04:00
|
|
|
BuiltIn::correct_word(self, word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:17:16 -04:00
|
|
|
fn case_correct(correction: &mut Cow<'_, str>, case: Case) {
|
2019-08-08 11:24:50 -04:00
|
|
|
match case {
|
2020-10-24 22:17:16 -04:00
|
|
|
Case::Lower | Case::None => (),
|
|
|
|
Case::Title => match correction {
|
|
|
|
Cow::Borrowed(s) => {
|
|
|
|
let mut s = String::from(*s);
|
|
|
|
s[0..1].make_ascii_uppercase();
|
|
|
|
*correction = s.into();
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
2020-10-24 22:17:16 -04:00
|
|
|
Cow::Owned(s) => {
|
|
|
|
s[0..1].make_ascii_uppercase();
|
|
|
|
}
|
|
|
|
},
|
2021-04-21 21:36:35 -04:00
|
|
|
Case::Upper => match correction {
|
2020-10-24 22:17:16 -04:00
|
|
|
Cow::Borrowed(s) => {
|
|
|
|
let mut s = String::from(*s);
|
|
|
|
s.make_ascii_uppercase();
|
|
|
|
*correction = s.into();
|
|
|
|
}
|
|
|
|
Cow::Owned(s) => {
|
|
|
|
s.make_ascii_uppercase();
|
|
|
|
}
|
|
|
|
},
|
2019-08-08 11:24:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 21:12:49 -04:00
|
|
|
pub struct Override<'i, 'w, D> {
|
2020-11-10 21:23:38 -05:00
|
|
|
identifiers: HashMap<&'i str, Status<'i>, ahash::RandomState>,
|
|
|
|
words: HashMap<unicase::UniCase<&'w str>, Status<'w>, ahash::RandomState>,
|
2020-09-02 21:12:49 -04:00
|
|
|
inner: D,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i, 'w, D: typos::Dictionary> Override<'i, 'w, D> {
|
|
|
|
pub fn new(inner: D) -> Self {
|
|
|
|
Self {
|
2020-10-24 22:17:16 -04:00
|
|
|
identifiers: Default::default(),
|
|
|
|
words: Default::default(),
|
2020-09-02 21:12:49 -04:00
|
|
|
inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:17:16 -04:00
|
|
|
pub fn identifiers<I: Iterator<Item = (&'i str, &'i str)>>(&mut self, identifiers: I) {
|
|
|
|
self.identifiers = Self::interpret(identifiers).collect();
|
2020-09-02 21:12:49 -04:00
|
|
|
}
|
|
|
|
|
2020-10-24 22:17:16 -04:00
|
|
|
pub fn words<I: Iterator<Item = (&'w str, &'w str)>>(&mut self, words: I) {
|
|
|
|
self.words = Self::interpret(words)
|
|
|
|
.map(|(k, v)| (UniCase::new(k), v))
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:19:12 -05:00
|
|
|
fn interpret<'z, I: Iterator<Item = (&'z str, &'z str)>>(
|
2020-10-24 22:17:16 -04:00
|
|
|
cases: I,
|
|
|
|
) -> impl Iterator<Item = (&'z str, Status<'z>)> {
|
|
|
|
cases.map(|(typo, correction)| {
|
|
|
|
let correction = if typo == correction {
|
|
|
|
Status::Valid
|
|
|
|
} else if correction.is_empty() {
|
|
|
|
Status::Invalid
|
|
|
|
} else {
|
|
|
|
Status::Corrections(vec![Cow::Borrowed(correction)])
|
|
|
|
};
|
|
|
|
(typo, correction)
|
|
|
|
})
|
2020-09-02 21:12:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'i, 'w, D: typos::Dictionary> typos::Dictionary for Override<'i, 'w, D> {
|
2020-10-24 22:17:16 -04:00
|
|
|
fn correct_ident<'s, 't>(&'s self, ident: typos::tokens::Identifier<'t>) -> Option<Status<'s>> {
|
2020-11-10 21:19:12 -05:00
|
|
|
// Skip hashing if we can
|
|
|
|
if !self.identifiers.is_empty() {
|
|
|
|
self.identifiers
|
|
|
|
.get(ident.token())
|
|
|
|
.map(|c| c.borrow())
|
|
|
|
.or_else(|| self.inner.correct_ident(ident))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-09-02 21:12:49 -04:00
|
|
|
}
|
|
|
|
|
2020-10-24 22:17:16 -04:00
|
|
|
fn correct_word<'s, 't>(&'s self, word: typos::tokens::Word<'t>) -> Option<Status<'s>> {
|
2020-11-11 19:24:51 -05:00
|
|
|
if word.case() == typos::tokens::Case::None {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:19:12 -05:00
|
|
|
// Skip hashing if we can
|
2020-11-11 13:22:23 -05:00
|
|
|
let custom = if !self.words.is_empty() {
|
2020-11-10 21:19:12 -05:00
|
|
|
let w = UniCase::new(word.token());
|
|
|
|
// HACK: couldn't figure out the lifetime issue with replacing `cloned` with `borrow`
|
2020-11-11 13:22:23 -05:00
|
|
|
self.words.get(&w).cloned()
|
2020-11-10 21:19:12 -05:00
|
|
|
} else {
|
|
|
|
None
|
2020-11-11 13:22:23 -05:00
|
|
|
};
|
|
|
|
custom.or_else(|| self.inner.correct_word(word))
|
2020-09-02 21:12:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:50 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
2021-05-17 22:23:03 -04:00
|
|
|
#[cfg(feature = "dict")]
|
|
|
|
#[test]
|
|
|
|
fn test_dict_correct() {
|
|
|
|
let dict = BuiltIn::new(crate::config::Locale::default());
|
|
|
|
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
|
|
|
|
"finallizes",
|
|
|
|
typos::tokens::Case::Lower,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(
|
|
|
|
correction,
|
|
|
|
Some(Status::Corrections(vec!["finalizes".into()]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "vars")]
|
|
|
|
#[test]
|
|
|
|
fn test_varcon_no_locale() {
|
|
|
|
let dict = BuiltIn::new(crate::config::Locale::En);
|
|
|
|
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
|
|
|
|
"finalizes",
|
|
|
|
typos::tokens::Case::Lower,
|
|
|
|
0,
|
|
|
|
));
|
2021-05-18 22:05:18 -04:00
|
|
|
assert_eq!(correction, None);
|
2021-05-17 22:23:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "vars")]
|
|
|
|
#[test]
|
|
|
|
fn test_varcon_same_locale() {
|
|
|
|
let dict = BuiltIn::new(crate::config::Locale::EnUs);
|
|
|
|
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
|
|
|
|
"finalizes",
|
|
|
|
typos::tokens::Case::Lower,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(correction, Some(Status::Valid));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "vars")]
|
|
|
|
#[test]
|
|
|
|
fn test_varcon_different_locale() {
|
|
|
|
let dict = BuiltIn::new(crate::config::Locale::EnGb);
|
|
|
|
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
|
|
|
|
"finalizes",
|
|
|
|
typos::tokens::Case::Lower,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(
|
|
|
|
correction,
|
|
|
|
Some(Status::Corrections(vec!["finalises".into()]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "dict", feature = "vars"))]
|
|
|
|
#[test]
|
|
|
|
fn test_dict_to_varcon() {
|
|
|
|
let dict = BuiltIn::new(crate::config::Locale::EnGb);
|
|
|
|
let correction = dict.correct_word(typos::tokens::Word::new_unchecked(
|
|
|
|
"finallizes",
|
|
|
|
typos::tokens::Case::Lower,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(
|
|
|
|
correction,
|
|
|
|
Some(Status::Corrections(vec!["finalises".into()]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:50 -04:00
|
|
|
#[test]
|
|
|
|
fn test_case_correct() {
|
|
|
|
let cases = [
|
|
|
|
("foo", Case::Lower, "foo"),
|
|
|
|
("foo", Case::None, "foo"),
|
|
|
|
("foo", Case::Title, "Foo"),
|
2021-04-21 21:36:35 -04:00
|
|
|
("foo", Case::Upper, "FOO"),
|
2019-08-08 11:24:50 -04:00
|
|
|
("fOo", Case::None, "fOo"),
|
|
|
|
];
|
|
|
|
for (correction, case, expected) in cases.iter() {
|
2020-10-24 22:17:16 -04:00
|
|
|
let mut actual = Cow::Borrowed(*correction);
|
|
|
|
case_correct(&mut actual, *case);
|
|
|
|
assert_eq!(*expected, actual);
|
|
|
|
|
|
|
|
let mut actual = Cow::Owned(String::from(*correction));
|
|
|
|
case_correct(&mut actual, *case);
|
2019-08-08 11:24:50 -04:00
|
|
|
assert_eq!(*expected, actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|