Created
December 17, 2021 04:50
-
-
Save andrewmatte/a119757157f4c89e81774eef0b0a312c to your computer and use it in GitHub Desktop.
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
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