From 44088022899fd81fe47d851c24a68d73f247639c Mon Sep 17 00:00:00 2001 From: Mau Magnaguagno Date: Wed, 25 Jan 2023 19:51:01 -0300 Subject: [PATCH 1/2] Remove Integer floor and prefix index from Jaro distance The variable ``length2`` is an Integer, call to floor returns self. The variables ``i`` and ``prefix_bonus`` are equally incremented, no need to keep the ``i`` variable. --- lib/did_you_mean/jaro_winkler.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/did_you_mean/jaro_winkler.rb b/lib/did_you_mean/jaro_winkler.rb index 56db130a..edc456f1 100644 --- a/lib/did_you_mean/jaro_winkler.rb +++ b/lib/did_you_mean/jaro_winkler.rb @@ -8,7 +8,7 @@ def distance(str1, str2) m = 0.0 t = 0.0 - range = (length2 / 2).floor - 1 + range = length2 / 2 - 1 range = 0 if range < 0 flags1 = 0 flags2 = 0 @@ -72,10 +72,8 @@ def distance(str1, str2) codepoints2 = str2.codepoints prefix_bonus = 0 - i = 0 str1.each_codepoint do |char1| - char1 == codepoints2[i] && i < 4 ? prefix_bonus += 1 : break - i += 1 + char1 == codepoints2[prefix_bonus] && prefix_bonus < 4 ? prefix_bonus += 1 : break end jaro_distance + (prefix_bonus * WEIGHT * (1 - jaro_distance)) From cb518e251aef5f163ffb2fc69cd515747b21de4a Mon Sep 17 00:00:00 2001 From: Mau Magnaguagno Date: Sun, 10 Mar 2024 14:48:59 -0300 Subject: [PATCH 2/2] Avoid division for small length2 --- lib/did_you_mean/jaro_winkler.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/did_you_mean/jaro_winkler.rb b/lib/did_you_mean/jaro_winkler.rb index edc456f1..9a3e57f6 100644 --- a/lib/did_you_mean/jaro_winkler.rb +++ b/lib/did_you_mean/jaro_winkler.rb @@ -8,8 +8,7 @@ def distance(str1, str2) m = 0.0 t = 0.0 - range = length2 / 2 - 1 - range = 0 if range < 0 + range = length2 > 3 ? length2 / 2 - 1 : 0 flags1 = 0 flags2 = 0