class RuboCop::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, RuboCop::StringUtil::Jaro.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 RuboCop::StringUtil::Jaro.new
# File lib/rubocop/string_util.rb, line 116
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/rubocop/string_util.rb, line 146
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/rubocop/string_util.rb, line 124
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/rubocop/string_util.rb, line 136
def limited_common_prefix_length
  length = common_prefix_length

  if length > MAX_COMMON_PREFIX_LENGTH
    MAX_COMMON_PREFIX_LENGTH
  else
    length
  end
end