Skip to content

Commit

Permalink
add frequency number to word_frequency.add allowing for more priority (
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust authored Aug 29, 2022
1 parent 35b2c4e commit 1ff6693
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 4 additions & 3 deletions spellchecker/spellchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/spellchecker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"})
Expand Down

0 comments on commit 1ff6693

Please sign in to comment.