rambling-trie-opal 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +26 -0
- data/Guardfile +10 -0
- data/LICENSE +26 -0
- data/README.md +301 -0
- data/Rakefile +15 -0
- data/lib/rambling-trie.rb +3 -0
- data/lib/rambling/trie.rb +119 -0
- data/lib/rambling/trie/comparable.rb +19 -0
- data/lib/rambling/trie/compressible.rb +16 -0
- data/lib/rambling/trie/compressor.rb +64 -0
- data/lib/rambling/trie/configuration.rb +16 -0
- data/lib/rambling/trie/configuration/properties.rb +75 -0
- data/lib/rambling/trie/configuration/provider_collection.rb +122 -0
- data/lib/rambling/trie/container.rb +226 -0
- data/lib/rambling/trie/enumerable.rb +29 -0
- data/lib/rambling/trie/inspectable.rb +39 -0
- data/lib/rambling/trie/invalid_operation.rb +15 -0
- data/lib/rambling/trie/nodes.rb +18 -0
- data/lib/rambling/trie/nodes/compressed.rb +98 -0
- data/lib/rambling/trie/nodes/missing.rb +12 -0
- data/lib/rambling/trie/nodes/node.rb +183 -0
- data/lib/rambling/trie/nodes/raw.rb +82 -0
- data/lib/rambling/trie/readers.rb +15 -0
- data/lib/rambling/trie/readers/plain_text.rb +18 -0
- data/lib/rambling/trie/serializers.rb +18 -0
- data/lib/rambling/trie/serializers/file.rb +27 -0
- data/lib/rambling/trie/serializers/marshal.rb +48 -0
- data/lib/rambling/trie/serializers/yaml.rb +55 -0
- data/lib/rambling/trie/serializers/zip.rb +74 -0
- data/lib/rambling/trie/stringifyable.rb +26 -0
- data/lib/rambling/trie/version.rb +8 -0
- data/rambling-trie-opal.gemspec +36 -0
- data/spec/assets/test_words.en_US.txt +23 -0
- data/spec/assets/test_words.es_DO.txt +24 -0
- data/spec/integration/rambling/trie_spec.rb +87 -0
- data/spec/lib/rambling/trie/comparable_spec.rb +97 -0
- data/spec/lib/rambling/trie/compressor_spec.rb +108 -0
- data/spec/lib/rambling/trie/configuration/properties_spec.rb +57 -0
- data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +149 -0
- data/spec/lib/rambling/trie/container_spec.rb +591 -0
- data/spec/lib/rambling/trie/enumerable_spec.rb +42 -0
- data/spec/lib/rambling/trie/inspectable_spec.rb +56 -0
- data/spec/lib/rambling/trie/nodes/compressed_spec.rb +37 -0
- data/spec/lib/rambling/trie/nodes/node_spec.rb +9 -0
- data/spec/lib/rambling/trie/nodes/raw_spec.rb +179 -0
- data/spec/lib/rambling/trie/readers/plain_text_spec.rb +16 -0
- data/spec/lib/rambling/trie/serializers/file_spec.rb +13 -0
- data/spec/lib/rambling/trie/serializers/marshal_spec.rb +12 -0
- data/spec/lib/rambling/trie/serializers/yaml_spec.rb +12 -0
- data/spec/lib/rambling/trie/serializers/zip_spec.rb +28 -0
- data/spec/lib/rambling/trie/stringifyable_spec.rb +85 -0
- data/spec/lib/rambling/trie_spec.rb +182 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/support/config.rb +15 -0
- data/spec/support/helpers/add_word.rb +20 -0
- data/spec/support/helpers/one_line_heredoc.rb +11 -0
- data/spec/support/shared_examples/a_compressible_trie.rb +40 -0
- data/spec/support/shared_examples/a_serializable_trie.rb +30 -0
- data/spec/support/shared_examples/a_serializer.rb +37 -0
- data/spec/support/shared_examples/a_trie_data_structure.rb +31 -0
- data/spec/support/shared_examples/a_trie_node.rb +127 -0
- data/spec/support/shared_examples/a_trie_node_implementation.rb +152 -0
- data/spec/tmp/.gitkeep +0 -0
- metadata +179 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 24fddca452c0fe9c0738992d1e04fb2586e4b39a44b64f28cac72f18ae067f2f
|
4
|
+
data.tar.gz: 72b6d08b8a028c934d7c28f5a4c3a5f4f3bf56eb650b3900caf0a4e30b183379
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84a491cab68d7d407ae8ab7609d1fddfcf67775d8fcdcc967df1fab0ba9d4b8a0ae3d23d7c39ee72f5c469fd1d447493006e0bd65e9f25fb833edac9999d8d25
|
7
|
+
data.tar.gz: 22624b059608407a4066645c4917d82820f5c399ec09063503acc69c28afbc8fd2c495f5b7c2f9a508b7f526e56d5729a2e186b992a99fbc319cec85d0c854ba
|
data/Gemfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'rubyzip'
|
8
|
+
|
9
|
+
group :development do
|
10
|
+
gem 'benchmark-ips'
|
11
|
+
gem 'flamegraph'
|
12
|
+
gem 'memory_profiler'
|
13
|
+
gem 'pry'
|
14
|
+
gem 'ruby-prof'
|
15
|
+
gem 'stackprof'
|
16
|
+
end
|
17
|
+
|
18
|
+
group :test do
|
19
|
+
gem 'coveralls', '~>0.8.21', require: false
|
20
|
+
gem 'simplecov', require: false
|
21
|
+
end
|
22
|
+
|
23
|
+
group :local do
|
24
|
+
gem 'guard-rspec'
|
25
|
+
gem 'rubocop', require: false
|
26
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A sample Guardfile
|
4
|
+
# More info at https://github.com/guard/guard#readme
|
5
|
+
|
6
|
+
guard 'rspec', cmd: 'rspec', all_on_start: true, all_after_pass: false do
|
7
|
+
watch(%r{^spec/.+_spec\.rb$})
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
9
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
10
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2012-2017 Edgar Gonzalez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person
|
6
|
+
obtaining a copy of this software and associated documentation
|
7
|
+
files (the "Software"), to deal in the Software without
|
8
|
+
restriction, including without limitation the rights to use,
|
9
|
+
copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
+
sell copies of the Software, and to permit persons to whom
|
11
|
+
the Software is furnished to do so, subject to the following
|
12
|
+
conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall
|
15
|
+
be included in all copies or substantial portions of the
|
16
|
+
Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
19
|
+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
20
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
21
|
+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
24
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
25
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
26
|
+
|
data/README.md
ADDED
@@ -0,0 +1,301 @@
|
|
1
|
+
# Rambling Trie
|
2
|
+
|
3
|
+
[![Gem Version][badge_fury_badge]][badge_fury_link] [![Build Status][travis_ci_badge]][travis_ci_link] [![Code Climate][code_climate_badge]][code_climage_link] [![Coverage Status][coveralls_badge]][coveralls_link] [![Documentation Status][inch_ci_badge]][inch_ci_link] [![License][license_badge]][license_link]
|
4
|
+
|
5
|
+
The Rambling Trie is a Ruby implementation of the [trie data structure][trie_wiki], which includes compression abilities and is designed to be very fast to traverse.
|
6
|
+
|
7
|
+
## Installing the Rambling Trie
|
8
|
+
|
9
|
+
### Requirements
|
10
|
+
|
11
|
+
You will need:
|
12
|
+
|
13
|
+
* Ruby 2.5.0 or up
|
14
|
+
* RubyGems
|
15
|
+
|
16
|
+
See [RVM][rvm], [rbenv][rbenv] or [chruby][chruby] for more information on how to manage Ruby versions.
|
17
|
+
|
18
|
+
### Installation
|
19
|
+
|
20
|
+
You can either install it manually with:
|
21
|
+
|
22
|
+
``` bash
|
23
|
+
gem install rambling-trie
|
24
|
+
```
|
25
|
+
|
26
|
+
Or, include it in your `Gemfile` and bundle it:
|
27
|
+
|
28
|
+
``` ruby
|
29
|
+
gem 'rambling-trie'
|
30
|
+
```
|
31
|
+
|
32
|
+
## Using the Rambling Trie
|
33
|
+
|
34
|
+
### Creation
|
35
|
+
|
36
|
+
To create a new trie, initialize it like this:
|
37
|
+
|
38
|
+
``` ruby
|
39
|
+
trie = Rambling::Trie.create
|
40
|
+
```
|
41
|
+
|
42
|
+
You can also provide a block and the created trie instance will be yielded for you to perform any operation on it:
|
43
|
+
|
44
|
+
``` ruby
|
45
|
+
Rambling::Trie.create do |trie|
|
46
|
+
trie << 'word'
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Additionally, you can provide the path to a file that contains all the words to be added to the trie, and it will read the file and create the complete structure for you, like this:
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
trie = Rambling::Trie.create '/path/to/file'
|
54
|
+
```
|
55
|
+
|
56
|
+
By default, a plain text file with the following format will be expected:
|
57
|
+
|
58
|
+
``` text
|
59
|
+
some
|
60
|
+
words
|
61
|
+
to
|
62
|
+
populate
|
63
|
+
the
|
64
|
+
trie
|
65
|
+
```
|
66
|
+
|
67
|
+
If you want to use a custom file format, you will need to provide a custom file reader that defines an `#each_word` method that yields each word contained in the file. Look at the [`PlainText` reader][rambling_trie_plain_text_reader] class for an example, and at the [Configuration section][rambling_trie_configuration] to see how to add your own custom file readers.
|
68
|
+
|
69
|
+
### Operations
|
70
|
+
|
71
|
+
To add new words to the trie, use `#add` or its alias `#<<`:
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
trie.add 'word'
|
75
|
+
trie << 'word'
|
76
|
+
```
|
77
|
+
|
78
|
+
Or if you have multiple words to add, you can use `#concat`:
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
trie.concat %w(a collection of words)
|
82
|
+
```
|
83
|
+
|
84
|
+
And to find out if a word already exists in the trie, use `#word?` or its alias `#include?`:
|
85
|
+
|
86
|
+
``` ruby
|
87
|
+
trie.word? 'word'
|
88
|
+
trie.include? 'word'
|
89
|
+
```
|
90
|
+
|
91
|
+
If you wish to find if part of a word exists in the trie instance, you should call `#partial_word?` or its alias `#match?`:
|
92
|
+
|
93
|
+
``` ruby
|
94
|
+
trie.partial_word? 'partial_word'
|
95
|
+
trie.match? 'partial_word'
|
96
|
+
```
|
97
|
+
|
98
|
+
To get all the words that start with a particular string, you can use `#scan` or its alias `#words`:
|
99
|
+
|
100
|
+
``` ruby
|
101
|
+
trie.scan 'hi' # => ['hi', 'high', 'highlight', ...]
|
102
|
+
trie.words 'hi' # => ['hi', 'high', 'highlight', ...]
|
103
|
+
```
|
104
|
+
|
105
|
+
To get all the words within a given string, you can use `#words_within`:
|
106
|
+
|
107
|
+
``` ruby
|
108
|
+
trie.words_within 'ifdxawesome45someword3' # => ['if', 'aw', 'awe', ...]
|
109
|
+
trie.words_within 'tktktktk' # => []
|
110
|
+
```
|
111
|
+
|
112
|
+
Or, if you're just interested in knowing whether a given string contains any valid words or not, you can use `#words_within?`:
|
113
|
+
|
114
|
+
``` ruby
|
115
|
+
trie.words_within? 'ifdxawesome45someword3' # => true
|
116
|
+
trie.words_within? 'tktktktk' # => false
|
117
|
+
```
|
118
|
+
|
119
|
+
### Compression
|
120
|
+
|
121
|
+
By default, the Rambling Trie works as a standard trie. Starting from version 0.1.0, you can obtain a compressed trie from the standard one, by using the compression feature. Just call the `#compress!` method on the trie instance:
|
122
|
+
|
123
|
+
``` ruby
|
124
|
+
trie.compress!
|
125
|
+
```
|
126
|
+
|
127
|
+
This will reduce the size of the trie by using redundant node elimination (redundant nodes are the only-child non-terminal nodes).
|
128
|
+
|
129
|
+
> _**Note**: The `#compress!` method acts over the trie instance it belongs to
|
130
|
+
> and replaces the root `Node`. Also, adding words after compression (with `#add` or
|
131
|
+
> `#<<`) is not supported._
|
132
|
+
|
133
|
+
If you want, you can also create a new compressed trie and leave the existing one intact. Just use `#compress` instead:
|
134
|
+
|
135
|
+
``` ruby
|
136
|
+
compressed_trie = trie.compress
|
137
|
+
```
|
138
|
+
|
139
|
+
You can find out if a trie instance is compressed by calling the `#compressed?` method. From the `#compress` example:
|
140
|
+
|
141
|
+
``` ruby
|
142
|
+
trie.compressed? # => false
|
143
|
+
compressed_trie.compressed? # => true
|
144
|
+
```
|
145
|
+
|
146
|
+
### Enumeration
|
147
|
+
|
148
|
+
Starting from version 0.4.2, you can use any `Enumerable` method over a trie instance, and it will iterate over each word contained in the trie. You can now do things like:
|
149
|
+
|
150
|
+
``` ruby
|
151
|
+
trie.each { |word| puts word }
|
152
|
+
trie.any? { |word| word.include? 'x' }
|
153
|
+
trie.all? { |word| word.include? 'x' }
|
154
|
+
# etc.
|
155
|
+
```
|
156
|
+
|
157
|
+
### Serialization
|
158
|
+
|
159
|
+
Starting from version 1.0.0, you can store a full trie instance on disk and retrieve/use it later on. Loading a trie from disk takes less time, less cpu and less memory than loading every word into the trie every time. This is particularly useful for production applications, when you have word lists that you know are going to be static, or that change with little frequency.
|
160
|
+
|
161
|
+
To store a trie on disk, you can use `.dump` like this:
|
162
|
+
|
163
|
+
``` ruby
|
164
|
+
Rambling::Trie.dump trie, '/path/to/file'
|
165
|
+
```
|
166
|
+
|
167
|
+
Then, when you need to use a trie next time, you don't have to create a new one with all the necessary words. Rather, you can retrieve a previously stored one with `.load` like this:
|
168
|
+
|
169
|
+
``` ruby
|
170
|
+
trie = Rambling::Trie.load trie, '/path/to/file'
|
171
|
+
```
|
172
|
+
|
173
|
+
#### Supported formats
|
174
|
+
|
175
|
+
Currently, these formats are supported to store tries on disk:
|
176
|
+
|
177
|
+
- Ruby's [binary (Marshal)][marshal] format
|
178
|
+
- [YAML][yaml]
|
179
|
+
|
180
|
+
> When dumping into or loading from disk, the format is determined
|
181
|
+
> automatically based on the file extension, so `.yml` or `.yaml` files will be
|
182
|
+
> handled through `YAML` and `.marshal` files through `Marshal`.
|
183
|
+
|
184
|
+
Optionally, you can use a `.zip` version of the supported formats. In order to do so, you'll have to install the [`rubyzip`][rubyzip] gem:
|
185
|
+
|
186
|
+
``` bash
|
187
|
+
gem install rubyzip
|
188
|
+
```
|
189
|
+
|
190
|
+
Or, include it in your `Gemfile` and bundle it:
|
191
|
+
|
192
|
+
``` ruby
|
193
|
+
gem 'rubyzip'
|
194
|
+
```
|
195
|
+
|
196
|
+
Then, you can load contents form a `.zip` file like this:
|
197
|
+
|
198
|
+
``` ruby
|
199
|
+
require 'zip'
|
200
|
+
trie = Rambling::Trie.load trie, '/path/to/file.zip'
|
201
|
+
```
|
202
|
+
|
203
|
+
> For `.zip` files, the format is also determined automatically based on the
|
204
|
+
> file extension, so `.yml.zip` or `.yaml.zip` files will be handled through
|
205
|
+
> `YAML` after decompression and `.marshal.zip` files through `Marshal`.
|
206
|
+
|
207
|
+
### Configuration
|
208
|
+
|
209
|
+
Starting from version 1.0.0, you can change the configuration values used by Rambling Trie. You can now supply:
|
210
|
+
|
211
|
+
* A `Compressor` object
|
212
|
+
* A root `Node` builder
|
213
|
+
* More `Readers` (implement `#each_word`)
|
214
|
+
* Change the default `reader`
|
215
|
+
* More `Serializers` (implement `#dump` and `#load`)
|
216
|
+
* Change the default `serializer`
|
217
|
+
|
218
|
+
You can configure those values by using `.config` like this:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
require 'rambling-trie'
|
222
|
+
|
223
|
+
Rambling::Trie.config do |config|
|
224
|
+
config.compressor = MyCompressor.new
|
225
|
+
config.root_builder = lambda { MyNode.new }
|
226
|
+
|
227
|
+
config.readers.add :html, MyHtmlReader.new
|
228
|
+
config.readers.default = config.readers[:html]
|
229
|
+
|
230
|
+
config.serializers.add :json, MyJsonSerializer.new
|
231
|
+
config.serializers.default = config.serializers[:yml]
|
232
|
+
end
|
233
|
+
|
234
|
+
# Create a trie or load one from disk and do things with it...
|
235
|
+
```
|
236
|
+
|
237
|
+
### Further Documentation
|
238
|
+
|
239
|
+
You can find further API documentation on the autogenerated [rambling-trie gem RubyDoc.info page][rubydoc] or if you want edge documentation, you can go the [GitHub project RubyDoc.info page][rubydoc_github].
|
240
|
+
|
241
|
+
## Compatible Ruby and Rails versions
|
242
|
+
|
243
|
+
The Rambling Trie has been tested with the following Ruby versions:
|
244
|
+
|
245
|
+
* 2.7.x
|
246
|
+
* 2.6.x
|
247
|
+
* 2.5.x
|
248
|
+
|
249
|
+
**No longer supported**:
|
250
|
+
|
251
|
+
* 2.4.x (EOL'ed)
|
252
|
+
* 2.3.x (EOL'ed)
|
253
|
+
* 2.2.x
|
254
|
+
* 2.1.x
|
255
|
+
* 2.0.x
|
256
|
+
* 1.9.x
|
257
|
+
* 1.8.x
|
258
|
+
|
259
|
+
## Contributing to Rambling Trie
|
260
|
+
|
261
|
+
Take a look at the [contributing guide][rambling_trie_contributing_guide] to get started, or fire a question to [@gonzedge][github_user_gonzedge].
|
262
|
+
|
263
|
+
## License and copyright
|
264
|
+
|
265
|
+
Copyright (c) 2012-2017 Edgar Gonzalez
|
266
|
+
|
267
|
+
MIT License
|
268
|
+
|
269
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
270
|
+
|
271
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
272
|
+
|
273
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
274
|
+
|
275
|
+
[badge_fury_badge]: https://badge.fury.io/rb/rambling-trie.svg
|
276
|
+
[badge_fury_link]: https://badge.fury.io/rb/rambling-trie
|
277
|
+
[chruby]: https://github.com/postmodern/chruby
|
278
|
+
[code_climage_link]: https://codeclimate.com/github/gonzedge/rambling-trie
|
279
|
+
[code_climate_badge]: https://codeclimate.com/github/gonzedge/rambling-trie/badges/gpa.svg
|
280
|
+
[coveralls_badge]: https://img.shields.io/coveralls/gonzedge/rambling-trie.svg
|
281
|
+
[coveralls_link]: https://coveralls.io/r/gonzedge/rambling-trie
|
282
|
+
[gemnasium_badge]: https://gemnasium.com/gonzedge/rambling-trie.svg
|
283
|
+
[gemnasium_link]: https://gemnasium.com/gonzedge/rambling-trie
|
284
|
+
[github_user_gonzedge]: https://github.com/gonzedge
|
285
|
+
[inch_ci_badge]: https://inch-ci.org/github/gonzedge/rambling-trie.svg?branch=master
|
286
|
+
[inch_ci_link]: https://inch-ci.org/github/gonzedge/rambling-trie
|
287
|
+
[license_badge]: https://badges.frapsoft.com/os/mit/mit.svg?v=103
|
288
|
+
[license_link]: https://opensource.org/licenses/mit-license.php
|
289
|
+
[marshal]: https://ruby-doc.org/core-2.5.0/Marshal.html
|
290
|
+
[rambling_trie_configuration]: https://github.com/gonzedge/rambling-trie#configuration
|
291
|
+
[rambling_trie_contributing_guide]: https://github.com/gonzedge/rambling-trie/blob/master/CONTRIBUTING.md
|
292
|
+
[rambling_trie_plain_text_reader]: https://github.com/gonzedge/rambling-trie/blob/master/lib/rambling/trie/readers/plain_text.rb
|
293
|
+
[rbenv]: https://github.com/sstephenson/rbenv
|
294
|
+
[rubydoc]: http://rubydoc.info/gems/rambling-trie
|
295
|
+
[rubydoc_github]: http://rubydoc.info/github/gonzedge/rambling-trie
|
296
|
+
[rubyzip]: https://github.com/rubyzip/rubyzip
|
297
|
+
[rvm]: https://rvm.io
|
298
|
+
[travis_ci_badge]: https://travis-ci.org/gonzedge/rambling-trie.svg
|
299
|
+
[travis_ci_link]: https://travis-ci.org/gonzedge/rambling-trie
|
300
|
+
[trie_wiki]: https://en.wikipedia.org/wiki/Trie
|
301
|
+
[yaml]: https://ruby-doc.org/stdlib-2.5.0/libdoc/yaml/rdoc/YAML.html
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
|
7
|
+
require 'rambling-trie'
|
8
|
+
require_relative 'tasks/performance'
|
9
|
+
require_relative 'tasks/serialization'
|
10
|
+
require_relative 'tasks/ips'
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new :spec
|
13
|
+
RuboCop::RakeTask.new :rubocop
|
14
|
+
|
15
|
+
task default: :spec
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# %w(
|
4
|
+
# comparable compressible compressor configuration container enumerable
|
5
|
+
# inspectable invalid_operation readers serializers stringifyable nodes
|
6
|
+
# version
|
7
|
+
# ).each do |file|
|
8
|
+
# require File.join('rambling', 'trie', file)
|
9
|
+
# end
|
10
|
+
require 'rambling/trie/configuration'
|
11
|
+
require 'rambling/trie/comparable'
|
12
|
+
require 'rambling/trie/compressible'
|
13
|
+
require 'rambling/trie/compressor'
|
14
|
+
require 'rambling/trie/container'
|
15
|
+
require 'rambling/trie/enumerable'
|
16
|
+
require 'rambling/trie/inspectable'
|
17
|
+
require 'rambling/trie/invalid_operation'
|
18
|
+
require 'rambling/trie/readers'
|
19
|
+
require 'rambling/trie/serializers'
|
20
|
+
require 'rambling/trie/stringifyable'
|
21
|
+
require 'rambling/trie/nodes'
|
22
|
+
require 'rambling/trie/version'
|
23
|
+
|
24
|
+
# General namespace for all Rambling gems.
|
25
|
+
module Rambling
|
26
|
+
# Entry point for rambling-trie API.
|
27
|
+
module Trie
|
28
|
+
class << self
|
29
|
+
# Creates a new Rambling::Trie. Entry point for the Rambling::Trie API.
|
30
|
+
# @param [String, nil] filepath the file to load the words from.
|
31
|
+
# @param [Reader, nil] reader the file parser to get each word.
|
32
|
+
# @return [Container] the trie just created.
|
33
|
+
# @yield [Container] the trie just created.
|
34
|
+
# @see Rambling::Trie::Readers Readers.
|
35
|
+
def create filepath = nil, reader = nil
|
36
|
+
root = root_builder.call
|
37
|
+
|
38
|
+
Rambling::Trie::Container.new root, compressor do |container|
|
39
|
+
if filepath
|
40
|
+
reader ||= readers.resolve filepath
|
41
|
+
reader.each_word filepath do |word|
|
42
|
+
container << word
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
yield container if block_given?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Loads an existing trie from disk into memory. By default, it will
|
51
|
+
# deduce the correct way to deserialize based on the file extension.
|
52
|
+
# Available formats are `yml`, `marshal`, and `zip` versions of all the
|
53
|
+
# previous formats. You can also define your own.
|
54
|
+
# @param [String] filepath the file to load the words from.
|
55
|
+
# @param [Serializer, nil] serializer the object responsible of loading
|
56
|
+
# the trie from disk
|
57
|
+
# @return [Container] the trie just loaded.
|
58
|
+
# @yield [Container] the trie just loaded.
|
59
|
+
# @see Rambling::Trie::Serializers Serializers.
|
60
|
+
# @note Use of
|
61
|
+
# {https://ruby-doc.org/core-2.5.0/Marshal.html#method-c-load
|
62
|
+
# Marshal.load} is generally discouraged. Only use the `.marshal`
|
63
|
+
# format with trusted input.
|
64
|
+
def load filepath, serializer = nil
|
65
|
+
serializer ||= serializers.resolve filepath
|
66
|
+
root = serializer.load filepath
|
67
|
+
Rambling::Trie::Container.new root, compressor do |container|
|
68
|
+
yield container if block_given?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Dumps an existing trie from memory into disk. By default, it will
|
73
|
+
# deduce the correct way to serialize based on the file extension.
|
74
|
+
# Available formats are `yml`, `marshal`, and `zip` versions of all the
|
75
|
+
# previous formats. You can also define your own.
|
76
|
+
# @param [Container] trie the trie to dump into disk.
|
77
|
+
# @param [String] filepath the file to dump to serialized trie into.
|
78
|
+
# @param [Serializer, nil] serializer the object responsible of
|
79
|
+
# serializing and dumping the trie into disk.
|
80
|
+
# @see Rambling::Trie::Serializers Serializers.
|
81
|
+
def dump trie, filepath, serializer = nil
|
82
|
+
serializer ||= serializers.resolve filepath
|
83
|
+
serializer.dump trie.root, filepath
|
84
|
+
end
|
85
|
+
|
86
|
+
# Provides configuration properties for the Rambling::Trie gem.
|
87
|
+
# @return [Configuration::Properties] the configured properties of the
|
88
|
+
# gem.
|
89
|
+
# @yield [Configuration::Properties] the configured properties of the
|
90
|
+
# gem.
|
91
|
+
def config
|
92
|
+
yield properties if block_given?
|
93
|
+
properties
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def properties
|
99
|
+
@properties ||= Rambling::Trie::Configuration::Properties.new
|
100
|
+
end
|
101
|
+
|
102
|
+
def readers
|
103
|
+
properties.readers
|
104
|
+
end
|
105
|
+
|
106
|
+
def serializers
|
107
|
+
properties.serializers
|
108
|
+
end
|
109
|
+
|
110
|
+
def compressor
|
111
|
+
properties.compressor
|
112
|
+
end
|
113
|
+
|
114
|
+
def root_builder
|
115
|
+
properties.root_builder
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|