« PKD: The Early Novels | Main | "It was probably surreal" »

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} "; exit
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

TrackBack

TrackBack URL for this entry:
http://prospero.bluescarf.net/cgi-bin/mt/mt-tb.cgi/41

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on December 11, 2007 10:51 PM.

The previous post in this blog was PKD: The Early Novels.

The next post in this blog is "It was probably surreal".

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.35