Sha256: f4ea1cedb47ee0fd51307de868402c39911b0c90556faf8db2c50a061b87488f
Contents?: true
Size: 1.28 KB
Versions: 1
Compression:
Stored size: 1.28 KB
Contents
#encoding: utf-8 require 'json' require "japanese_deinflector/version" class JapaneseDeinflector def initialize File.open(File.join(File.expand_path(File.dirname(__FILE__)), 'data/deinflect.json')) do |f| rules_and_reasons = JSON.parse(f.read, :symbolize_names => true) @reasons = rules_and_reasons[:reasons] # Convert hash keys to integers @rules = {} # Convert hash keys from something like :"9" -> 9 rules_and_reasons[:rules].each do |size, rules| @rules[size.to_s.to_i] = rules end end end def deinflect(word) possibilities = [] rules_less_than_size(word.size).each do |size, rules| ending = word[-size..-1] rules.each do |rule| next unless ending == rule[:from] deinflected_word = "#{word[0..-size-1]}#{rule[:to]}" next if possibilities.include?(deinflected_word) # Weight is between 0 and 1, 1 being a higher chance of actual deinflection weight = (Float(size) / word.size).round(3) reason = @reasons[rule[:reason_id]] possibilities << {:weight => weight, :word => deinflected_word, :reason => reason} end end possibilities end private def rules_less_than_size(max_size) @rules.clone.keep_if{|size, rules| size < max_size} end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
japanese_deinflector-0.0.1 | lib/japanese_deinflector.rb |