Expose the Checker type

This was a TODO comment before - we can expose the Checker type to
allow customizing its behavior like the Suggester does with ngram
suggestions.
This commit is contained in:
Michael Davis
2025-01-27 19:09:39 -05:00
parent ad65f0a3f4
commit 6b3d6f6985
3 changed files with 37 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ use crate::{
aff::{
AffData, Affix, AffixKind, CompoundPattern, Pfx, Prefix, Sfx, Suffix, HIDDEN_HOMONYM_FLAG,
},
alloc::{string::String, vec::Vec},
alloc::{fmt, string::String, vec::Vec},
classify_casing, erase_chars, AffixingMode, Casing, Dictionary, Flag, FlagSet, Stem, WordList,
AT_COMPOUND_BEGIN, AT_COMPOUND_END, AT_COMPOUND_MIDDLE, FULL_WORD, MAX_WORD_LEN,
};
@@ -23,14 +23,23 @@ macro_rules! flag {
}};
}
// TODO: expose type and add options to it?
pub(crate) struct Checker<'a, S: BuildHasher> {
/// A wrapper struct for a dictionary that allows customizing checking behavior.
#[derive(Clone, Copy)]
pub struct Checker<'a, S: BuildHasher> {
pub(crate) words: &'a WordList<S>,
pub(crate) aff: &'a AffData,
}
impl<S: BuildHasher> fmt::Debug for Checker<'_, S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Checker")
.field("words", &self.words.len())
.finish_non_exhaustive()
}
}
impl<'a, S: BuildHasher> Checker<'a, S> {
pub fn new(dict: &'a Dictionary<S>) -> Self {
pub(crate) fn new(dict: &'a Dictionary<S>) -> Self {
Self {
words: &dict.words,
aff: &dict.aff_data,

View File

@@ -31,11 +31,11 @@ mod umbra_slice;
pub use aff::parser::{
ParseDictionaryError, ParseDictionaryErrorKind, ParseDictionaryErrorSource, ParseFlagError,
};
pub use checker::Checker;
pub use suggester::Suggester;
use crate::alloc::{borrow::Cow, slice, string::String, vec::Vec};
use aff::AffData;
use checker::Checker;
use core::{cmp::Ordering, fmt, hash::BuildHasher};
use hash_bag::HashBag;
@@ -186,7 +186,14 @@ impl<S: BuildHasher> Dictionary<S> {
/// assert!(!dict.check("optimise")); // allowed by en_GB but not en_US.
/// ```
pub fn check(&self, word: &str) -> bool {
Checker::new(self).check(word)
self.checker().check(word)
}
/// Creates a [Checker] that borrows this dictionary.
///
/// The [Checker] type can be used to customize the checking behavior. See the [Checker] docs.
pub fn checker(&self) -> Checker<S> {
Checker::new(self)
}
/// Fills the given vec with possible corrections from the dictionary for the given word.
@@ -201,7 +208,7 @@ impl<S: BuildHasher> Dictionary<S> {
/// The [Suggester] type can be used to customize the suggestion behavior (for example to
/// disable ngram suggestions). See the [Suggester] docs.
pub fn suggester(&self) -> Suggester<S> {
Suggester::new(Checker::new(self))
self.checker().into_suggester()
}
/// Adds a word to the dictionary.

View File

@@ -21,6 +21,7 @@ macro_rules! has_flag {
/// A wrapper struct for a dictionary that allows customizing suggestion behavior.
///
/// Currently only [ngram suggestions](Suggester::with_ngram_suggestions) may be configured.
#[derive(Clone, Copy)]
pub struct Suggester<'a, S: BuildHasher> {
checker: Checker<'a, S>,
ngram_suggest: bool,
@@ -34,14 +35,26 @@ impl<S: BuildHasher> fmt::Debug for Suggester<'_, S> {
}
}
impl<'a, S: BuildHasher> Checker<'a, S> {
/// Converts the [Checker] into a [Suggester] with default suggester options set.
pub fn into_suggester(self) -> Suggester<'a, S> {
Suggester::new(self)
}
}
impl<'a, S: BuildHasher> Suggester<'a, S> {
pub(crate) fn new(checker: Checker<'a, S>) -> Self {
fn new(checker: Checker<'a, S>) -> Self {
Self {
checker,
ngram_suggest: true,
}
}
/// Returns the underlying [Checker] used by this suggester.
pub fn into_checker(self) -> Checker<'a, S> {
self.checker
}
/// Enables or disables the suggester from finding suggestions based on "ngram similarity."
///
/// Ngram similarity is a bespoke string similarity metric used by the suggester to find