class Analects::CedictLoader
Attributes
headers[R]
Public Class Methods
new(io, library)
click to toggle source
# File lib/analects/cedict_loader.rb, line 9 def initialize(io, library) @contents = io.read @headers = {} @contents.each_line do |line| if line =~ /^#! (.*)=(.*)/ @headers[$1.strip] = $2.strip end break unless line =~ /^#/ end end
Public Instance Methods
each(&blk)
click to toggle source
# File lib/analects/cedict_loader.rb, line 24 def each(&blk) return to_enum(__method__) unless block_given? @entries ||= @contents.each_line.map do |line| process_contents(line) if line !~ /^#/ end.compact @entries.each(&blk) end
field_names()
click to toggle source
# File lib/analects/cedict_loader.rb, line 20 def field_names [:traditional, :simplified, :pinyin, :definitions] end
find_by(qry)
click to toggle source
# File lib/analects/cedict_loader.rb, line 32 def find_by(qry) qry.map {|field, value| lookup_index(field).fetch(value, [])}.inject {|r1, r2| r1 & r2} end
lookup_index(field)
click to toggle source
# File lib/analects/cedict_loader.rb, line 36 def lookup_index(field) @indexes ||= field_names.each_with_object({}) do |field, acc| acc[field] = each_with_object({}) do |entry, acc| (acc[entry[field_names.index(field)]] ||= []) << entry end end @indexes[field] end
Private Instance Methods
process_contents(line)
click to toggle source
# File lib/analects/cedict_loader.rb, line 47 def process_contents(line) if line.strip =~ /^([^\s]*) ([^\s]*) \[([\w\d:,ยท ]+)\](.*)/ [$1,$2,$3,$4].map{|x| x.strip} else raise "Unexpected contents : #{line.inspect}" end end