Close the chapter with a corrector that reads text and fixes it. Today you run correct() over every unknown word in a passage, reporting each typo with its single best correction.
For each unknown word in a text, report it with its one best correction.
This is a real spell corrector in miniature: hand it a line, and it returns each
word it did not recognize alongside the single word it thinks you meant. It threads
together the whole project so far - the tokenizer finds words and positions,
membership decides which are suspect, and Correct picks the best fix using
distance and frequency together.
Compared with chapter two’s Suggestions, which dumped every nearby word, this
gives one confident answer per typo - the behavior a user actually wants. What it
does not yet have is speed: Correct still leans on known2, whose
tens-of-thousands of generated strings make a large document slow. Chapter five
replaces that engine with a BK-tree index that finds the same candidates while
touching only a sliver of the dictionary - same corrections, far less work.
type Correction struct {Token TokenBest string}func (d *Dictionary) Corrections(text string) []Correction {// Check(text) finds unknown tokens; for each, attach Correct(word)}