SymSpellPy

6.9.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize SymSpell, manually create dictionary entries, and perform basic spell correction for single words and compound phrases. In a production environment, dictionaries are typically loaded from text files using `sym_spell.load_dictionary()`.

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})")

view raw JSON →