pretrie 0.0.3
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 +7 -0
- data/lib/prefixtree/prefixtreenode.rb +9 -0
- data/lib/prefixtree/tocsv.rb +21 -0
- data/lib/prefixtree.rb +123 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7d72e5a7117dc00b96ddfb3aed12a90db76adfeeed454a4141c3fc752985dfb3
|
|
4
|
+
data.tar.gz: 91f22036f643489c27f691801da3e347e062ec05251e86af1f962b63d37a8f6c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0d807b739d9fc0db111ba5b21c75a06f97e3b0663e4fd48ca3084f0916e080d22dad1bac105c6c6d78182246ca046e92a69cff4a189641caf5f86e57a47b18ac
|
|
7
|
+
data.tar.gz: 2d280c1ad2f81f5f6a00bf9f5ee24eb78a4145878e8121294e4b14d19e81d21fa5b07d6a68e2c8e50e3bf427e51d628a015b520da53678ff44ed45cf492781ab
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
$LOAD_PATH << "../lib"
|
|
2
|
+
require 'csv'
|
|
3
|
+
|
|
4
|
+
class FileWriter
|
|
5
|
+
attr_accessor :path
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@path = 'lib/data.csv'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def save_to_csv(word_list)
|
|
12
|
+
CSV.open(path, 'a') do |csv|
|
|
13
|
+
csv << word_list
|
|
14
|
+
end
|
|
15
|
+
puts 'Successfully added to CSV'
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def read_from_csv
|
|
19
|
+
CSV.read(path).to_a
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/prefixtree.rb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
$LOAD_PATH << "../lib"
|
|
2
|
+
require "prefixtree/prefixtreenode"
|
|
3
|
+
require "prefixtree/tocsv"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PrefixTree
|
|
8
|
+
attr_accessor :root, :container
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@root = Node.new
|
|
12
|
+
@container = []
|
|
13
|
+
@csv_container = FileWriter.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def add(word)
|
|
17
|
+
container << word
|
|
18
|
+
current = root
|
|
19
|
+
word.each_char do |char|
|
|
20
|
+
current = adder_logic(current, char)
|
|
21
|
+
end
|
|
22
|
+
current.is_word = true
|
|
23
|
+
current
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def include?(prefix)
|
|
27
|
+
current = root
|
|
28
|
+
prefix.each_char do |char|
|
|
29
|
+
return false unless current.data.include?(char)
|
|
30
|
+
current = current.data[char]
|
|
31
|
+
end
|
|
32
|
+
true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def find(prefix)
|
|
36
|
+
current = root
|
|
37
|
+
prefix.each_char do |char|
|
|
38
|
+
return false unless current.data.include?(char)
|
|
39
|
+
current = current.data[char]
|
|
40
|
+
end
|
|
41
|
+
current.is_word
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def list(prefix = nil)
|
|
45
|
+
return container if prefix.nil?
|
|
46
|
+
verified_words(container, prefix)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def delete(word)
|
|
50
|
+
container.delete(word)
|
|
51
|
+
delete_recursively(word, 0, root)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def save_all_to_csv
|
|
55
|
+
@csv_container.save_to_csv(container)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def read_all_from_csv
|
|
59
|
+
@csv_container.read_from_csv
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def adder_logic(current, char)
|
|
65
|
+
if current.data.include?(char)
|
|
66
|
+
current = current.data[char]
|
|
67
|
+
else
|
|
68
|
+
new_node = Node.new
|
|
69
|
+
current.data[char] = new_node
|
|
70
|
+
current = new_node
|
|
71
|
+
end
|
|
72
|
+
current
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def verified_words(container, prefix)
|
|
76
|
+
res = []
|
|
77
|
+
container.each do |word|
|
|
78
|
+
res << word if word.start_with?(prefix)
|
|
79
|
+
end
|
|
80
|
+
res.length == 0 ? false : res
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def word_exists?(word, index, current)
|
|
84
|
+
if index == word.length
|
|
85
|
+
return false unless current.is_word
|
|
86
|
+
|
|
87
|
+
current.is_word = false
|
|
88
|
+
current.data.length.zero?
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def delete_reference(current, char, should_delete_ref)
|
|
93
|
+
if should_delete_ref
|
|
94
|
+
current.data.delete(char)
|
|
95
|
+
current.data.length.zero?
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def delete_recursively(word, index, root)
|
|
100
|
+
current = root
|
|
101
|
+
|
|
102
|
+
word_exists?(word, index, current)
|
|
103
|
+
|
|
104
|
+
char = word[index]
|
|
105
|
+
return false unless current.data.include?(char)
|
|
106
|
+
|
|
107
|
+
next_node = current.data[char]
|
|
108
|
+
should_delete_ref = delete_recursively(word, index + 1, next_node)
|
|
109
|
+
|
|
110
|
+
delete_reference(current, char, should_delete_ref)
|
|
111
|
+
false
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
test = PrefixTree.new
|
|
117
|
+
|
|
118
|
+
test.add("a")
|
|
119
|
+
test.save_all_to_csv
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pretrie
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Giga Chubinidze
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-05-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A data structure for easily saving and retrieving strings
|
|
14
|
+
email: gchubinidze@unisens.ge
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/prefixtree.rb
|
|
20
|
+
- lib/prefixtree/prefixtreenode.rb
|
|
21
|
+
- lib/prefixtree/tocsv.rb
|
|
22
|
+
homepage: https://rubygems.org/gems/prefixtree
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubygems_version: 3.1.6
|
|
42
|
+
signing_key:
|
|
43
|
+
specification_version: 4
|
|
44
|
+
summary: Prefix tree data structure
|
|
45
|
+
test_files: []
|