Build a Simple Python Spell Checker Using Bayesian Methods in 21 Lines

Learn how to create a fully functional spell checker in just 21 lines of Python by training a Bayesian model on a large text corpus, generating candidate corrections via edit distance, and selecting the most probable word using probability calculations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Simple Python Spell Checker Using Bayesian Methods in 21 Lines

Introduction

When you type a misspelled word into Google or Baidu, they instantly suggest the correct spelling. The following 21‑line Python program implements a simple yet complete spell checker that works on the same principle.

Code

import re, collections

def words(text): return re.findall('[a-z]+', text.lower())

def train(features):
    model = collections.defaultdict(lambda: 1)
    for f in features:
        model[f] += 1
    return model

NWORDS = train(words(open('big.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):
    splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
    deletes = [a + b[1:] for a, b in splits if b]
    transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
    replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
    inserts = [a + c + b for a, b in splits for c in alphabet]
    return set(deletes + transposes + replaces + inserts)

def known_edits2(word):
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correct(word):
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
    return max(candidates, key=NWORDS.get)

# examples
# >>> correct("cpoy")
# 'copy'
# >>> correct("engilsh")
# 'english'
# >>> correct("sruprise")
# 'surprise'

Underlying Theory

The algorithm is based on Bayes' theorem. For a misspelled word w, we choose the candidate correction c that maximizes P(c|w), which by Bayes becomes P(w|c)·P(c) (the denominator P(w) is constant for all candidates).

P(c|w) : probability that the intended word is c given the observed misspelling w.

P(w|c) : probability of typing w when the intended word is c, estimated by edit distance.

P(c) : prior probability of c in the training corpus.

Since P(w) is the same for all candidates, the decision reduces to maximizing P(w|c)·P(c). The code implements this by:

Counting word frequencies in a large corpus ( big.txt) to obtain P(c).

Generating all words one edit away from w ( edits1) and two edits away ( known_edits2) to approximate P(w|c).

Selecting the candidate with the highest frequency in the corpus.

Code Walk‑through

words() extracts lowercase alphabetic tokens from the corpus using a regular expression.

train() builds a frequency dictionary; unseen words default to a count of 1 using collections.defaultdict(lambda: 1).

edits1() produces all strings that are one edit operation (deletion, transposition, replacement, insertion) away from the input word.

Empirical studies show that 80‑95 % of spelling errors are within one edit distance, so the algorithm also considers two‑edit candidates via known_edits2().

known() filters a set of words to those present in the frequency dictionary.

correct() assembles the candidate set (exact match, one‑edit, two‑edit) and returns the word with the highest corpus frequency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonNLPBayesianedit distancespell checking
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.