trie-storage 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/trie-storage.rb +19 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8a3fd73429cb8c7b95251a46ae2f4b7c15930d8
|
4
|
+
data.tar.gz: 25bd794fb5d8ca4191708c1962f3942e8e326431
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53bf0d8059d6862aef12fd878be5271edc18af53da416377807ac2c2e2cdaed71d62f6cf594091ecbd8f60883f655fc82a5e28cf5ba29f8fdcffc2e6f9fec070
|
7
|
+
data.tar.gz: b7724ffeede6923581b5a756c1effb168214fdbe45c8d45ce6830ec4b117f5929ae19ee1f929193e2161a7aa4430d5dabc45fd0127b83ae77a253a4e595926a6
|
data/lib/trie-storage.rb
CHANGED
@@ -1,25 +1,33 @@
|
|
1
1
|
require 'zip'
|
2
|
+
#Class trie - storage
|
2
3
|
class Storage
|
3
4
|
attr_accessor :trie
|
4
5
|
def initialize
|
5
6
|
@trie=Node.new
|
6
7
|
end
|
7
8
|
|
9
|
+
#add str to storage
|
8
10
|
def add(str)
|
9
|
-
|
11
|
+
if !str.match(/^[[:alpha:][,]]+$/)
|
12
|
+
raise ArgumentError
|
13
|
+
end
|
14
|
+
words = str.split(",").reject { |c| c.empty? }
|
10
15
|
words.each do |word|
|
11
16
|
@trie.add_word(word)
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
20
|
+
#print all trie words
|
15
21
|
def print
|
16
22
|
puts @trie.get_subtrie_words([],"")
|
17
23
|
end
|
18
24
|
|
25
|
+
#check contains str in storage
|
19
26
|
def contains?(str)
|
20
27
|
@trie.contains?(str)
|
21
28
|
end
|
22
29
|
|
30
|
+
#find all words starting from str
|
23
31
|
def find(str)
|
24
32
|
if str.length<3
|
25
33
|
raise ArgumentError
|
@@ -28,17 +36,20 @@ class Storage
|
|
28
36
|
@trie.find([],str,st)
|
29
37
|
end
|
30
38
|
|
39
|
+
#load words in storage from file
|
31
40
|
def load_from_file(filename)
|
32
41
|
lines=IO.readlines(filename)
|
33
42
|
lines.map {|line| add(line.strip)}
|
34
43
|
end
|
35
44
|
|
45
|
+
#save words from storage to file
|
36
46
|
def save_to_file(filename)
|
37
47
|
File.open(filename, "w+") do |f|
|
38
48
|
f.puts(@trie.get_subtrie_words([],"").join(","))
|
39
49
|
end
|
40
50
|
end
|
41
51
|
|
52
|
+
#load words in storage from zip
|
42
53
|
def load_from_zip(filename)
|
43
54
|
Zip::File.foreach(filename) do |entry|
|
44
55
|
istream = entry.get_input_stream
|
@@ -48,6 +59,7 @@ class Storage
|
|
48
59
|
end
|
49
60
|
end
|
50
61
|
|
62
|
+
#save words from storage to zip
|
51
63
|
def save_to_zip(filename)
|
52
64
|
Zip::File.open(filename, Zip::File::CREATE) {
|
53
65
|
|zipfile|
|
@@ -57,13 +69,16 @@ class Storage
|
|
57
69
|
|
58
70
|
end
|
59
71
|
|
72
|
+
#Class Node of trie
|
60
73
|
class Node
|
74
|
+
# subtrie and property of ending word on this node
|
61
75
|
attr_accessor :subtrie,:final
|
62
76
|
def initialize
|
63
77
|
@final = false
|
64
78
|
@subtrie = {}
|
65
79
|
end
|
66
80
|
|
81
|
+
#check contains str starting from current node
|
67
82
|
def contains?(str)
|
68
83
|
let=str[0]
|
69
84
|
if str.empty?
|
@@ -75,6 +90,7 @@ class Node
|
|
75
90
|
end
|
76
91
|
end
|
77
92
|
|
93
|
+
#add word in trie
|
78
94
|
def add_word(word)
|
79
95
|
let=word[0]
|
80
96
|
if !word.empty?
|
@@ -87,6 +103,7 @@ class Node
|
|
87
103
|
end
|
88
104
|
end
|
89
105
|
|
106
|
+
#get all words starting from current node
|
90
107
|
def get_subtrie_words(res,str)
|
91
108
|
@subtrie.each do |k,node|
|
92
109
|
if node.final
|
@@ -99,6 +116,7 @@ class Node
|
|
99
116
|
res
|
100
117
|
end
|
101
118
|
|
119
|
+
#find all words starting from current node and str
|
102
120
|
def find(res,str,st)
|
103
121
|
let=str[0]
|
104
122
|
if @subtrie[let]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trie-storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danila Potapov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple trie storage gem
|
14
14
|
email: potapovd36@gmail.com
|