Skip to content

Instantly share code, notes, and snippets.

@shunsugai
Created September 25, 2011 15:58
Show Gist options
  • Save shunsugai/1240754 to your computer and use it in GitHub Desktop.
Save shunsugai/1240754 to your computer and use it in GitHub Desktop.
Test.
#encoding: utf-8
class String
def make_ngram(n)
ary = self.split(//)
ngram_ary = []
p = ary.length
for i in 0..(p - n) do
ngram_ary << ary[i...(i + n)]
end
ngram_ary
end
end
def compare(str_a, str_b)
a = str_a.make_ngram(3)
b = str_b.make_ngram(3)
count = 0
a.each do |elem|
for i in 0..(b.length) do
if elem == b[i]
count += 1
b.delete_at i
break
end
end
end
num = count.to_f
den = a.length > b.length ? a.length : b.length
puts num / den
end
compare(ARGV[0], ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment