Module: Eco::Data::FuzzyMatch::CharsPositionScore
- Included in:
- ClassMethods
- Defined in:
- lib/eco/data/fuzzy_match/chars_position_score.rb
Instance Method Summary collapse
-
#chars_position_score(str1, str2, max_distance: 3, normalized: false) ⇒ Score
For each character in
str1
, a search is performed onstr2
.
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.
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 |