module MorpherInflecter

Constants

CASES

Падежи

ERROR_CODES
URL
VERSION

Public Class Methods

clear_cache() click to toggle source

Очистить кеш

# File lib/morpher_inflecter.rb, line 85
def self.clear_cache
  @@cache.clear
end
inflections(word, options = {}) click to toggle source

Возвращает хэш склонений в следуюшем формате {:singular => [], :plural => []} Если слово не найдено в словаре или произошла ошибка, будет возвращен код ошибки { error: “XXX”, code: “Y”, message: 'error message' }

# File lib/morpher_inflecter.rb, line 52
def self.inflections(word, options = {})

  lookup = cache_lookup(word)
  return lookup if lookup
  resp = Inflection.new.get(word, options[:token])
  inflections = {}

  if resp[:error]
    inflections = resp
  else
    plural_only = resp['множественное'].nil?

    inflections[:singular] = [] unless plural_only
    inflections[:plural] = []

    CASES.each do |_case|
      i = _case == 'И' ? word : resp[_case]

      if plural_only
        inflections[:plural] << i
      else
        inflections[:singular] << i
        inflections[:plural] << resp['множественное'][_case]
      end
    end

    cache_store(word, inflections)
  end

  inflections
end

Private Class Methods

cache_lookup(word) click to toggle source
# File lib/morpher_inflecter.rb, line 90
def self.cache_lookup(word)
  @@cache[word.to_s]
end
cache_store(word, value) click to toggle source
# File lib/morpher_inflecter.rb, line 94
def self.cache_store(word, value)
  @@cache[word.to_s] = value
end