Quick and Dirty Spell Correction
I was recently reading about Levenshtein string edit distance (edit distance, for short) and came across a nice little article by Peter Norvig about spelling correction. (Why doesn't he have a proper blog?)
The article discusses spelling correction and has some sample code written in Python. Python is a nice language, but these days I mainly use Ruby since it's the programming language of choice at Powerset, where I work. I found a Ruby implementation of Norvig's code, but I felt like it wasn't OO enough for my tastes, so I decided to reimplement it. All of the code is downlodable as a gzipped tarball here. Here's what one of the scripts looks like:
#!/usr/local/bin/ruby
require 'spell_checker'
def usage
STDERR.puts "#{$0}
end
def main
word = ARGV[0] || usage
spell = SpellChecker.new("holmes.txt")
candidates = spell.correct(word)
puts "Word: '#{word}'"
puts "Suggestions: #{candidates.map { |w| "'#{w}'"}.join(', ')}"
end
main