class RubbyCop::StringUtil::JaroWinkler

This class computes Jaro-Winkler distance, which adds prefix-matching bonus to Jaro distance.

Constants

DEFAULT_BOOST_THRESHOLD

Add the prefix bonus only when the Jaro distance is above this value. In other words, if the Jaro distance is less than this value, JaroWinkler.distance returns the raw Jaro distance.

DEFAULT_SCALING_FACTOR

How much the prefix bonus is weighted. This should not exceed 0.25.

MAX_COMMON_PREFIX_LENGTH

Cutoff the common prefix length to this value if it's longer than this.

Attributes

boost_threshold[R]
scaling_factor[R]

Public Class Methods

new(a, b, boost_threshold = nil, scaling_factor = nil) click to toggle source
Calls superclass method RubbyCop::StringUtil::Jaro::new
# File lib/rubbycop/string_util.rb, line 117
def initialize(a, b, boost_threshold = nil, scaling_factor = nil)
  super(a, b)
  @boost_threshold = boost_threshold || DEFAULT_BOOST_THRESHOLD
  @scaling_factor = scaling_factor || DEFAULT_SCALING_FACTOR
end

Private Instance Methods

common_prefix_length() click to toggle source
# File lib/rubbycop/string_util.rb, line 147
def common_prefix_length
  shorter.size.times do |index|
    return index unless shorter[index] == longer[index]
  end

  shorter.size
end
compute_distance() click to toggle source
# File lib/rubbycop/string_util.rb, line 125
def compute_distance
  jaro_distance = super

  if jaro_distance >= boost_threshold
    bonus = limited_common_prefix_length.to_f * scaling_factor.to_f *
            (1.0 - jaro_distance)
    jaro_distance + bonus
  else
    jaro_distance
  end
end
limited_common_prefix_length() click to toggle source
# File lib/rubbycop/string_util.rb, line 137
def limited_common_prefix_length
  length = common_prefix_length

  if length > MAX_COMMON_PREFIX_LENGTH
    MAX_COMMON_PREFIX_LENGTH
  else
    length
  end
end