Sha256: 8f82ade1d8ec1c82e462c44565751834d8c0897614ea02ca78ec843a07062607
Contents?: true
Size: 1.73 KB
Versions: 1
Compression:
Stored size: 1.73 KB
Contents
require 'yaml' namespace :harmonious_dictionary do desc "generate harmonious dictionary for use" task :generate => :environment do chinese_dictionary_path = File.join(Rails.root, 'config','harmonious_dictionary','chinese_dictionary.txt') english_dictionary_path = File.join(Rails.root, 'config','harmonious_dictionary','english_dictionary.txt') puts "Processing chinese words..." tree = {} model = ENV['model'] process(chinese_dictionary_path, tree) File.open(hash_path(model), "wb") {|io| Marshal.dump(tree, io)} puts 'Done' puts 'Processing english words...' english_dictionary = [] process_english_words(english_dictionary_path,english_dictionary) File.open(yaml_path, "wb") {|io| YAML::dump(english_dictionary, io)} puts 'Done' end end def process_english_words(path,list) File.open(path, 'r') do |file| file.each_line{|line| list << line.gsub!("\n",'') } end end def process(path, tree) File.open(path, 'r') do |file| file.each_line do |line| node = nil line.chars.each do |c| next if c == "\n" || c == "\r" if node node[c] ||= {} node = node[c] else tree[c] ||= Hash.new node = tree[c] end end node[:end] = true end end end def hash_path(model = nil) if model File.join(Rails.root, 'config','harmonious_dictionary',"#{model}_harmonious.hash") else File.join(Rails.root, 'config','harmonious_dictionary','harmonious.hash') end end def yaml_path(model = nil) if model File.join(Rails.root, 'config','harmonious_dictionary',"#{model}_harmonious_english.yml") else File.join(Rails.root, 'config','harmonious_dictionary','harmonious_english.yml') end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
harmonious_check-0.0.2 | lib/tasks/generate_dictionary.rake |