pyspellchecker
pyspellchecker is a pure Python spell checker based on Peter Norvig's blog post, utilizing a Levenshtein Distance algorithm to find permutations and compare them to a word frequency list. It supports multiple languages including English, Spanish, German, French, Portuguese, Arabic, and Basque. The current version is 0.9.0, and it is actively maintained with updates to support newer Python versions.
Warnings
- breaking Version 0.9.0 removed support for Python 3.8 and 3.9. Users on these Python versions should use an older pyspellchecker version (e.g., <=0.8.x).
- breaking As of version 0.9.0, a `ValueError` is raised if `language` is used simultaneously with `case_sensitive=True` or `local_dictionary` in the `SpellChecker` constructor, as these options are mutually exclusive.
- gotcha For longer words or performance-critical applications, it is highly recommended to set the `distance` parameter to 1 (default is 2) during `SpellChecker` initialization or afterwards. A higher distance increases computation time significantly.
- gotcha Since version 0.7.0, `spell.correction()` and `spell.candidates()` will return `None` if no valid corrections or candidates are found for a given word. Previously, they might have returned the original word or an empty set.
- gotcha pyspellchecker works by comparing permutations to a word frequency list and does not inherently understand word context. This means it may suggest corrections that are grammatically or contextually incorrect in a sentence (e.g., correcting 'their' to 'there').
Install
-
pip install pyspellchecker
Imports
- SpellChecker
from spellchecker import SpellChecker
Quickstart
from spellchecker import SpellChecker
spell = SpellChecker()
mispelled_words = spell.unknown(['something', 'is', 'hapenning', 'here'])
for word in mispelled_words:
print(f"Correction for '{word}': {spell.correction(word)}")
print(f"Candidates for '{word}': {spell.candidates(word)}")