From 1ff66936df6fe7c82ff94fa4ce91952079a9891f Mon Sep 17 00:00:00 2001 From: Tyler Barrus Date: Mon, 29 Aug 2022 19:48:32 -0400 Subject: [PATCH] add frequency number to word_frequency.add allowing for more priority (#132) --- spellchecker/spellchecker.py | 7 ++++--- tests/spellchecker_test.py | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/spellchecker/spellchecker.py b/spellchecker/spellchecker.py index 760b9c2..7c2fb8e 100644 --- a/spellchecker/spellchecker.py +++ b/spellchecker/spellchecker.py @@ -471,13 +471,14 @@ def load_words(self, words: typing.Iterable[KeyT]) -> None: self._dictionary.update([word if self._case_sensitive else word.lower() for word in words]) self._update_dictionary() - def add(self, word: KeyT) -> None: + def add(self, word: KeyT, val: int = 1) -> None: """Add a word to the word frequency list Args: - word (str): The word to add""" + word (str): The word to add + val (int): The number of times to insert the word""" word = ensure_unicode(word) - self.load_words([word]) + self.load_json({word if self._case_sensitive else word.lower(): val}) def remove_words(self, words: typing.Iterable[KeyT]) -> None: """Remove a list of words from the word frequency list diff --git a/tests/spellchecker_test.py b/tests/spellchecker_test.py index 2e3ee57..e9db470 100644 --- a/tests/spellchecker_test.py +++ b/tests/spellchecker_test.py @@ -269,6 +269,13 @@ def test_add_word(self): spell.word_frequency.add("appt") self.assertEqual(spell["appt"], 1) + def test_add_word_priority(self): + """test adding a word with larger priority""" + spell = SpellChecker() + self.assertEqual(spell["appt"], 0) + spell.word_frequency.add("appt", 5000) + self.assertEqual(spell["appt"], 5000) + def test_checking_odd_word(self): """test checking a word that is really a number""" spell = SpellChecker() @@ -334,7 +341,7 @@ def test_capitalization_when_case_sensitive_defaults_to_false(self): def test_large_words(self): """test checking for words that are clearly larger than the largest dictionary word""" spell = SpellChecker(language=None, distance=2) - spell.word_frequency.add("Bob") + spell.word_frequency.add("Bob", 1) words = ["Bb", "bb", "BB"] self.assertEqual(spell.unknown(words), {"bb"})