Last active
August 29, 2015 14:06
-
-
Save claudijd/9597c2163e79ed277e29 to your computer and use it in GitHub Desktop.
Test Stamples
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
require 'levenshtein-ffi' | |
class String | |
def size_percent(other) | |
if self.size > other.size | |
diff = self.size - other.size | |
return diff.to_f / self.size.to_f | |
elsif self.size < other.size | |
diff = other.size - self.size | |
return diff.to_f / other.size.to_f | |
else | |
return 0.0 | |
end | |
end | |
def close_enough?(other) | |
return size_percent(other) < 0.1 ? true : false | |
end | |
def normalized_edit_distance(other) | |
diff = Levenshtein.distance(self, other) | |
if self.size > other.size | |
return diff.to_f / self.size.to_f | |
elsif self.size < other.size | |
return diff.to_f / other.size.to_f | |
else | |
return diff.to_f / self.size.to_f | |
end | |
end | |
end | |
baseline_samples = { | |
"pony" => File.read('templates/pony1'), | |
"zeus" => File.read('templates/zeus1'), | |
"citadel" => File.read('templates/citadel1') | |
} | |
Dir.glob("samples/*").each do |file| | |
next unless File.file?(file) | |
unknown = File.read(file) | |
baseline_samples.each do |baseline_name, baseline_data| | |
next unless unknown.close_enough?(baseline_data) | |
if (percent_diff = unknown.normalized_edit_distance(baseline_data)) < 0.1 | |
puts "[+] #{file} is #{baseline_name} (#{(100 - (percent_diff * 100)).round(2).to_s}% match)" | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment