SymSpellPy
SymSpellPy is a Python port of the SymSpell spelling corrector, designed for high performance and low memory consumption. It provides fast fuzzy search and spell correction capabilities, capable of handling large dictionaries. The library is actively maintained, with a recent major release (v6.x) and a consistent release cadence addressing new features, bug fixes, and performance improvements.
Warnings
- breaking SymSpellPy dropped support for Python 3.6 in v6.7.7 and Python 3.7/3.8 in v6.8.0. The library now requires Python 3.9 or higher. Ensure your environment meets this requirement.
- breaking As of v6.9.0, frequency counts provided to dictionary methods (e.g., `create_dictionary_entry`, `load_dictionary`) must be 64-bit integers. Passing smaller integers or non-integer types for counts may lead to unexpected behavior or errors.
- breaking In v6.9.0, internal argument names `string1` and `string2` in distance comparison methods were renamed. If you have custom distance comparers or interact with these methods directly, you may need to update your code.
- gotcha Prior to v6.7.7, `symspellpy` could inadvertently modify the root logger's configuration. Since v6.7.7, it correctly configures its own module logger. If your application relied on `symspellpy`'s old logging behavior, you may need to adjust your logging setup.
- gotcha When using `ignore_term_with_digits=True` in `lookup` or `lookup_compound` methods, it is recommended to also set `ignore_non_words=True` for comprehensive filtering, as clarified in v6.7.3. Failing to do so might result in inconsistent filtering of terms with digits.
Install
-
pip install symspellpy
Imports
- SymSpell
from symspellpy import SymSpell
- Verbosity
from symspellpy import Verbosity
- DistanceAlgorithm
from symspellpy.editdistance import DistanceAlgorithm
Quickstart
from symspellpy import SymSpell, Verbosity
import os
# Initialize SymSpell
sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
# A simple corpus for demonstration (in a real app, load from file)
dictionary_corpus = {"apple": 10, "banana": 8, "aple": 2, "apply": 5}
for term, count in dictionary_corpus.items():
sym_spell.create_dictionary_entry(term, count)
# Example usage: lookup a word
input_term = "aple"
suggestions = sym_spell.lookup(input_term, Verbosity.CLOSEST, max_edit_distance=2)
print(f"Input: '{input_term}'")
print("Suggestions:")
for suggestion in suggestions:
print(f" {suggestion.term} (distance: {suggestion.distance}, count: {suggestion.count})")
# Example usage: lookup compound words
input_phrase = "whereis th elibary"
suggestions_compound = sym_spell.lookup_compound(input_phrase, max_edit_distance=2)
print(f"\nInput phrase: '{input_phrase}'")
print("Compound suggestions:")
for suggestion in suggestions_compound:
print(f" {suggestion.term} (distance: {suggestion.distance}, count: {suggestion.count})")