Storage_strings 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/storage.rb +128 -0
  2. metadata +45 -0
data/lib/storage.rb ADDED
@@ -0,0 +1,128 @@
1
+ require 'zip/zip'
2
+ class Storage
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
126
+ end
127
+
128
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Storage_strings
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Shevchenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/storage.rb
21
+ homepage:
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.23
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: The class which is doing storage of strings using the TRIE
45
+ test_files: []