rambling-trie 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/LICENSE +1 -1
- data/README.md +68 -29
- data/lib/rambling/trie/comparable.rb +2 -3
- data/lib/rambling/trie/compressible.rb +3 -5
- data/lib/rambling/trie/compressor.rb +2 -12
- data/lib/rambling/trie/configuration/properties.rb +8 -14
- data/lib/rambling/trie/configuration/provider_collection.rb +19 -27
- data/lib/rambling/trie/container.rb +28 -42
- data/lib/rambling/trie/enumerable.rb +4 -2
- data/lib/rambling/trie/nodes/compressed.rb +4 -5
- data/lib/rambling/trie/nodes/missing.rb +1 -2
- data/lib/rambling/trie/nodes/node.rb +21 -34
- data/lib/rambling/trie/nodes/raw.rb +2 -2
- data/lib/rambling/trie/readers/plain_text.rb +10 -6
- data/lib/rambling/trie/readers/reader.rb +19 -0
- data/lib/rambling/trie/readers.rb +1 -1
- data/lib/rambling/trie/serializers/file.rb +1 -1
- data/lib/rambling/trie/serializers/marshal.rb +12 -20
- data/lib/rambling/trie/serializers/serializer.rb +27 -0
- data/lib/rambling/trie/serializers/yaml.rb +10 -16
- data/lib/rambling/trie/serializers/zip.rb +9 -5
- data/lib/rambling/trie/serializers.rb +1 -1
- data/lib/rambling/trie/stringifyable.rb +1 -1
- data/lib/rambling/trie/version.rb +1 -1
- data/lib/rambling/trie.rb +19 -22
- data/rambling-trie.gemspec +9 -5
- data/spec/integration/rambling/trie_spec.rb +49 -20
- data/spec/lib/rambling/trie/comparable_spec.rb +29 -39
- data/spec/lib/rambling/trie/compressor_spec.rb +17 -14
- data/spec/lib/rambling/trie/configuration/properties_spec.rb +25 -7
- data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +44 -16
- data/spec/lib/rambling/trie/container_spec.rb +202 -327
- data/spec/lib/rambling/trie/enumerable_spec.rb +18 -10
- data/spec/lib/rambling/trie/inspectable_spec.rb +9 -3
- data/spec/lib/rambling/trie/nodes/compressed_spec.rb +6 -0
- data/spec/lib/rambling/trie/nodes/node_spec.rb +1 -1
- data/spec/lib/rambling/trie/nodes/raw_spec.rb +32 -27
- data/spec/lib/rambling/trie/readers/plain_text_spec.rb +11 -1
- data/spec/lib/rambling/trie/readers/reader_spec.rb +14 -0
- data/spec/lib/rambling/trie/serializers/file_spec.rb +2 -4
- data/spec/lib/rambling/trie/serializers/marshal_spec.rb +2 -4
- data/spec/lib/rambling/trie/serializers/serializer_spec.rb +21 -0
- data/spec/lib/rambling/trie/serializers/yaml_spec.rb +2 -4
- data/spec/lib/rambling/trie/serializers/zip_spec.rb +24 -16
- data/spec/lib/rambling/trie/stringifyable_spec.rb +17 -13
- data/spec/lib/rambling/trie_spec.rb +107 -45
- data/spec/spec_helper.rb +16 -9
- data/spec/support/shared_examples/a_compressible_trie.rb +9 -3
- data/spec/support/shared_examples/a_container_partial_word.rb +17 -0
- data/spec/support/shared_examples/a_container_scan.rb +14 -0
- data/spec/support/shared_examples/a_container_word.rb +43 -0
- data/spec/support/shared_examples/a_container_words_within.rb +44 -0
- data/spec/support/shared_examples/a_serializable_trie.rb +5 -9
- data/spec/support/shared_examples/a_serializer.rb +37 -14
- data/spec/support/shared_examples/a_trie_data_structure.rb +24 -10
- data/spec/support/shared_examples/a_trie_node.rb +22 -14
- data/spec/support/shared_examples/a_trie_node_implementation.rb +40 -43
- metadata +25 -9
@@ -9,14 +9,13 @@ module Rambling
|
|
9
9
|
# trying to add a word to the current compressed trie node
|
10
10
|
# @param [String] _ the word to add to the trie.
|
11
11
|
# @raise [InvalidOperation] if the trie is already compressed.
|
12
|
-
# @return [
|
12
|
+
# @return [void]
|
13
13
|
def add _
|
14
|
-
raise Rambling::Trie::InvalidOperation,
|
15
|
-
'Cannot add word to compressed trie'
|
14
|
+
raise Rambling::Trie::InvalidOperation, 'Cannot add word to compressed trie'
|
16
15
|
end
|
17
16
|
|
18
|
-
# Always return
|
19
|
-
# @return [Boolean] always
|
17
|
+
# Always return +true+ for a compressed node.
|
18
|
+
# @return [Boolean] always +true+ for a compressed node.
|
20
19
|
def compressed?
|
21
20
|
true
|
22
21
|
end
|
@@ -3,8 +3,7 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Nodes
|
6
|
-
# A representation of a missing node in the trie data structure. Returned
|
7
|
-
# when a node is not found.
|
6
|
+
# A representation of a missing node in the trie data structure. Returned when a node is not found.
|
8
7
|
class Missing < Rambling::Trie::Nodes::Node
|
9
8
|
end
|
10
9
|
end
|
@@ -22,8 +22,7 @@ module Rambling
|
|
22
22
|
attr_reader :letter
|
23
23
|
|
24
24
|
# Child nodes tree.
|
25
|
-
# @return [Hash] the
|
26
|
-
# node`.
|
25
|
+
# @return [Hash<Symbol, Node>] the children tree hash, consisting of +:letter => node+.
|
27
26
|
attr_accessor :children_tree
|
28
27
|
|
29
28
|
# Parent node.
|
@@ -31,7 +30,7 @@ module Rambling
|
|
31
30
|
attr_accessor :parent
|
32
31
|
|
33
32
|
# Creates a new node.
|
34
|
-
# @param [Symbol, nil] letter the Node's letter value
|
33
|
+
# @param [Symbol, nil] letter the Node's letter value.
|
35
34
|
# @param [Node, nil] parent the parent of the current node.
|
36
35
|
def initialize letter = nil, parent = nil, children_tree = {}
|
37
36
|
@letter = letter
|
@@ -40,8 +39,7 @@ module Rambling
|
|
40
39
|
end
|
41
40
|
|
42
41
|
# Child nodes.
|
43
|
-
# @return [Array<Node>] the array of
|
44
|
-
# in the current node.
|
42
|
+
# @return [Array<Node>] the array of child nodes contained in the current node.
|
45
43
|
def children
|
46
44
|
children_tree.values
|
47
45
|
end
|
@@ -51,26 +49,25 @@ module Rambling
|
|
51
49
|
def first_child
|
52
50
|
return if children_tree.empty?
|
53
51
|
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
# rubocop:disable Lint/UnreachableLoop
|
53
|
+
children_tree.each_value { |child| return child }
|
54
|
+
# rubocop:enable Lint/UnreachableLoop
|
57
55
|
end
|
58
56
|
|
59
57
|
# Indicates if the current node is the root node.
|
60
|
-
# @return [Boolean]
|
61
|
-
# otherwise.
|
58
|
+
# @return [Boolean] +true+ if the node does not have a parent, +false+ otherwise.
|
62
59
|
def root?
|
63
60
|
!parent
|
64
61
|
end
|
65
62
|
|
66
63
|
# Indicates if a {Node Node} is terminal or not.
|
67
|
-
# @return [Boolean]
|
64
|
+
# @return [Boolean] +true+ for terminal nodes, +false+ otherwise.
|
68
65
|
def terminal?
|
69
66
|
!!terminal
|
70
67
|
end
|
71
68
|
|
72
69
|
# Mark {Node Node} as terminal.
|
73
|
-
# @return [
|
70
|
+
# @return [self] the modified node.
|
74
71
|
def terminal!
|
75
72
|
self.terminal = true
|
76
73
|
self
|
@@ -82,8 +79,7 @@ module Rambling
|
|
82
79
|
|
83
80
|
# Checks if a path for a set of characters exists in the trie.
|
84
81
|
# @param [Array<String>] chars the characters to look for in the trie.
|
85
|
-
# @return [Boolean]
|
86
|
-
# otherwise.
|
82
|
+
# @return [Boolean] +true+ if the characters are found, +false+ otherwise.
|
87
83
|
def partial_word? chars
|
88
84
|
return true if chars.empty?
|
89
85
|
|
@@ -92,8 +88,7 @@ module Rambling
|
|
92
88
|
|
93
89
|
# Checks if a path for set of characters represents a word in the trie.
|
94
90
|
# @param [Array<String>] chars the characters to look for in the trie.
|
95
|
-
# @return [Boolean]
|
96
|
-
# `false` otherwise.
|
91
|
+
# @return [Boolean] +true+ if the characters are found and form a word, +false+ otherwise.
|
97
92
|
def word? chars = []
|
98
93
|
return terminal? if chars.empty?
|
99
94
|
|
@@ -112,8 +107,7 @@ module Rambling
|
|
112
107
|
|
113
108
|
# Returns all words that match a prefix of any length within chars.
|
114
109
|
# @param [String] chars the chars to base the prefix on.
|
115
|
-
# @return [Enumerator<String>] all the words that match a prefix
|
116
|
-
# by chars.
|
110
|
+
# @return [Enumerator<String>] all the words that match a prefix by chars.
|
117
111
|
# @yield [String] each word found.
|
118
112
|
def match_prefix chars
|
119
113
|
return enum_for :match_prefix, chars unless block_given?
|
@@ -128,8 +122,7 @@ module Rambling
|
|
128
122
|
# Get {Node Node} corresponding to a given letter.
|
129
123
|
# @param [Symbol] letter the letter to search for in the node.
|
130
124
|
# @return [Node] the node corresponding to that letter.
|
131
|
-
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D
|
132
|
-
# Hash#[]
|
125
|
+
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D Hash#[]
|
133
126
|
def [] letter
|
134
127
|
children_tree[letter]
|
135
128
|
end
|
@@ -137,31 +130,25 @@ module Rambling
|
|
137
130
|
# Set the {Node Node} that corresponds to a given letter.
|
138
131
|
# @param [Symbol] letter the letter to insert or update in the node's
|
139
132
|
# @param [Node] node the {Node Node} to assign to that letter.
|
140
|
-
# @return [Node] the node corresponding to the inserted or
|
141
|
-
#
|
142
|
-
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D
|
143
|
-
# Hash#[]
|
133
|
+
# @return [Node] the node corresponding to the inserted or updated letter.
|
134
|
+
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D Hash#[]
|
144
135
|
def []= letter, node
|
145
136
|
children_tree[letter] = node
|
146
137
|
end
|
147
138
|
|
148
|
-
# Check if a {Node Node}'s children tree contains a given
|
149
|
-
# letter.
|
139
|
+
# Check if a {Node Node}'s children tree contains a given letter.
|
150
140
|
# @param [Symbol] letter the letter to search for in the node.
|
151
|
-
# @return [Boolean]
|
152
|
-
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-has_key-3F
|
153
|
-
# Hash#key?
|
141
|
+
# @return [Boolean] +true+ if the letter is present, +false+ otherwise.
|
142
|
+
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-has_key-3F Hash#key?
|
154
143
|
def key? letter
|
155
144
|
children_tree.key? letter
|
156
145
|
end
|
157
146
|
|
158
147
|
# Delete a given letter and its corresponding {Node Node} from
|
159
|
-
#
|
160
|
-
# @param [Symbol] letter the letter to delete from the node's children
|
161
|
-
# tree.
|
148
|
+
# this {Node Node}'s children tree.
|
149
|
+
# @param [Symbol] letter the letter to delete from the node's children tree.
|
162
150
|
# @return [Node] the node corresponding to the deleted letter.
|
163
|
-
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-delete
|
164
|
-
# Hash#delete
|
151
|
+
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-delete Hash#delete
|
165
152
|
def delete letter
|
166
153
|
children_tree.delete letter
|
167
154
|
end
|
@@ -17,8 +17,8 @@ module Rambling
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
# Always return
|
21
|
-
# @return [Boolean] always
|
20
|
+
# Always return +false+ for a raw (uncompressed) node.
|
21
|
+
# @return [Boolean] always +false+ for a raw (uncompressed) node.
|
22
22
|
def compressed?
|
23
23
|
false
|
24
24
|
end
|
@@ -3,14 +3,18 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Readers
|
6
|
-
# File reader for
|
7
|
-
class PlainText
|
8
|
-
# Yields each word read from a
|
9
|
-
# @param [String] filepath the full path of the file to load the words
|
10
|
-
# from.
|
6
|
+
# File reader for +.txt+ files.
|
7
|
+
class PlainText < Reader
|
8
|
+
# Yields each word read from a +.txt+ file.
|
9
|
+
# @param [String] filepath the full path of the file to load the words from.
|
11
10
|
# @yield [String] Each line read from the file.
|
11
|
+
# @return [self]
|
12
12
|
def each_word filepath
|
13
|
-
|
13
|
+
return enum_for :each_word unless block_given?
|
14
|
+
|
15
|
+
::File.foreach(filepath) { |line| yield line.chomp! }
|
16
|
+
|
17
|
+
self
|
14
18
|
end
|
15
19
|
end
|
16
20
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rambling
|
4
|
+
module Trie
|
5
|
+
module Readers
|
6
|
+
# Base class for all readers.
|
7
|
+
class Reader
|
8
|
+
# Yields each word read from given file.
|
9
|
+
# @abstract Subclass and override {#each_word} to fit to a particular file format.
|
10
|
+
# @param [String] filepath the full path of the file to load the words from.
|
11
|
+
# @yield [String] Each line read from the file.
|
12
|
+
# @return [self]
|
13
|
+
def each_word filepath
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -4,7 +4,7 @@ module Rambling
|
|
4
4
|
module Trie
|
5
5
|
module Serializers
|
6
6
|
# Basic file serializer. Dumps/loads string contents from files.
|
7
|
-
class File
|
7
|
+
class File < Serializer
|
8
8
|
# Loads contents from a specified filepath.
|
9
9
|
# @param [String] filepath the filepath to load contents from.
|
10
10
|
# @return [String] all contents of the file.
|
@@ -3,38 +3,30 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Serializers
|
6
|
-
# Serializer for Ruby marshal format (
|
7
|
-
class Marshal
|
6
|
+
# Serializer for Ruby marshal format (+.marshal+) files.
|
7
|
+
class Marshal < Serializer
|
8
8
|
# Creates a new Marshal serializer.
|
9
|
-
# @param [Serializer] serializer the serializer responsible to write to
|
10
|
-
# and read from disk.
|
9
|
+
# @param [Serializer] serializer the serializer responsible to write to and read from disk.
|
11
10
|
def initialize serializer = nil
|
11
|
+
super()
|
12
12
|
@serializer = serializer || Rambling::Trie::Serializers::File.new
|
13
13
|
end
|
14
14
|
|
15
|
-
# Loads marshaled object from contents in filepath and deserializes it
|
16
|
-
#
|
17
|
-
# @param [String] filepath the full path of the file to load the
|
18
|
-
# marshaled object from.
|
15
|
+
# Loads marshaled object from contents in filepath and deserializes it into a {Nodes::Node Node}.
|
16
|
+
# @param [String] filepath the full path of the file to load the marshaled object from.
|
19
17
|
# @return [Nodes::Node] The deserialized {Nodes::Node Node}.
|
20
|
-
# @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load
|
21
|
-
#
|
22
|
-
#
|
23
|
-
# {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load
|
24
|
-
# Marshal.load} is generally discouraged. Only use this with trusted
|
25
|
-
# input.
|
18
|
+
# @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load
|
19
|
+
# @note Use of {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load} is generally
|
20
|
+
# discouraged. Only use this with trusted input.
|
26
21
|
def load filepath
|
27
22
|
::Marshal.load serializer.load filepath
|
28
23
|
end
|
29
24
|
|
30
|
-
# Serializes a {Nodes::Node Node} and dumps it as a marshaled object
|
31
|
-
# into filepath.
|
25
|
+
# Serializes a {Nodes::Node Node} and dumps it as a marshaled object into filepath.
|
32
26
|
# @param [Nodes::Node] node the node to serialize
|
33
|
-
# @param [String] filepath the full path of the file to dump the
|
34
|
-
# marshaled object into.
|
27
|
+
# @param [String] filepath the full path of the file to dump the marshaled object into.
|
35
28
|
# @return [Numeric] number of bytes written to disk.
|
36
|
-
# @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-dump
|
37
|
-
# Marshal.dump
|
29
|
+
# @see https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-dump Marshal.dump
|
38
30
|
def dump node, filepath
|
39
31
|
serializer.dump ::Marshal.dump(node), filepath
|
40
32
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rambling
|
4
|
+
module Trie
|
5
|
+
module Serializers
|
6
|
+
# Base class for all serializers.
|
7
|
+
class Serializer
|
8
|
+
# Loads contents from a specified filepath.
|
9
|
+
# @abstract Subclass and override {#load} to parse the desired format.
|
10
|
+
# @param [String] filepath the filepath to load contents from.
|
11
|
+
# @return [TContents] parsed contents from given file.
|
12
|
+
def load filepath
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
# Dumps contents into a specified filepath.
|
17
|
+
# @abstract Subclass and override {#dump} to output the desired format.
|
18
|
+
# @param [TContents] contents the contents to dump into given file.
|
19
|
+
# @param [String] filepath the filepath to dump the contents to.
|
20
|
+
# @return [Numeric] number of bytes written to disk.
|
21
|
+
def dump contents, filepath
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -3,22 +3,19 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Serializers
|
6
|
-
# Serializer for Ruby yaml format (
|
7
|
-
class Yaml
|
6
|
+
# Serializer for Ruby yaml format (+.yaml+, or +.yml+) files.
|
7
|
+
class Yaml < Serializer
|
8
8
|
# Creates a new Yaml serializer.
|
9
|
-
# @param [Serializer] serializer the serializer responsible to write to
|
10
|
-
# and read from disk.
|
9
|
+
# @param [Serializer] serializer the serializer responsible to write to and read from disk.
|
11
10
|
def initialize serializer = nil
|
11
|
+
super()
|
12
12
|
@serializer = serializer || Rambling::Trie::Serializers::File.new
|
13
13
|
end
|
14
14
|
|
15
|
-
# Loads serialized object from YAML file in filepath and deserializes
|
16
|
-
#
|
17
|
-
# @param [String] filepath the full path of the file to load the
|
18
|
-
# serialized YAML object from.
|
15
|
+
# Loads serialized object from YAML file in filepath and deserializes it into a {Nodes::Node Node}.
|
16
|
+
# @param [String] filepath the full path of the file to load the serialized YAML object from.
|
19
17
|
# @return [Nodes::Node] The deserialized {Nodes::Node Node}.
|
20
|
-
# @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load
|
21
|
-
# Psych.safe_load
|
18
|
+
# @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-safe_load Psych.safe_load
|
22
19
|
def load filepath
|
23
20
|
require 'yaml'
|
24
21
|
::YAML.safe_load(
|
@@ -32,14 +29,11 @@ module Rambling
|
|
32
29
|
)
|
33
30
|
end
|
34
31
|
|
35
|
-
# Serializes a {Nodes::Node Node} and dumps it as a YAML object into
|
36
|
-
# filepath.
|
32
|
+
# Serializes a {Nodes::Node Node} and dumps it as a YAML object into filepath.
|
37
33
|
# @param [Nodes::Node] node the node to serialize
|
38
|
-
# @param [String] filepath the full path of the file to dump the YAML
|
39
|
-
# object into.
|
34
|
+
# @param [String] filepath the full path of the file to dump the YAML object into.
|
40
35
|
# @return [Numeric] number of bytes written to disk.
|
41
|
-
# @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-dump
|
42
|
-
# Psych.dump
|
36
|
+
# @see https://ruby-doc.org/stdlib-2.7.0/libdoc/psych/rdoc/Psych.html#method-c-dump Psych.dump
|
43
37
|
def dump node, filepath
|
44
38
|
require 'yaml'
|
45
39
|
serializer.dump ::YAML.dump(node), filepath
|
@@ -3,20 +3,22 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Serializers
|
6
|
-
# Zip file serializer. Dumps/loads contents from zip files.
|
7
|
-
# detects if zip file contains
|
8
|
-
|
6
|
+
# Zip file serializer. Dumps/loads contents from +.zip+ files.
|
7
|
+
# Automatically detects if zip file contains a +.marshal+ or +.yml+ file,
|
8
|
+
# or any other registered +:format => serializer+ combo.
|
9
|
+
class Zip < Serializer
|
9
10
|
# Creates a new Zip serializer.
|
10
11
|
# @param [Configuration::Properties] properties the configuration
|
11
12
|
# properties set up so far.
|
12
13
|
def initialize properties
|
14
|
+
super()
|
13
15
|
@properties = properties
|
14
16
|
end
|
15
17
|
|
16
18
|
# Unzip contents from specified filepath and load in contents from
|
17
19
|
# unzipped files.
|
18
20
|
# @param [String] filepath the filepath to load contents from.
|
19
|
-
# @return [
|
21
|
+
# @return [TContents] all contents of the unzipped loaded file.
|
20
22
|
# @see https://github.com/rubyzip/rubyzip#reading-a-zip-file Zip
|
21
23
|
# reading a file
|
22
24
|
def load filepath
|
@@ -35,7 +37,7 @@ module Rambling
|
|
35
37
|
# Dumps contents and zips into a specified filepath.
|
36
38
|
# @param [String] contents the contents to dump.
|
37
39
|
# @param [String] filepath the filepath to dump the contents to.
|
38
|
-
# @return [
|
40
|
+
# @return [TContents] number of bytes written to disk.
|
39
41
|
# @see https://github.com/rubyzip/rubyzip#basic-zip-archive-creation
|
40
42
|
# Zip archive creation
|
41
43
|
def dump contents, filepath
|
@@ -50,6 +52,8 @@ module Rambling
|
|
50
52
|
|
51
53
|
zip.add filename, entry_path
|
52
54
|
end
|
55
|
+
|
56
|
+
::File.size filepath
|
53
57
|
end
|
54
58
|
|
55
59
|
private
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
|
-
# Provides the String representation behavior for the trie data structure.
|
5
|
+
# Provides the +String+ representation behavior for the trie data structure.
|
6
6
|
module Stringifyable
|
7
7
|
# String representation of the current node, if it is a terminal node.
|
8
8
|
# @return [String] the string representation of the current node.
|
data/lib/rambling/trie.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
%w(
|
4
|
-
comparable compressible compressor configuration container enumerable
|
5
|
-
|
6
|
-
version
|
4
|
+
comparable compressible compressor configuration container enumerable inspectable invalid_operation
|
5
|
+
readers serializers stringifyable nodes version
|
7
6
|
).each do |file|
|
8
7
|
require File.join('rambling', 'trie', file)
|
9
8
|
end
|
10
9
|
|
11
10
|
# General namespace for all Rambling gems.
|
12
11
|
module Rambling
|
13
|
-
# Entry point for rambling-trie API.
|
12
|
+
# Entry point for +rambling-trie+ API.
|
14
13
|
module Trie
|
15
14
|
class << self
|
16
|
-
# Creates a new Rambling::Trie
|
15
|
+
# Creates a new +Rambling::Trie+. Entry point for the +rambling-trie+ API.
|
17
16
|
# @param [String, nil] filepath the file to load the words from.
|
18
|
-
# @param [Reader, nil] reader the file parser to get each word.
|
17
|
+
# @param [Readers::Reader, nil] reader the file parser to get each word.
|
19
18
|
# @return [Container] the trie just created.
|
20
19
|
# @yield [Container] the trie just created.
|
21
20
|
# @see Rambling::Trie::Readers Readers.
|
@@ -23,8 +22,10 @@ module Rambling
|
|
23
22
|
root = root_builder.call
|
24
23
|
|
25
24
|
Rambling::Trie::Container.new root, compressor do |container|
|
25
|
+
# noinspection RubyMismatchedArgumentType
|
26
26
|
if filepath
|
27
27
|
reader ||= readers.resolve filepath
|
28
|
+
# noinspection RubyMismatchedArgumentType,RubyNilAnalysis
|
28
29
|
reader.each_word filepath do |word|
|
29
30
|
container << word
|
30
31
|
end
|
@@ -36,18 +37,15 @@ module Rambling
|
|
36
37
|
|
37
38
|
# Loads an existing trie from disk into memory. By default, it will
|
38
39
|
# deduce the correct way to deserialize based on the file extension.
|
39
|
-
# Available formats are
|
40
|
+
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
|
40
41
|
# previous formats. You can also define your own.
|
41
42
|
# @param [String] filepath the file to load the words from.
|
42
|
-
# @param [Serializer, nil] serializer the object responsible of loading
|
43
|
-
# the trie from disk
|
43
|
+
# @param [Serializer, nil] serializer the object responsible of loading the trie from disk.
|
44
44
|
# @return [Container] the trie just loaded.
|
45
45
|
# @yield [Container] the trie just loaded.
|
46
46
|
# @see Rambling::Trie::Serializers Serializers.
|
47
|
-
# @note Use of
|
48
|
-
#
|
49
|
-
# Marshal.load} is generally discouraged. Only use the `.marshal`
|
50
|
-
# format with trusted input.
|
47
|
+
# @note Use of # {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load Marshal.load} is generally
|
48
|
+
# discouraged. Only use the +.marshal+ format with trusted input.
|
51
49
|
def load filepath, serializer = nil
|
52
50
|
serializer ||= serializers.resolve filepath
|
53
51
|
root = serializer.load filepath
|
@@ -58,23 +56,22 @@ module Rambling
|
|
58
56
|
|
59
57
|
# Dumps an existing trie from memory into disk. By default, it will
|
60
58
|
# deduce the correct way to serialize based on the file extension.
|
61
|
-
# Available formats are
|
59
|
+
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
|
62
60
|
# previous formats. You can also define your own.
|
63
61
|
# @param [Container] trie the trie to dump into disk.
|
64
62
|
# @param [String] filepath the file to dump to serialized trie into.
|
65
|
-
# @param [Serializer, nil] serializer the object responsible
|
66
|
-
#
|
67
|
-
# @see
|
63
|
+
# @param [Serializers::Serializer, nil] serializer the object responsible for trie serialization.
|
64
|
+
# @return [void]
|
65
|
+
# @see Serializers Serializers.
|
68
66
|
def dump trie, filepath, serializer = nil
|
69
67
|
serializer ||= serializers.resolve filepath
|
68
|
+
# noinspection RubyNilAnalysis
|
70
69
|
serializer.dump trie.root, filepath
|
71
70
|
end
|
72
71
|
|
73
|
-
# Provides configuration properties for the Rambling::Trie gem.
|
74
|
-
# @return [Configuration::Properties] the configured properties of the
|
75
|
-
#
|
76
|
-
# @yield [Configuration::Properties] the configured properties of the
|
77
|
-
# gem.
|
72
|
+
# Provides configuration properties for the +Rambling::Trie+ gem.
|
73
|
+
# @return [Configuration::Properties] the configured properties of the gem.
|
74
|
+
# @yield [Configuration::Properties] the configured properties of the gem.
|
78
75
|
def config
|
79
76
|
yield properties if block_given?
|
80
77
|
properties
|
data/rambling-trie.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
$LOAD_PATH.push File.expand_path('
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
4
4
|
require 'rambling/trie/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.authors = ['Edgar Gonzalez', 'Lilibeth De La Cruz']
|
8
|
-
gem.email =
|
8
|
+
gem.email = %w(edggonzalezg@gmail.com lilibethdlc@gmail.com)
|
9
9
|
|
10
10
|
gem.description = <<~DESCRIPTION.gsub(%r{\s+}, ' ')
|
11
11
|
The Rambling Trie is a Ruby implementation of the trie data structure, which
|
@@ -13,8 +13,12 @@ Gem::Specification.new do |gem|
|
|
13
13
|
DESCRIPTION
|
14
14
|
|
15
15
|
gem.summary = 'A Ruby implementation of the trie data structure.'
|
16
|
-
gem.homepage = '
|
16
|
+
gem.homepage = 'https://github.com/gonzedge/rambling-trie'
|
17
17
|
gem.date = Time.now.strftime '%Y-%m-%d'
|
18
|
+
gem.metadata = {
|
19
|
+
'changelog_uri' => 'https://github.com/gonzedge/rambling-trie/blob/master/CHANGELOG.md',
|
20
|
+
'documentation_uri' => 'https://www.rubydoc.info/gems/rambling-trie',
|
21
|
+
}
|
18
22
|
|
19
23
|
executables = `git ls-files -- bin/*`.split "\n"
|
20
24
|
files = `git ls-files -- {lib,*file,*.gemspec,LICENSE*,README*}`.split "\n"
|
@@ -31,7 +35,7 @@ Gem::Specification.new do |gem|
|
|
31
35
|
gem.platform = Gem::Platform::RUBY
|
32
36
|
gem.required_ruby_version = '>= 2.7', '< 4'
|
33
37
|
|
34
|
-
gem.add_development_dependency 'rake', '~> 13.
|
38
|
+
gem.add_development_dependency 'rake', '~> 13.1'
|
35
39
|
gem.add_development_dependency 'rspec', '~> 3.12'
|
36
|
-
gem.add_development_dependency 'yard', '~> 0.9.
|
40
|
+
gem.add_development_dependency 'yard', '~> 0.9.34'
|
37
41
|
end
|