adieush 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/adieush.rb +124 -2
- metadata +1 -1
data/lib/adieush.rb
CHANGED
@@ -1,5 +1,127 @@
|
|
1
|
+
require 'zip/zip'
|
1
2
|
class Adieush
|
2
|
-
|
3
|
-
|
3
|
+
attr_accessor :storage_array, :search_words
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@storage_array = {}
|
7
|
+
@search_words = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_string(init_string)
|
11
|
+
input_array = init_string.split(',')
|
12
|
+
input_array.each do |store|
|
13
|
+
add(store)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_from_file(filename)
|
18
|
+
file_str = ''
|
19
|
+
File.open(filename, 'r') {|f|
|
20
|
+
file_str = f.read
|
21
|
+
}
|
22
|
+
load_string(file_str)
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_to_file(filename)
|
26
|
+
input_array = output_words(@storage_array)
|
27
|
+
input_string = ''
|
28
|
+
input_array.each do |word|
|
29
|
+
input_string += "#{word},"
|
30
|
+
end
|
31
|
+
File.open(filename, 'w') {|f|
|
32
|
+
f.print input_string
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_to_zip(zip_name)
|
37
|
+
input_array = output_words(@storage_array)
|
38
|
+
input_string = ''
|
39
|
+
input_array.each do |word|
|
40
|
+
input_string += "#{word},"
|
41
|
+
end
|
42
|
+
Zip::ZipFile.open(zip_name, Zip::ZipFile::CREATE) {
|
43
|
+
|zipfile|
|
44
|
+
zipfile.get_output_stream("storage") { |f| f.print input_string }
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def load_from_zip(zip_name)
|
49
|
+
Zip::ZipFile.open(zip_name) do |zipfile|
|
50
|
+
zipfile.each do |entry|
|
51
|
+
file_string = entry.get_input_stream.read
|
52
|
+
load_string(file_string)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def add(string)
|
58
|
+
if contains(string)
|
59
|
+
p "'#{string}' is already in the vocabulary"
|
60
|
+
return
|
61
|
+
end
|
62
|
+
prev_node = @storage_array
|
63
|
+
i = 0
|
64
|
+
string_size = string.size
|
65
|
+
string.each_char do |c|
|
66
|
+
current_node = prev_node
|
67
|
+
if !current_node.has_key?(c)
|
68
|
+
current_node[c] = {}
|
69
|
+
end
|
70
|
+
prev_node = current_node[c]
|
71
|
+
i+=1
|
72
|
+
if string_size == i
|
73
|
+
current_node[c]['value'] = string
|
74
|
+
p "New word '#{string}' added"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def contains(string)
|
80
|
+
prev_node = @storage_array
|
81
|
+
i = 0
|
82
|
+
string_size = string.size
|
83
|
+
string.each_char do |c|
|
84
|
+
current_node = prev_node
|
85
|
+
if !current_node.has_key?(c)
|
86
|
+
return false
|
87
|
+
end
|
88
|
+
prev_node = current_node[c]
|
89
|
+
i+=1
|
90
|
+
if string_size == i
|
91
|
+
if current_node[c]['value'] == string
|
92
|
+
return true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def find(string)
|
99
|
+
string_size = string.size
|
100
|
+
if string_size > 2
|
101
|
+
prev_node = @storage_array
|
102
|
+
i = 0
|
103
|
+
string.each_char do |c|
|
104
|
+
current_node = prev_node
|
105
|
+
if !current_node.has_key?(c)
|
106
|
+
return false
|
107
|
+
end
|
108
|
+
prev_node = current_node[c]
|
109
|
+
i+=1
|
110
|
+
if string_size == i
|
111
|
+
return output_words(current_node[c])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def output_words(node)
|
118
|
+
node.keys.each do |child|
|
119
|
+
if child == 'value'
|
120
|
+
@search_words << node[child]
|
121
|
+
else
|
122
|
+
output_words(node[child])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
return @search_words
|
4
126
|
end
|
5
127
|
end
|