Scanning the whole dictionary for close words is slow. The faster idea is to work from the typo instead - generate the strings a few edits away and see which are real. Today you build the first edit family, every string one deletion away.
Generate all strings formed by deleting exactly one character from a word.
The brute-force Nearby scan measures a typo against every word in the dictionary,
which does not scale. Peter Norvig’s insight flips it around: instead of asking the
dictionary “who is near me?”, generate the strings that are near the typo and
ask “which of you are real?” There are only a few of those per word, and the number
does not grow with the dictionary. This chapter builds that generator, one edit
operation at a time, starting with the simplest.
A deletion removes one character. For an n-letter word there are exactly n
of them - drop position 0, then 1, and so on - so cat yields at, ct, ca.
Most of these are gibberish and will be thrown away; that is fine. The point is
that the real corrections for a single-deletion typo (someone typed ct for cat)
are guaranteed to be somewhere in this small list, found without touching the
dictionary at all.
// for each position i, drop the character at i:// word[:i] + word[i+1:]func deletes(word string) []string {// one result per position, left to right}