A fast, lightweight spell checker built in pure Python β no external modules required.
It handles large dictionaries and suggests smart corrections using edit distance.
We calculate how many changes are needed to convert a user word into each dictionary word using:
- βοΈ Insert
- β Delete
- π Replace
This is done using a custom Levenshtein Distance algorithm optimized with early exit.
If absolute length difference > max_dist: β Return max_dist + 1 (skip this word)
Initialize prev[] with values from 0 to len(s2)
For each character in s1: a. Start a new row curr[] with [i + 1]
b. For each character in s2: i. If characters match, cost = 0 Else, cost = 1 ii. Calculate insert, delete, replace costs iii. curr[j + 1] = min(insert, delete, replace)
c. If min(curr) > max_dist: β Early exit (too different)
d. Set prev = curr
Return prev[-1] as final edit distance
yaml Copy Edit
Input: word_to_check β the user input word dictionary β list of words max_dist β allowed edit distance (default: 2) max_suggestions β number of best matches to return
Steps:
Lowercase the word
For each word in the dictionary: a. Skip if length difference > max_dist b. Compute edit distance c. If distance β€ max_dist β store it
Sort suggestions by (distance, then alphabet)
Return top N suggestions
python Copy Edit
python from main import FastSpellChecker
with open("ENG.txt", "r") as f: word_list = f.readlines()
checker = FastSpellChecker(word_list)
print(checker.suggest("appl")) # ['apple', 'apply', 'applet']
text = "see is a goof bay" print(checker.correct_string(text)) # he is a good boy
β Features βοΈ Pure Python β no third-party libraries
π Fast suggestion system with early-exit
π Can handle large dictionaries (tested with 400,000+ words)
π§ Supports sentence-level correction with correct_string()
π Word distance cutoff to speed up search