Module: DidYouMean::JaroWinkler

Defined in:
lib/did_you_mean/jaro_winkler.rb

Constant Summary collapse

WEIGHT =
0.1
THRESHOLD =
0.7

Class Method Summary collapse

Class Method Details

.distance(str1, str2) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/did_you_mean/jaro_winkler.rb', line 67

def distance(str1, str2)
  jaro_distance = Jaro.distance(str1, str2)

  if jaro_distance > THRESHOLD
    codepoints2  = str2.codepoints
    prefix_bonus = 0

    str1.each_codepoint do |char1|
      char1 == codepoints2[prefix_bonus] && prefix_bonus < 4 ? prefix_bonus += 1 : break
    end

    jaro_distance + (prefix_bonus * WEIGHT * (1 - jaro_distance))
  else
    jaro_distance
  end
end