Sha256: 51361557070a8121a6f6416865e91aff1e60da609f486d2288c86001c70f1992

Contents?: true

Size: 905 Bytes

Versions: 5

Compression:

Stored size: 905 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(/\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

5 entries across 5 versions & 1 rubygems

Version Path
keyword_matcher-0.5.0 lib/keyword_matcher/group.rb
keyword_matcher-0.4.0 lib/keyword_matcher/group.rb
keyword_matcher-0.3.5 lib/keyword_matcher/group.rb
keyword_matcher-0.3.4 lib/keyword_matcher/group.rb
keyword_matcher-0.3.3 lib/keyword_matcher/group.rb