Last active
January 2, 2016 03:39
-
-
Save DiegoSalazar/8245411 to your computer and use it in GitHub Desktop.
string likeness test using Levenshtein distance
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
# install the levenshtein gem first with: | |
# sudo gem install levenshtein-ffi | |
require 'levenshtein' | |
def names_are_close?(s1, s2, tolerance = 1) | |
Levenshtein.distance(s1, s2) == tolerance | |
end | |
name_pairs = [ | |
['james', 'jimes'], | |
['Worthington', 'Worthintgon'], | |
['Diego', 'Deigo'], | |
['Tobias', 'Tiboas'], | |
['Eduardo', 'Edward'], | |
['Brian', 'Burton'] | |
] | |
name_pairs.each do |names| | |
print "Testing #{names.inspect}: " | |
passed = names_are_close? *names | |
puts "They are #{passed ? 'close' : 'not close'}\n" | |
end | |
puts "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment