Module: Eco::Data::FuzzyMatch::CharsPositionScore

Included in:
ClassMethods
Defined in:
lib/eco/data/fuzzy_match/chars_position_score.rb

Instance Method Summary collapse

Instance Method Details

#chars_position_score(str1, str2, max_distance: 3, normalized: false) ⇒ Score

Note:

This algorithm is best suited for matching mis-spellings.

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.

Returns:

  • (Score)

    the score object with the result.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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