Module: RussianWordForms

Defined in:
lib/russian_word_forms.rb,
lib/russian_word_forms/rules.rb,
lib/russian_word_forms/version.rb,
lib/russian_word_forms/dictionary.rb

Defined Under Namespace

Classes: Dictionary, Rules

Constant Summary collapse

VERSION =
"0.0.5"
@@dictionary =
Dictionary.new
@@rules =
Rules.new

Class Method Summary collapse

Class Method Details

.dictionaryObject



68
69
70
# File 'lib/russian_word_forms.rb', line 68

def self.dictionary
  @@dictionary
end

.get_base_form(word) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/russian_word_forms.rb', line 33

def self.get_base_form(word)
  word = Unicode::upcase(word).to_s
  flags = @@dictionary.dictionary[word]
  variants = []
  variants << word if flags
  @@rules.rules.each do |flag,rules_keys|
    rules_keys.each do |key,rules|
      rules.each do |rule|
        if rule.suffix && !rule.suffix.empty?
          # puts "#{word} #{rule.suffix}"

          if word.end_with? rule.suffix
            tmp = word.gsub(rule.suffix,rule.normal_suffix)
            # puts tmp
            variants << tmp if tmp != word && tmp.match(/(#{rule.rule})$/i) && @@dictionary.get_flags(tmp)
          end

        else
          if word.end_with?(rule.normal_suffix)
            tmp = word.gsub(rule.normal_suffix,"")
            variants << tmp if tmp != word && tmp.match(/(#{rule.rule})$/i) && @@dictionary.dictionary[tmp]
          end
        end
      end
    end
  end


  return variants.uniq
end

.inflect(word) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/russian_word_forms.rb', line 12

def self.inflect(word)
  word = Unicode::upcase(word)
  flags = @@dictionary.get_flags word
  output = []
  if flags
    flags.each_char do |flag|
      rules_keys = @@rules.rules[flag]
      rules_keys.each do |key,rules|
        rules.each do |rule|
          if rule.suffix
            output << word.gsub(/(#{rule.normal_suffix})$/i,rule.suffix) if word.match(/(#{rule.rule})$/i)
          else
            output << word+rule.normal_suffix if word.match(/(#{rule.rule})$/i)
          end

        end
      end
    end
  end
  output.uniq
end

.rulesObject



64
65
66
# File 'lib/russian_word_forms.rb', line 64

def self.rules
  @@rules
end