Skip to content

Instantly share code, notes, and snippets.

@andrewmatte
Created December 17, 2021 04:50
Show Gist options
  • Save andrewmatte/a119757157f4c89e81774eef0b0a312c to your computer and use it in GitHub Desktop.
Save andrewmatte/a119757157f4c89e81774eef0b0a312c to your computer and use it in GitHub Desktop.
def one_way_distance(string_a, string_b)
score = 0
against = 0
(0..string_a.size-1).each do |idx|
(idx..string_a.size-1).each do |len|
if !string_b.index(string_a[idx, len-idx+1], 0).nil?
score += (len-idx+1)*2
else
against += 1
end
end
end
return {score: score, against: against}
end
def jaccard_string_similarity(string_a, string_b)
score_1 = one_way_distance(string_a, string_b)
score_2 = one_way_distance(string_b, string_a)
if score_1[:score] == score_2[:score] & score_2[:score] == 0
return 0
else
return (score_1[:score] + score_2[:score])/(score_1[:score] + score_2[:score] + score_1[:against] + score_2[:against])
end
end
puts jaccard_string_similarity("batman", "banana") # 0.5970149253731343
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment