Class: JapaneseDeinflector

Inherits:
Object
  • Object
show all
Defined in:
lib/japanese_deinflector.rb,
lib/japanese_deinflector/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initializeJapaneseDeinflector

Returns a new instance of JapaneseDeinflector.



7
8
9
10
11
12
13
14
15
16
# File 'lib/japanese_deinflector.rb', line 7

def initialize
  File.open(File.join(File.expand_path(File.dirname(__FILE__)), 'data/deinflect.json')) do |f|
    rule_groups = JSON.parse(f.read, :symbolize_names => true)
    # Convert hash keys to integers (from something like :"9" -> 9)
    @rule_groups = {}
    rule_groups.each do |suffix_size, rules|
      @rule_groups[suffix_size.to_s.to_i] = rules
    end
  end
end

Instance Method Details

#deinflect(word, iteration = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/japanese_deinflector.rb', line 18

def deinflect(word, iteration = 0)
  results = Set.new

  filter_rule_groups(@rule_groups, :max_suffix_size => word.size).each do |suffix_size, rules|
    from_suffix = word[-suffix_size..-1]
    filter_rules(rules, :from_suffix => from_suffix).each do |rule|
      results << result_hash(word, from_suffix, rule[:to_suffix], rule[:reason])
    end
  end

  if iteration < 2
    additional_results = Set.new
    results.each do |result|
      additional_results.merge(deinflect(result[:word], iteration + 1))
    end
    results.merge(additional_results)
  end

  # Sort results in descending order by weight
  results.to_a.sort{|x, y| y[:weight] <=> x[:weight]}
end