Top Level Namespace

Defined Under Namespace

Modules: RsegEngine, RsegFilter Classes: App, Rseg

Instance Method Summary collapse

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/builder/dict.rb', line 22

def build
  tree = {}
  dictionaries = ['cedict.zh_CN.utf8', 'wikipedia.zh.utf8']
  
  dictionaries.each do |dictionary|
    puts "Processing #{dictionary}..."
    path = File.join(File.dirname(__FILE__), '../../dict', dictionary)
    process(path, tree)
  end
  
  File.open(hash_path, "wb") {|io| Marshal.dump(tree, io)}  
  puts 'Done'
end

#hash_pathObject



36
37
38
# File 'lib/builder/dict.rb', line 36

def hash_path
  File.join(File.dirname(__FILE__), '../../dict/dict.hash')
end

#process(path, tree) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/builder/dict.rb', line 3

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