Sha256: 377fd6c6bd15c229e08828269a6ae8e94185be09551e6b10a3ffcb4735ee5248

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module KeywordMatcher
  class Prophet
    attr_reader :phrase

    PRECISION = 0.5
    SPLIT = 0.2
    SEPARATOR = %r{[\s\(\)\/*:"'\\\/\$\.,=]+}
    MEASURES = 'кг|г|л|мл|уп|ед|шт|мг|пак'.freeze

    def initialize(phrase)
      @phrase = phrase
    end

    def explode
      prepare
        .split(SEPARATOR)
        .map(&:strip)
        .map(&:downcase)
        .reject { |w| w =~ /\d{5,}/ }
        .reject(&:blank?)
    end

    def prepare
      phrase.gsub(/(\p{Ll}{2,})(\d+\S)/, '\1 \2') # split text contains > 1 character from digits
            .gsub(/%([\p{L}\d])/, '% \1') # add space after percents
            .gsub(/(\d)[\.,](\d)/, '\1-\2') # replace separator between digits from , or . to -
            .gsub(/(\d)[\.,\s]+(#{MEASURES})\.?/i, '\1\2') # replace gaps between numbers and measures
            .gsub(/(\p{Ll})(\p{Lu})/, '\1 \2') # split camelcase string
            .gsub(/(\d)-0+(#{MEASURES})/i, '\1\2') # remove trailing zeroes after measures
            .gsub(/([а-яa-z])(\d+)(#{MEASURES})/i, '\1 \2\3')
            .downcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keyword_matcher-0.5.0 lib/keyword_matcher/prophet.rb