Sha256: 52455c488c4b7ae8610cf8f99196356a3dba8daed11d7bf853875f71ce21dfdc

Contents?: true

Size: 906 Bytes

Versions: 3

Compression:

Stored size: 906 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

3 entries across 3 versions & 1 rubygems

Version Path
keyword_matcher-0.6.2 lib/keyword_matcher/group.rb
keyword_matcher-0.6.1 lib/keyword_matcher/group.rb
keyword_matcher-0.6.0 lib/keyword_matcher/group.rb