Sha256: c68b024568bae829f001e2706b414c94ab4580f398330f6db2e4f5e666570ab1

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module KeywordMatcher
  class Process
    attr_reader :group, :words

    def initialize(group, words)
      @group = group
      @words = words
    end

    def found?
      in_any?(group.or) && negation_found?.blank?
    end

    private

    def negation_found?
      return if group.not.blank?

      in_any?(group.not)
    end

    def in_any?(groups)
      groups.map do |values|
        values.map do |terms|
          match = false
          terms.each do |term|
            words.each do |word|
              if condition(term, word)
                match = true
                break
              end
            end
          end
          match
        end.include?(false).blank?
      end.include?(true)
    end

    def matched?(term, word)
      word == term
    end

    def condition(term, word)
      synonym = find_synonym(term)
      synonym.present? ? (matched?(term, word) || matched?(synonym, word)) : matched?(term, word)
    end

    def find_synonym(term)
      synonyms_h.map { |k, v| term.gsub(k, v) if term.match?(k) }.reject(&:blank?).try(:first)
    end

    def synonyms_h
      {
        %r{([0-9]+)гр} => '\1г',
        %r{([0-9]+)г(?!р)} => '\1гр',
        %r{([0-9])([,|.])(.*)} => '\1-\3'
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keyword_matcher-0.7.0 lib/keyword_matcher/process.rb