Sha256: 2174ed0acfa00275b0321aded42a40bf8d6926400f2403bf28e43786fd9b5868

Contents?: true

Size: 859 Bytes

Versions: 1

Compression:

Stored size: 859 Bytes

Contents

#encoding: utf-8
require 'json'

def parse(fpath)
  reasons = []
  rules_hash = {}
  File.open(fpath).each_with_index do |line, i|
    next  if i == 0 # Skip header
    parts = line.strip.split(/\t/)
    # Reasons are listed at the top of the file and are not tab-separated
    if parts.size == 1
      reasons << parts[0]
    # Rules are tab-separated in the following format:
    # <from>\t<to>\t<type>\t<reason_index>
    else
      from = parts.first
      rules_hash[from.size] ||= []
      rules_hash[from.size] << {
        :from => from,
        :to => parts[1],
        :reason_id => parts[3].to_i
      }
    end
  end

  {:reasons => reasons, :rules => rules_hash}
end

root = File.expand_path(File.dirname(__FILE__))
File.open(File.join(root, 'data/deinflect.json'), 'w') do |f|
  f.write(parse(File.join(root, 'data/deinflect.dat')).to_json)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
japanese_deinflector-0.0.1 lib/deinflect_to_json.rb