Skip to content

Commit 1ff6693

Browse files
authored
add frequency number to word_frequency.add allowing for more priority (#132)
1 parent 35b2c4e commit 1ff6693

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

spellchecker/spellchecker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,14 @@ def load_words(self, words: typing.Iterable[KeyT]) -> None:
471471
self._dictionary.update([word if self._case_sensitive else word.lower() for word in words])
472472
self._update_dictionary()
473473

474-
def add(self, word: KeyT) -> None:
474+
def add(self, word: KeyT, val: int = 1) -> None:
475475
"""Add a word to the word frequency list
476476
477477
Args:
478-
word (str): The word to add"""
478+
word (str): The word to add
479+
val (int): The number of times to insert the word"""
479480
word = ensure_unicode(word)
480-
self.load_words([word])
481+
self.load_json({word if self._case_sensitive else word.lower(): val})
481482

482483
def remove_words(self, words: typing.Iterable[KeyT]) -> None:
483484
"""Remove a list of words from the word frequency list

tests/spellchecker_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ def test_add_word(self):
269269
spell.word_frequency.add("appt")
270270
self.assertEqual(spell["appt"], 1)
271271

272+
def test_add_word_priority(self):
273+
"""test adding a word with larger priority"""
274+
spell = SpellChecker()
275+
self.assertEqual(spell["appt"], 0)
276+
spell.word_frequency.add("appt", 5000)
277+
self.assertEqual(spell["appt"], 5000)
278+
272279
def test_checking_odd_word(self):
273280
"""test checking a word that is really a number"""
274281
spell = SpellChecker()
@@ -334,7 +341,7 @@ def test_capitalization_when_case_sensitive_defaults_to_false(self):
334341
def test_large_words(self):
335342
"""test checking for words that are clearly larger than the largest dictionary word"""
336343
spell = SpellChecker(language=None, distance=2)
337-
spell.word_frequency.add("Bob")
344+
spell.word_frequency.add("Bob", 1)
338345

339346
words = ["Bb", "bb", "BB"]
340347
self.assertEqual(spell.unknown(words), {"bb"})

0 commit comments

Comments
 (0)