jtrie 0.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/jtrie.rb +112 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2fe0bc8f28fbff700f9ba8ec762d43eaaaab312bea737cff08de26a928ed420a
|
|
4
|
+
data.tar.gz: a4c14c137644e08e12bd59b6f33471070ca65eaf25d88d6c3c0d10c0f08c2475
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6fad1ffac14eac381cf11c29e2d74ca3dfe9ef2258600d82c6409d8c7612326141465edd48ae2eb339351fde79312f3cd7a62df5c007f9844b5ebcef3db1082a
|
|
7
|
+
data.tar.gz: fbd20214bc17f3d57e2482e3f63ac861770e3a908d2394ae7747f57cab7cabaa605d3520a4e55f89b6a88ad87aa9058773215a569b7c38c17c6e797e4f714c3d
|
data/lib/jtrie.rb
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#require_relative 'csv_write'
|
|
2
|
+
|
|
3
|
+
class TrieNode
|
|
4
|
+
attr_accessor :is_end, :children
|
|
5
|
+
|
|
6
|
+
def initialize(is_end = false)
|
|
7
|
+
@is_end = is_end
|
|
8
|
+
@children = Array.new(26)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class Jtrie
|
|
13
|
+
attr_accessor :root, :dictionary
|
|
14
|
+
|
|
15
|
+
def initialize
|
|
16
|
+
@root = TrieNode.new
|
|
17
|
+
@dictionay = []
|
|
18
|
+
@csv_dictionary = Csv.new
|
|
19
|
+
@test = []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def add(word)
|
|
23
|
+
@dictionay << word
|
|
24
|
+
# @test << word
|
|
25
|
+
word_chars = word.chars
|
|
26
|
+
current = @root
|
|
27
|
+
0.upto(word.length - 1) do |i|
|
|
28
|
+
letter = word_chars[i].ord - 'a'.ord
|
|
29
|
+
current.children[letter] = TrieNode.new if current.children[letter].nil?
|
|
30
|
+
current = current.children[letter]
|
|
31
|
+
end
|
|
32
|
+
current.is_end = true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def find(word)
|
|
36
|
+
word_chars = word.chars
|
|
37
|
+
current = @root
|
|
38
|
+
(0..word.length - 1).each do |i|
|
|
39
|
+
letter = word_chars[i].ord - 'a'.ord
|
|
40
|
+
return false if current.children[letter].nil?
|
|
41
|
+
|
|
42
|
+
current = current.children[letter]
|
|
43
|
+
end
|
|
44
|
+
current.is_end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def list(prefix)
|
|
48
|
+
result = []
|
|
49
|
+
@dictionay.each { |word| result << word if word.start_with?(prefix) }
|
|
50
|
+
result.length.zero? ? 'word is not in dictionary' : result
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def include?(word)
|
|
54
|
+
find(word) { |found, root| return found && root.word }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def delete_word(word)
|
|
58
|
+
@dictionay.delete(word)
|
|
59
|
+
delete_recursively(word, 0, root)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# def write_csv
|
|
63
|
+
# @csv_dictionary.save_to_csv(@test)
|
|
64
|
+
# end
|
|
65
|
+
|
|
66
|
+
# def read_csv
|
|
67
|
+
# @csv_dictionary.read_from_csv
|
|
68
|
+
# end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def is_in_trie?(word, index, current)
|
|
73
|
+
if index == word.length
|
|
74
|
+
return false unless current.is_end
|
|
75
|
+
|
|
76
|
+
current.is_end = false
|
|
77
|
+
current.children.length.zero?
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def delete_each(current, char, should_delete_ref)
|
|
82
|
+
if should_delete_ref
|
|
83
|
+
current.children.delete(char)
|
|
84
|
+
current.children.length.zero?
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def delete_recursively(word, index, root)
|
|
89
|
+
current = root
|
|
90
|
+
|
|
91
|
+
is_in_trie?(word, index, current)
|
|
92
|
+
|
|
93
|
+
char = word[index]
|
|
94
|
+
return false unless current.children.include?(char)
|
|
95
|
+
|
|
96
|
+
next_node = current.children[char]
|
|
97
|
+
should_delete_ref = delete_recursively(word, index + 1, next_node)
|
|
98
|
+
|
|
99
|
+
delete_each(current, char, should_delete_ref)
|
|
100
|
+
false
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# trie = Trie.new
|
|
105
|
+
# trie.add('cat')
|
|
106
|
+
# trie.add('camp')
|
|
107
|
+
# trie.add('cave')
|
|
108
|
+
# trie.add('cask')
|
|
109
|
+
# trie.add('cow')
|
|
110
|
+
# trie.add('apple')
|
|
111
|
+
# trie.add('tester')
|
|
112
|
+
# puts trie.list('')
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jtrie
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jeko Apulava
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-05-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: fast prefix tree implementation
|
|
14
|
+
email: japulava@unisens.ge
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/jtrie.rb
|
|
20
|
+
homepage: https://rubygems.org/gems/jtrie
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubygems_version: 3.2.32
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: Prefix tree
|
|
43
|
+
test_files: []
|