Skip to content

Instantly share code, notes, and snippets.

@automatthew
Created August 14, 2009 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save automatthew/168060 to your computer and use it in GitHub Desktop.
Save automatthew/168060 to your computer and use it in GitHub Desktop.
basic edits for Strings
# useful for things like http://norvig.com/spell-correct.html
module Edits
DICT = { "cap" => 1, "carp" => 1, "clap" => 1, "cramp" => 1 }
def deletes
map_transforms { |word, i| word.delete_at(i) }
end
def transposes
map_transforms { |word, i| word[i], word[i+1] = word[i+1], word[i] }
end
def replaces
("a".."z").map do |c|
map_transforms { |word, i| word[i] = c }
end.flatten
end
def inserts
("a".."z").map do |c|
r = map_transforms { |word, i| word.insert(i, c) }
terminal = "#{self}#{c}"
r << terminal if terminal.score
r
end.flatten
end
def map_transforms
out = []
chars = self.split('')
self.size.times do |i|
yield(word = chars.dup, i)
word = word.join
out << word if word.score && word != self
end
out
end
def score
DICT[self]
end
end
String.send(:include, Edits)
describe "a String, imbued with Edits" do
it "works" do
r = "crap".deletes
r.should == %w{ cap }
r = "crap".transposes
r.should == %w{ carp }
r = "crap".replaces
r.should == %w{ clap }
r = "crap".inserts
r.should == %w{ cramp }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment