class Storage::Da::Es::Storage
Public Class Methods
new()
click to toggle source
# File lib/storage/da/es.rb, line 9 def initialize @root_node = assign_node('', [], false) end
Public Instance Methods
add(string)
click to toggle source
# File lib/storage/da/es.rb, line 13 def add(string) words = string.delete("' '||'\n'").split(',') words.each do |word| add_word(word) end self end
add_word(word)
click to toggle source
# File lib/storage/da/es.rb, line 68 def add_word(word) current_node = @root_node word.each_char.with_index do |c, i| new_node = find_node_in_array(current_node[:childList], c) if new_node current_node = new_node if i == word.length-1 current_node[:isEnd] = true end else new_node = assign_node(c, [], i == word.length-1) current_node[:childList].push(new_node) current_node = new_node end end end
contains?(key)
click to toggle source
# File lib/storage/da/es.rb, line 21 def contains?(key) current_node = @root_node key.each_char.with_index do |c, i| new_node = find_node_in_array(current_node[:childList], c) if new_node current_node = new_node return true if current_node[:isEnd] && i == key.length-1 else return false end end end
find(prefix)
click to toggle source
# File lib/storage/da/es.rb, line 34 def find(prefix) raise ArgumentError, 'Length of prefix should be 3 or great' if prefix.length < 3 current_node = @root_node prefix.each_char.with_index do |c, i| new_node = find_node_in_array(current_node[:childList], c) if new_node current_node = new_node return get_all_tail_by_node('', current_node, []).map{|tail| prefix.chop + tail} if i == prefix.length-1 else return [] end end end
load_from_file(filename)
click to toggle source
# File lib/storage/da/es.rb, line 104 def load_from_file(filename) f = File.open(filename, 'r') f.each_line do |line| add(line) end f.close self end
load_from_zip(zipfile)
click to toggle source
# File lib/storage/da/es.rb, line 113 def load_from_zip(zipfile) Zip::ZipFile.open(zipfile) do |file| file.each do |content| text = file.read(content) text.each_line do |line| add(line) end end end end
save_to_file(filename)
click to toggle source
# File lib/storage/da/es.rb, line 124 def save_to_file(filename) all_keys = get_all_tail_by_node('', @root_node, []).join(',') file = File.open(filename, 'w') file.write(all_keys) file.close end
save_to_zip(zipfile_name)
click to toggle source
# File lib/storage/da/es.rb, line 131 def save_to_zip(zipfile_name) filename = 'core/files/tmp/output.txt' save_to_file(filename) Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile| zipfile.add(filename,filename) end end
to_s()
click to toggle source
# File lib/storage/da/es.rb, line 100 def to_s @root_node end
Private Instance Methods
assign_node(label, child_list, is_end)
click to toggle source
# File lib/storage/da/es.rb, line 92 def assign_node(label, child_list, is_end) node = Hash.new node[:label] = label node[:childList] = child_list node[:isEnd] = is_end return node end
find_node_in_array(list, label)
click to toggle source
# File lib/storage/da/es.rb, line 85 def find_node_in_array(list, label) list.each do |n| return n if (n[:label] == label) end nil end
get_all_tail_by_node(tail, node, acc)
click to toggle source
# File lib/storage/da/es.rb, line 48 def get_all_tail_by_node(tail, node, acc) if node[:isEnd] acc << tail + node[:label] if node[:childList].any? tail += node[:label] node[:childList].each do |n| get_all_tail_by_node(tail, n, acc) end else return acc end else tail += node[:label] node[:childList].each do |n| get_all_tail_by_node(tail, n, acc) end end acc end