Created
July 20, 2012 18:46
-
-
Save mattgaidica/3152509 to your computer and use it in GitHub Desktop.
Lazy Levenshtein
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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