Skip to content

Commit 0fa2e22

Browse files
committed
Add Dictionary::remove_stem
This is the same as `Hunspell::remove`: the given stem is marked with the FORBIDDENWORD flag if it is not already.
1 parent 64acc9e commit 0fa2e22

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/hash_bag.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ where
8888
/// Gets all values for any pairs in the table with the given key.
8989
///
9090
/// This is the same as `get` but only exclusive borrows of the values are returned.
91-
#[allow(unused)]
9291
pub fn get_mut<'bag, 'key, Q>(&'bag mut self, k: &'key Q) -> GetAllMutIter<'bag, 'key, Q, K, V>
9392
where
9493
K: Borrow<Q>,

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,47 @@ impl<S: BuildHasher> Dictionary<S> {
252252
self.words.insert(word, flagset);
253253
Ok(())
254254
}
255+
256+
/// Removes the given stem from the dictionary.
257+
///
258+
/// Once removed, `check` will return `false` for all conjugations of the stem. For example
259+
/// "adventuring" in the `en_US` dictionary is based on the stem "adventure" with the "ing"
260+
/// suffix applied. Removing "adventuring" does nothing while removing "adventure" removes
261+
/// "adventuring", "adventured", etc..
262+
///
263+
/// This function returns `true` if any stem in the dictionary is removed, otherwise `false`.
264+
///
265+
/// # Example
266+
///
267+
/// ```
268+
/// let aff = std::fs::read_to_string("./vendor/en_US/en_US.aff").unwrap();
269+
/// let dic = std::fs::read_to_string("./vendor/en_US/en_US.dic").unwrap();
270+
/// let mut dict = spellbook::Dictionary::new(&aff, &dic).unwrap();
271+
///
272+
/// assert!(dict.check("adventure"));
273+
/// assert!(dict.check("adventuring"));
274+
///
275+
/// // `remove` only works on stems.
276+
/// assert!(!dict.remove_stem("adventuring"));
277+
/// assert!(dict.check("adventure"));
278+
/// assert!(dict.check("adventuring"));
279+
/// // Removing the stem removes all conjugations.
280+
/// assert!(dict.remove_stem("adventure"));
281+
/// assert!(!dict.check("adventure"));
282+
/// assert!(!dict.check("adventuring"));
283+
/// // Once removed, removing the same stem again is a no-op.
284+
/// assert!(!dict.remove_stem("adventure"));
285+
/// ```
286+
pub fn remove_stem(&mut self, word: &str) -> bool {
287+
let mut did_remove = false;
288+
for flags in self.words.get_mut(word) {
289+
if !flags.contains(&self.aff_data.options.forbidden_word_flag) {
290+
did_remove = true;
291+
*flags = flags.with_flag(self.aff_data.options.forbidden_word_flag);
292+
}
293+
}
294+
did_remove
295+
}
255296
}
256297

257298
impl fmt::Debug for Dictionary {

0 commit comments

Comments
 (0)