Sha256: ead32b9ae4d4793e133c4c6970ea4982126c28ccce3c96d3b069b5d21666373a

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module KeywordMatcher
  class Group
    attr_reader :all, :or, :not, :title

    OPERATOR_OR = 'или'.freeze
    OPERATOR_NOT = 'не'.freeze

    def initialize(title)
      @title = title.gsub(/(["'])/, '') # remove quotes
                    .gsub(/(\d)[\.,](\d)/, '\1-\2') # replace separator between digits from , or . to -
      @all = values
      @or = or_groups
      @not = not_groups
    end

    def values
      title.downcase.split("\n").map do |line|
        line.split(' ').map(&:strip)
      end.reject(&:empty?)
    end

    def or_groups
      title.downcase.gsub(/\r?\n#{OPERATOR_NOT}.*/m, '').split(/\r?\n#{OPERATOR_OR}\r?\n/).map do |v|
        v.split("\n").reject(&:blank?).map(&:split)
      end.reject(&:blank?)
    end

    def not_groups
      return [] unless title.downcase.match?(/\r?\n#{OPERATOR_NOT}\r?\n/)

      title.downcase.match(/\r?\n#{OPERATOR_NOT}.*/m).to_s.split(/\r?\n#{OPERATOR_NOT}\r?\n/).map do |v|
        v.split("\n").reject(&:blank?).map(&:split)
      end.reject(&:blank?)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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