diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e0896e..540d3e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Version 0.6.1 (Future) * Deprecated `spell.word_probability` since the name makes it seem that it is building a true probability; use `spell.word_usage_frequency` instead * Added Russian language dictionary; [#91](https://github.com/barrust/pyspellchecker/pull/91) Thanks [@sviperm](https://github.com/sviperm) +* Include `__iter__` to both the `SpellChecker` and `WordFrequency` objects ## Version 0.6.0 * Removed **python 2.7** support diff --git a/README.rst b/README.rst index de4384d..1fc4e71 100644 --- a/README.rst +++ b/README.rst @@ -122,6 +122,30 @@ check class or after the fact. spell.distance = 2 # set the distance parameter back to the default +Non-English Dictionaries +------------------------------------------------------------------------------- + +``pyspellchecker`` supports several default dictionaries as part of the default +package. Each is simple to use when initializing the dictionary: + +.. code:: python + + from spellchecker import SpellChecker + + english = SpellChecker() # the default is English (language='en') + spanish = SpellChecker(language='es') # use the Spanish Dictionary + russian = SpellChecker(language='ru') # use the Russian Dictionary + + +The currently supported dictionaries are: + +* English - 'en' +* Spanish - 'es' +* French - 'fr' +* Portuguese - 'pt' +* German - 'de' +* Russian - 'ru' + Dictionary Creation and Updating ------------------------------------------------------------------------------- diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 9604037..f3dac5a 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -177,6 +177,23 @@ Removing words is as simple as adding words: # or remove a single word spell.word_frequency.remove('meh') +Iterating Over a Dictionary ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +Iterating over the dictionary is as easy as writing a simple for loop: + +.. code:: python + + from spellchecker import SpellChecker + + spell = SpellChecker() + + for word in spell: + print("{}: {}".format(word, spell[word])) + +The iterator returns the word. To get the number of times that the word is +found in the WordFrequency object one can use a simple lookup. + How to Build a New Dictionary +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/spellchecker/spellchecker.py b/spellchecker/spellchecker.py index 85df817..220c082 100644 --- a/spellchecker/spellchecker.py +++ b/spellchecker/spellchecker.py @@ -71,6 +71,11 @@ def __getitem__(self, key): key = ensure_unicode(key) return self._word_frequency[key] + def __iter__(self): + """ setup iter support """ + for word in self._word_frequency.dictionary: + yield word + @property def word_frequency(self): """ WordFrequency: An encapsulation of the word frequency `dictionary` @@ -344,6 +349,11 @@ def __getitem__(self, key): key = key if self._case_sensitive else key.lower() return self._dictionary[key] + def __iter__(self): + """ turn on iter support """ + for word in self._dictionary: + yield word + def pop(self, key, default=None): """ Remove the key and return the associated value or default if not found diff --git a/tests/spellchecker_test.py b/tests/spellchecker_test.py index ba0254a..bf13fa4 100644 --- a/tests/spellchecker_test.py +++ b/tests/spellchecker_test.py @@ -414,3 +414,29 @@ def test_split_words(self): spell = SpellChecker() res = spell.split_words("This isn't a good test, but it is a test!!!!") self.assertEqual(set(res), set(["this", "isn't", "a", "good", "test", "but", "it", "is", "a", "test"])) + + def test_iter_spellchecker(self): + """ Test using the iterator on the SpellChecker """ + here = os.path.dirname(__file__) + filepath = '{}/resources/small_dictionary.json'.format(here) + + spell = SpellChecker(language=None, local_dictionary=filepath) + + cnt = 0 + for word in spell: + self.assertTrue(word in spell) + cnt += 1 + self.assertEqual(cnt, len(spell.word_frequency.dictionary)) + + def test_iter_word_frequency(self): + """ Test using the iterator on the WordFrequency """ + here = os.path.dirname(__file__) + filepath = '{}/resources/small_dictionary.json'.format(here) + + spell = SpellChecker(language=None, local_dictionary=filepath) + + cnt = 0 + for word in spell.word_frequency: + self.assertTrue(word in spell) + cnt += 1 + self.assertEqual(cnt, len(spell.word_frequency.dictionary))