harper_core/spell/dictionary.rs
1use blanket::blanket;
2use std::borrow::Cow;
3
4use super::FuzzyMatchResult;
5use super::WordId;
6use crate::DictWordMetadata;
7
8/// An in-memory database that contains everything necessary to parse and analyze English text.
9///
10/// See also: [`super::FstDictionary`] and [`super::MutableDictionary`].
11#[blanket(derive(Arc))]
12pub trait Dictionary: Send + Sync {
13 /// Check if the dictionary contains any capitalization of a given word.
14 fn contains_word(&self, word: &[char]) -> bool;
15 /// Check if the dictionary contains any capitalization of a given word.
16 fn contains_word_str(&self, word: &str) -> bool;
17 /// Check if the dictionary contains the exact capitalization of a given word.
18 fn contains_exact_word(&self, word: &[char]) -> bool;
19 /// Check if the dictionary contains the exact capitalization of a given word.
20 fn contains_exact_word_str(&self, word: &str) -> bool;
21 /// Gets best fuzzy match from dictionary
22 fn fuzzy_match(
23 &'_ self,
24 word: &[char],
25 max_distance: u8,
26 max_results: usize,
27 ) -> Vec<FuzzyMatchResult<'_>>;
28 /// Gets best fuzzy match from dictionary
29 fn fuzzy_match_str(
30 &'_ self,
31 word: &str,
32 max_distance: u8,
33 max_results: usize,
34 ) -> Vec<FuzzyMatchResult<'_>>;
35 fn get_correct_capitalization_of(&self, word: &[char]) -> Option<&'_ [char]>;
36 /// Get the associated [`DictWordMetadata`] for any capitalization of a given word.
37 fn get_word_metadata(&self, word: &[char]) -> Option<Cow<'_, DictWordMetadata>>;
38 /// Get the associated [`DictWordMetadata`] for any capitalization of a given word.
39 /// If the word isn't in the dictionary, the resulting metadata will be
40 /// empty.
41 fn get_word_metadata_str(&self, word: &str) -> Option<Cow<'_, DictWordMetadata>>;
42
43 /// Iterate over the words in the dictionary.
44 fn words_iter(&self) -> Box<dyn Iterator<Item = &'_ [char]> + Send + '_>;
45
46 /// The number of words in the dictionary.
47 fn word_count(&self) -> usize;
48
49 /// Returns the correct capitalization of the word with the given ID.
50 fn get_word_from_id(&self, id: &WordId) -> Option<&[char]>;
51
52 /// Look for words with a specific prefix
53 fn find_words_with_prefix(&self, prefix: &[char]) -> Vec<Cow<'_, [char]>>;
54
55 /// Look for words that share a prefix with the provided word
56 fn find_words_with_common_prefix(&self, word: &[char]) -> Vec<Cow<'_, [char]>>;
57}