Skip to content

Commit

Permalink
Feature/iter (#93)
Browse files Browse the repository at this point in the history
* add __iter__ support
* minor update; update docs; etc
  • Loading branch information
barrust authored Mar 9, 2021
1 parent 777248c commit ab8e985
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------------------------------------

Expand Down
17 changes: 17 additions & 0 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down
10 changes: 10 additions & 0 deletions spellchecker/spellchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/spellchecker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit ab8e985

Please sign in to comment.