Skip to content

Commit

Permalink
Add and Remove words (barrust#14)
Browse files Browse the repository at this point in the history
* add ability to add and remove words easily
* better english dictionary
  • Loading branch information
barrust authored Sep 27, 2018
1 parent 2fbb1bf commit 41c6012
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Binary file modified spellchecker/resources/en.json.gz
Binary file not shown.
26 changes: 25 additions & 1 deletion spellchecker/spellchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,34 @@ def load_words(self, words):
''' Load a list of words from which to generate a word frequency list
Args:
text (list): The list of words to be loaded '''
words (list): The list of words to be loaded '''
self._dictionary.update([word.lower() for word in words])
self._update_dictionary()

def add(self, word):
''' Add a word to the word frequency list
Args:
word (str): The word to add '''
self.load_words([word.lower()])

def remove_words(self, words):
''' Remove a list of words from the word frequency list
Args:
words (list): The list of words to remove '''
for word in words:
self._dictionary.pop(word.lower())
self._update_dictionary()

def remove(self, word):
''' Remove a word from the word frequency list
Args:
word (str): The word to remove '''
self._dictionary.pop(word.lower())
self._update_dictionary()

def _update_dictionary(self):
''' Update the word frequency object '''
self._total_words = sum(self._dictionary.values())
Expand Down
21 changes: 21 additions & 0 deletions tests/spellchecker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,24 @@ def test_load_text_file(self):
self.assertFalse('awesome' in spell)
self.assertTrue(spell['whale'])
self.assertTrue('waves' in spell)

def test_remove_words(self):
''' test is a word is removed '''
spell = SpellChecker()
self.assertEqual(spell['the'], 6187925)
spell.word_frequency.remove_words(['the'])
self.assertEqual(spell['the'], 0)

def test_remove_word(self):
''' test a single word removed '''
spell = SpellChecker()
self.assertEqual(spell['teh'], 6)
spell.word_frequency.remove('teh')
self.assertEqual(spell['teh'], 0)

def test_add_word(self):
''' test adding a word '''
spell = SpellChecker()
self.assertEqual(spell['meh'], 0)
spell.word_frequency.add('meh')
self.assertEqual(spell['meh'], 1)

0 comments on commit 41c6012

Please sign in to comment.