Spell Correction
Spell correction turns a typo into the word you meant, so a search for aple still finds apple by weighing nearby words and how common each one is.
Why Does This Exist?
Real text is messy. You type fast on a phone, you drop a letter, you swap two. A search box that only knows exact words then fails on a query it could have answered. One missing letter should not cost you the right page.
Spell correction sits in front of the rest of the pipeline to fix that. It takes a typed token such as aple, proposes nearby known words, and returns the intended word such as apple. Search, chatbots, and dictation all lean on it because human input stays noisy no matter how good the downstream model gets.
This page covers isolated word correction with the noisy channel rule, plus where context must step in. The cleaning step around it is text normalization, which lowercases and strips noise before any lookup. The splitting step is tokenization, which decides what counts as one word. The full chain is the NLP pipeline.
The one idea you need first
A vocabulary is the set of known words your system will accept. A word outside it is out of vocabulary, often written OOV. Correction only proposes words inside the vocabulary, so a name or a new term that was never added can never win. That is why the vocabulary you ship matters as much as the scoring rule. Token forms and OOV handling are in tokenization.
Think of It Like This
A librarian who knows common slips
You ask a librarian for a book by a name you half remember. You say the title wrong by one sound. The librarian does not send you away. She thinks of every known title one slip away from what you said, then picks the one borrowers ask for most often, adjusted for the kind of slip you likely made.
The shelf of known titles is the vocabulary. The one slip away list is the candidate set. Borrow frequency is the word prior, and the slip type is the error model. The pick is the product of the two.
Where it stops: a librarian hears your whole request and knows you. This page starts with one word alone, no sentence around it. When two candidates sound equally close, the lone word rule must guess from frequency, and only context can break the tie.
How It Actually Works
Fix a typed token such as aple. Fix a vocabulary of known words. In plain words: is what you typed, ranges over candidate words in , is how common is, is how likely someone meaning types , and edit distance counts insertions, deletions, and substitutions to turn one into the other.
Propose nearby words
The system lists every in within distance 1 or 2 of . Distance 1 keeps the list small: a handful of candidates instead of thousands. For equal to aple, three distance-1 neighbors are apple by inserting one p, maple by inserting m at the front, and ape by deleting l. All three cost 1, so distance alone cannot choose.
Score with the noisy channel
The noisy channel rule picks the that maximizes the product of likelihood and prior:
In plain words: is the winning word, favors the typo pattern you likely made, and favors the common word. A frequent word with a plausible slip beats a rare word with the same distance.
Worked example
Give the three candidates priors from counts: , , . Give them typo likelihoods for producing aple: for apple by dropping a doubled letter, for maple, for ape. Multiply:
- apple:
- maple:
- ape:
Apple wins by a factor of 20 over the runner up, even though all three sit at distance 1. That gap is the product doing its job: frequency breaks the distance tie, and the plausible drop pattern widens it.
Let context break real word ties
One word alone cannot fix a valid word used wrongly. In I need too tickets, too is in the vocabulary, so an isolated corrector leaves it. A contextual model reads the neighbors and prefers two before a count noun. Use the lone word rule to clean clear typos, then let a sequence model rescore when every candidate is a real word.
Code
This snippet replays the worked example: it builds distance-1 candidates by one edit, scores each by prior times likelihood, and picks the winner. Outputs match the hand products above.
from typing import Dict, List
def score(candidates: Dict[str, float], likelihood: Dict[str, float]) -> Dict[str, float]: total: Dict[str, float] = {} for w, prior in candidates.items(): # prior P(w) from counts total[w] = prior * likelihood[w] # noisy channel product return total
priors = {"apple": 0.05, "maple": 0.01, "ape": 0.008}likes = {"apple": 0.08, "maple": 0.02, "ape": 0.01}ranked = score(priors, likes)
for w in sorted(ranked, key=lambda k: ranked[k], reverse=True): print(f"{w:<6} {ranked[w]:.5f}")# -> apple 0.00400# -> maple 0.00020# -> ape 0.00008
best = max(ranked, key=lambda k: ranked[k])print(f"correct aple to {best}")# -> correct aple to appleSwap in your own counts for and your own confusion counts for and the same argmax gives you the new winner. When priors are flat, the likelihood decides. When likelihoods tie, frequency decides.
Watch Out For
Fixing names and terms you never listed
A general vocabulary does not know your names, codes, or product terms. The corrector sees an unfamiliar token, finds a close common word at distance 1, and confidently rewrites it. The symptom is clean text with the wrong nouns: a surname becomes a fruit, an abbreviation becomes a word.
Skip tokens tagged as names before you correct. Keep a whitelist from named entity recognition, add domain terms to the vocabulary with real counts, and never correct an OOV token that matches your allow list.
Trusting a lone word when any candidate is valid
Distance plus frequency cannot see grammar. In I need too tickets, too is distance 1 from two but it is also a real word, so the isolated rule has no reason to change it. The symptom is a sentence with zero typos that still reads wrong.
Treat real word errors as a separate pass. Run the lone word fix first, then rescore with a window or a transformer that reads neighbors. If your misses are all valid words, the context model is missing, not the distance.
The Quick Version
- Spell correction maps a typed token to the intended vocabulary word by edit distance plus frequency.
- Distance 1 or 2 proposes a short list: aple gives apple, maple, and ape, all at distance 1.
- The noisy channel picks , so a common word with a likely slip wins.
- Here apple scores 0.0040 against 0.0002 and 0.00008, winning by a factor of 20.
- Names and domain terms need a whitelist, and valid word slips like too for two need context, not distance.