triepr 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/csvtrie.rb +36 -0
- data/lib/node.rb +13 -0
- data/lib/trie.rb +71 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5f2848ce74574ea7fb8b07e48cd2a19ef2e0f9e402cac9fd342f98aac4209839
|
4
|
+
data.tar.gz: 14cb16d6073729f4430e4979ee8c547ac8fbcc333e827da38fae2468daad26c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 126d8edadc705916fa848756f6d948a3d6192d6798c056c8699e9d81701ac888edf495fe95e086f284c4dd5f793a5543192846fc369f64d8c393e16a34d2135b
|
7
|
+
data.tar.gz: e83124bc25e3c9c2073820b1791878843c6912495cb6b85109efa3c57a2c18739b7181d1aacf22ccdc3e7707864b50c2ffcda2b41cac40b7fb40e8a373266fb2
|
data/lib/csvtrie.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require_relative 'trie'
|
3
|
+
|
4
|
+
class Csvtree
|
5
|
+
def initialize(a = Trie.new)
|
6
|
+
@a = a
|
7
|
+
end
|
8
|
+
|
9
|
+
def fill_csv(array_of_words)
|
10
|
+
CSV.open('../test.csv', 'wb') do |csv|
|
11
|
+
csv << %w[id string]
|
12
|
+
array_of_words.each_with_index { |word, index| csv << [index, word] }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def read_csv
|
17
|
+
strings = CSV.parse(File.read('../fill_words.csv'), headers: true).by_col[1]
|
18
|
+
add_strings(strings)
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_strings(strings)
|
22
|
+
strings.each { |string| @a.add_word(string) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
a = Trie.new
|
27
|
+
|
28
|
+
a.add_word('cat')
|
29
|
+
a.add_word('caps')
|
30
|
+
a.find_word('ca')
|
31
|
+
a.include?('cap')
|
32
|
+
|
33
|
+
b = Csvtree.new(a)
|
34
|
+
b.read_csv
|
35
|
+
array_of_words = a.list('ca')
|
36
|
+
b.fill_csv(array_of_words)
|
data/lib/node.rb
ADDED
data/lib/trie.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative 'node'
|
2
|
+
|
3
|
+
class Trie
|
4
|
+
def initialize
|
5
|
+
@root = Node.new('*')
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_word(word)
|
9
|
+
letters = word.chars
|
10
|
+
base = @root
|
11
|
+
letters.each { |letter| base = add_letter(letter, base.next) }
|
12
|
+
|
13
|
+
base.word = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_letter(character, trie)
|
17
|
+
trie.find { |n| n.value == character } || add_node(character, trie)
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_node(character, trie)
|
21
|
+
Node.new(character).tap { |new_node| trie << new_node }
|
22
|
+
end
|
23
|
+
|
24
|
+
def find_word(word)
|
25
|
+
found_word, base = get_word_and_base(word)
|
26
|
+
yield found_word if block_given?
|
27
|
+
|
28
|
+
base ? base.word : false
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_word_and_base(word)
|
32
|
+
letters = word.chars
|
33
|
+
base = @root
|
34
|
+
|
35
|
+
found_word = letters.all? { |letter| base = find_character(letter, base.next) }
|
36
|
+
[found_word, base]
|
37
|
+
end
|
38
|
+
|
39
|
+
def find_words_with_prefix(word)
|
40
|
+
found_word, base = get_word_and_base(word)
|
41
|
+
base
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_character(character, trie)
|
45
|
+
trie.find { |n| n.value == character }
|
46
|
+
end
|
47
|
+
|
48
|
+
def include?(word)
|
49
|
+
find_word(word) { |found| return found }
|
50
|
+
end
|
51
|
+
|
52
|
+
def list(prefix)
|
53
|
+
stack = []
|
54
|
+
words = []
|
55
|
+
prefix_stack = []
|
56
|
+
stack << find_words_with_prefix(prefix) # "a"
|
57
|
+
prefix_stack << prefix.chars.take(prefix.size - 1) # "c"
|
58
|
+
return [] unless stack.first
|
59
|
+
|
60
|
+
until stack.empty?
|
61
|
+
node = stack.pop
|
62
|
+
prefix_stack.pop and next if node == :guard_node
|
63
|
+
|
64
|
+
prefix_stack << node.value
|
65
|
+
stack << :guard_node
|
66
|
+
words << prefix_stack.join if node.word
|
67
|
+
node.next.each { |n| stack << n }
|
68
|
+
end
|
69
|
+
words
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: triepr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ana Meskhi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: this is prefix trie
|
14
|
+
email: ameskhi@unisens.ge
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/csvtrie.rb
|
20
|
+
- lib/node.rb
|
21
|
+
- lib/trie.rb
|
22
|
+
homepage: https://rubygems.org/gems/triepr
|
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: triepr!
|
45
|
+
test_files: []
|