Sha256: 1500132a1a4d81d21e7ac48692c381d22399a787669cdf961a929a23f372bac2

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Eco
  module Data
    module FuzzyMatch
      module CharsPositionScore
        # 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.
        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, len1 || 0).tap do |score|
            next if !str1 || !str2
            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

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-2.0.19 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-2.0.18 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-2.0.17 lib/eco/data/fuzzy_match/chars_position_score.rb