Sha256: 767c2b76cd927ef2b5be827c0314c018fb4caa9d72a4d487807bf71caebf1f9f

Contents?: true

Size: 1.47 KB

Versions: 130

Compression:

Stored size: 1.47 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, 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

      end
    end
  end
end

Version data entries

130 entries across 130 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.20 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.19 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.18 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.17 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.16 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.15 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.14 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.13 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.12 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.11 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.10 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.9 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.8 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.7 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.6 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.5 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.4 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.3 lib/eco/data/fuzzy_match/chars_position_score.rb
eco-helpers-3.0.2 lib/eco/data/fuzzy_match/chars_position_score.rb