module Eco::Data::FuzzyMatch::CharsPositionScore

Public Instance Methods

chars_position_score(str1, str2, max_distance: 3, normalized: false) click to toggle source

For each character in `str1`, a search is performed on `str2`. The search is deemed successful if a character is found in `str2` within `max_distance` characters of the current position. A score is kept of matching characters. @note This algorithm is best suited for matching mis-spellings. @max_distance [Integer] maximum char position distance to score. @normalized [Boolean] to avoid double ups in normalizing. @return [Score] the score object with the result.

# File lib/eco/data/fuzzy_match/chars_position_score.rb, line 12
def chars_position_score(str1, str2, max_distance: 3, normalized: false)
  str1, str2 = normalize_string([str1, str2]) unless normalized
  len1 = str1 && str1.length; len2 = str2 && str2.length
  Score.new(0, 0).tap do |score|
    next if !str2 || !str1 || str2.empty? || str1.empty?
    score.total = len1
    next score.increase(score.total) if str1 == str2
    next if len1 < 2
    pos = 0
    len1.times do |i|
      start = pos + 1
      found = false
      if pos = str2.index(str1[i])
        if pos < (start + max_distance)
          found = true
          score.increase
        end
      end
      pos = start unless found
    end
  end
end