Sha256: 7a1a0954c786932828584c5e98e2de71d885c4445f4090a08e012f3b90e54419

Contents?: true

Size: 733 Bytes

Versions: 4

Compression:

Stored size: 733 Bytes

Contents

# encoding: utf-8
require_relative "base"

module Corrector

  # Stores words translations.
  class Prefix < Base
    validates :from, format: { without: /\s/ }, allow_nil: false

    # Separates all prefixes in a scanned string.
    #
    # @example:
    #     Prefix.scan "КИЛОМЕТР" # => ["КИЛО", "^", "МЕТР"]
    #
    # Params:
    # +value+: a word to be scanned for prefixes and splitted
    #
    # Returns an array of recognized word's parts.
    def self.scan(value)
      all.reduce(value.dup) { |str, prefix| prefix.send :scan, str }.split(" ")
    end

    private

    # Separates a prefix in a scanned value.
    def scan(value)
      value.gsub Regexp.new("^#{ from }"), "#{ to } ^ "
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
corrector-0.1.0 app/models/corrector/prefix.rb
corrector-0.0.3 app/models/corrector/prefix.rb
corrector-0.0.2 app/models/corrector/prefix.rb
corrector-0.0.1 app/models/corrector/prefix.rb