Skip to content

Instantly share code, notes, and snippets.

@mattgaidica
Created July 20, 2012 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattgaidica/3152509 to your computer and use it in GitHub Desktop.
Save mattgaidica/3152509 to your computer and use it in GitHub Desktop.
Lazy Levenshtein
# test by running:
# ruby lazy.rb
# use control+c to exit
require 'amatch'
include Amatch
def lazy input, matches, abbreviations=true
# setup the Levenshtein comparator
distances = {}
m = Levenshtein.new(input)
matches.reverse.each do |match|
# get the edit distance
tests = []
tests << m.match(match)
tests << m.match(match[0..3]) if abbreviations
# lowest score gets placed
distances[tests.min] = match
end
# return input, returns original if matches is empty
input = distances.empty? ? input : distances.min.last
end
environments = %w(development integration staging production)
while true
puts "\nenvironments #{environments.join(', ')}"
print "choose environment: "
input = gets.chomp
puts "match: #{lazy(input, environments)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment