ptrie 1.0.0
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/csv_controller.rb +22 -0
- data/lib/prefix_tree.rb +126 -0
- data/lib/prefix_tree_node.rb +13 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 81802d98f47402dd56f3c7c91e945b87f5b849edcce8cf520a520135fef9ccf1
|
4
|
+
data.tar.gz: 3b818cb33bf00f1dd52441cc28099221cc9e295190844b30797d0a4d65148138
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb7a1ff648ee0d2df6b8e80c195a8ca49d26503dfed33d5059252c5b9f57b3a8aecec645b77f29752882fa6d635cb84cb485e75084fedd1c810bef39531d8eea
|
7
|
+
data.tar.gz: bbbed0406c061d73ffd54a38ded3937182bb7f8a67350deccebba1e4e51c07c9d330ffc998e664fd128c2de8559931fb8f391f946560650d24160172be4fa2aa
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
class Csv
|
5
|
+
attr_accessor :path
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@path = 'lib/data.csv'
|
9
|
+
end
|
10
|
+
|
11
|
+
def save_to_csv(word_list)
|
12
|
+
puts word_list.to_s
|
13
|
+
CSV.open(path, 'a') do |csv|
|
14
|
+
csv << word_list
|
15
|
+
end
|
16
|
+
puts 'Successfully added to CSV'
|
17
|
+
end
|
18
|
+
|
19
|
+
def read_from_csv
|
20
|
+
CSV.parse(File.read(@path), converters: :all)
|
21
|
+
end
|
22
|
+
end
|
data/lib/prefix_tree.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/prefix_tree_node'
|
4
|
+
require_relative '../lib/csv_controller'
|
5
|
+
require 'csv'
|
6
|
+
|
7
|
+
class PrefixTree
|
8
|
+
attr_accessor :root
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@root = PrefixTreeNode.new('')
|
12
|
+
@dictionay = []
|
13
|
+
@csv_dictionary = Csv.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_word(word)
|
17
|
+
@dictionay << word
|
18
|
+
letters = word.chars
|
19
|
+
base = @root
|
20
|
+
letters.each { |letter| base = add_letter(letter, base.next) }
|
21
|
+
puts "you added #{word} sucsesfuly"
|
22
|
+
base.word = true
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_word(word)
|
26
|
+
letters = word.chars
|
27
|
+
base = @root
|
28
|
+
|
29
|
+
found_word = letters.all? { |letter| base = find_character(letter, base.next) }
|
30
|
+
yield found_word if block_given?
|
31
|
+
|
32
|
+
base ? base.word : false
|
33
|
+
end
|
34
|
+
|
35
|
+
def includes?(word)
|
36
|
+
find_word(word) { |found| return found }
|
37
|
+
end
|
38
|
+
|
39
|
+
def list(prefix = nil)
|
40
|
+
prefix = '' if prefix.nil?
|
41
|
+
listlist(prefix)
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete_word(word)
|
45
|
+
@dictionay.delete(word)
|
46
|
+
delete_recursively(word, 0, root)
|
47
|
+
end
|
48
|
+
|
49
|
+
def write_csv
|
50
|
+
@csv_dictionary.save_to_csv(list)
|
51
|
+
end
|
52
|
+
|
53
|
+
def read_csv
|
54
|
+
qwe = @csv_dictionary.read_from_csv
|
55
|
+
qwe.each { |_, word| add_word(word.to_s) }
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def listlist(prefix)
|
61
|
+
result = []
|
62
|
+
@dictionay.each { |word| result << word if word.start_with?(prefix) }
|
63
|
+
result.length.zero? ? 'word is not in dictionary' : result
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_letter(character, trie)
|
67
|
+
trie.find { |n| n.value == character } || add_node(character, trie)
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_node(character, trie)
|
71
|
+
PrefixTreeNode.new(character).tap { |new_node| trie << new_node }
|
72
|
+
end
|
73
|
+
|
74
|
+
def find_words_with_prefix(word)
|
75
|
+
letters = word.chars
|
76
|
+
base = @root
|
77
|
+
letters.all? { |letter| base = find_character(letter, base.next) }
|
78
|
+
base
|
79
|
+
end
|
80
|
+
|
81
|
+
def find_character(character, trie)
|
82
|
+
trie.find { |n| n.value == character }
|
83
|
+
end
|
84
|
+
|
85
|
+
def delete_each(current, char, should_delete_ref)
|
86
|
+
if should_delete_ref
|
87
|
+
current.children.delete(char)
|
88
|
+
current.children.length.zero?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def delete_recursively(word, index, root)
|
93
|
+
current = root
|
94
|
+
|
95
|
+
in_trie?(word, index, current)
|
96
|
+
|
97
|
+
char = word[index]
|
98
|
+
return false unless current.children.include?(char)
|
99
|
+
|
100
|
+
next_node = current.children[char]
|
101
|
+
should_delete_ref = delete_recursively(word, index + 1, next_node)
|
102
|
+
|
103
|
+
delete_each(current, char, should_delete_ref)
|
104
|
+
false
|
105
|
+
end
|
106
|
+
|
107
|
+
def in_trie?(word, index, current)
|
108
|
+
if index == word.length
|
109
|
+
return false unless current.is_end
|
110
|
+
|
111
|
+
current.is_end = false
|
112
|
+
current.children.length.zero?
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
trie = PrefixTree.new
|
118
|
+
# trie.read_csv
|
119
|
+
trie.add_word('cat')
|
120
|
+
# trie.add_word('caps')
|
121
|
+
# trie.add_word('cattle')
|
122
|
+
# puts trie.find_word('caps')
|
123
|
+
# puts trie.includes?('ca')
|
124
|
+
# trie.delete_word('cattle')
|
125
|
+
# trie.write_csv
|
126
|
+
puts trie.list()
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ptrie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Beka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple Prefix Tree
|
14
|
+
email: barishvili@unisens.ge
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/csv_controller.rb
|
20
|
+
- lib/prefix_tree.rb
|
21
|
+
- lib/prefix_tree_node.rb
|
22
|
+
homepage: https://rubygems.org/gems/ptrie
|
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.2.3
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Prefix Tree!
|
45
|
+
test_files: []
|