Sha256: e21e9fd8d4521bd1cd717176a561088981b4d8ed877decc38b02938e24969b82

Contents?: true

Size: 872 Bytes

Versions: 2

Compression:

Stored size: 872 Bytes

Contents

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

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

    def initialize(title)
      @title = title
      @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(OPERATOR_OR).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(OPERATOR_NOT).map do |v|
        v.split("\n").reject(&:blank?).map(&:split)
      end.reject(&:blank?)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
keyword_matcher-0.2.0 lib/keyword_matcher/group.rb
keyword_matcher-0.1.0 lib/keyword_matcher/group.rb