By Ankit Tripathi
We will say that we are trying to find the correction c, out of all possible
corrections, that maximizes the probability of c given the original
word w:
argmaxc P(c|w)
why take a simple expression like P(c|w) and replace it with a more complex
expression involving two models rather than one? The answer is that P(c|w)
is already conflating two factors, and it is easier to separate the two out and
deal with them explicitly. Consider the misspelled word w="thew" and the
two candidate corrections c="the" and c="thaw".
We will read a big text file, big.txt, which consists of about a million words(Project
Gutenberg).
We then extract the individual words from the file (using the function words,
which converts everything to lowercase, so that "the" and "The" will be the same
and then defines a word as a sequence of alphabetic characters, so "don't" will be
seen as the two words "don" and "t"). Next we train a probability model, which is a
fancy way of saying we count how many times each word occurs, using the
function train. It looks like this:
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(file('big.txt').read()))
At this point, NWORDS[w] holds a count of how many times the word w has
been seen. There is one complication: novel words.
smoothing
Edit Distance
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)
Thumb Rule:
I defined a trivial model that says all known words of edit distance 1 are
infinitely more probable than known words of edit distance 2, and infinitely less
probable than a known word of edit distance 0
>>> correct('speling')
'spelling'
>>> correct('korrecter')
'corrector'