rambling-trie 0.7.0 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6379fbb202c3257cad63367006a150b801b69649
4
- data.tar.gz: 9b4b9f07ec20f5e7a2aef641d06501a03cc20195
3
+ metadata.gz: 4d281908146ad7997bb01e3765836bb2053d7251
4
+ data.tar.gz: b3811b63c5d068e49e207849ca90083fe4ca95a1
5
5
  SHA512:
6
- metadata.gz: 043f1af6cd0b8390585e44b73e63101810f5506198e0ee5adbbe8708b394c8a0bc3db5ce3bdfae4e5fbcb9c1b6692cebf25f5412df2d74926352119690aed573
7
- data.tar.gz: 7de76f2ad5e324e1a500bd2f33a80ade73a9c7a1488062ce4ea01fa982dad33955dd724c469ad1f7eeb4c49e69c1e91fc46e62270eaa65b44db5cb288ccc4362
6
+ metadata.gz: a28530aa9cd4c0d3276c351e6f275e4c24566a9b1f7b87a35377f55287fd35affb5c25b12daf3642aa6e621876be44987df4322f428dc784017bbb621bbb761f
7
+ data.tar.gz: 2c17da1286a41d083f2bb2a38a4152b682097f980f426e06fa83181707725b5963acbbc214e32d0f7584635ff195321c0ceffe6e9a3586211c4019070f11256e
data/.travis.yml CHANGED
@@ -1,10 +1,21 @@
1
1
  language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ install:
5
+ - bundle install --without test
2
6
  rvm:
7
+ - 2.3.0
8
+ - 2.2.4
9
+ - 2.2.3
10
+ - 2.2.2
11
+ - 2.2.1
12
+ - 2.2.0
13
+ - 2.1.8
14
+ - 2.1.7
15
+ - 2.1.6
3
16
  - 2.1.5
4
17
  - 2.1.4
5
18
  - 2.1.3
6
19
  - 2.1.2
7
20
  - 2.1.1
8
21
  - 2.1.0
9
- - 2.0.0
10
- - 1.9.3
data/Gemfile CHANGED
@@ -4,10 +4,10 @@ gemspec
4
4
 
5
5
  gem 'coveralls', require: false
6
6
 
7
+ group :development do
8
+ gem 'simplecov', require: false
9
+ end
10
+
7
11
  group :test do
8
- gem 'rake'
9
12
  gem 'guard-rspec'
10
- gem 'rb-fsevent'
11
- gem 'rb-inotify'
12
- gem 'simplecov', '~> 0.9.1', require: false
13
13
  end
data/Guardfile CHANGED
@@ -1,9 +1,8 @@
1
1
  # A sample Guardfile
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
- guard 'rspec', all_on_start: true, all_after_pass: false do
4
+ guard 'rspec', cmd: 'rspec', all_on_start: true, all_after_pass: false do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
6
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
7
  watch('spec/spec_helper.rb') { "spec" }
8
8
  end
9
-
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-2014 Edgar Gonzalez
1
+ Copyright (c) 2012-2016 Edgar Gonzalez
2
2
 
3
3
  MIT License
4
4
 
@@ -1,6 +1,6 @@
1
1
  # Rambling Trie
2
2
 
3
- [![Gem Version][badge_fury_badge]][badge_fury_link] [![Dependency Status][gemnasium_badge]][gemnasium_link] [![Build Status][travis_ci_badge]][travis_ci_link] [![Code Climate][code_climate_badge]][code_climage_link] [![Coverage Status][coveralls_badge]][coveralls_link]
3
+ [![Gem Version][badge_fury_badge]][badge_fury_link] [![Dependency Status][gemnasium_badge]][gemnasium_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]
4
4
 
5
5
  The Rambling Trie is a custom implementation of the Trie data structure with Ruby, which includes compression abilities and is designed to be very fast to traverse.
6
6
 
@@ -10,7 +10,7 @@ The Rambling Trie is a custom implementation of the Trie data structure with Rub
10
10
 
11
11
  You will need:
12
12
 
13
- * Ruby 1.9.3 or up
13
+ * Ruby 2.1.0 or up
14
14
  * RubyGems
15
15
 
16
16
  See [RVM][rvm] or [rbenv][rbenv] for more information on how to manage Ruby versions.
@@ -37,14 +37,6 @@ To create the trie, initialize it like this:
37
37
  trie = Rambling::Trie.create
38
38
  ```
39
39
 
40
- - - -
41
-
42
- #### Deprecation warnings
43
-
44
- * Starting from version 0.4.0, `Rambling::Trie.new` is deprecated. Please use `Rambling::Trie.create` instead.
45
-
46
- - - -
47
-
48
40
  You can also provide a block and the created instance will be yielded for you to perform any operation on it:
49
41
 
50
42
  ``` ruby
@@ -72,23 +64,6 @@ trie
72
64
 
73
65
  If you want to use a custom file format, you will need to provide a custom file reader that defines the `each_word` method that yields each word contained in the file. Look at the `Rambling::Trie::PlainTextReader` class for an example.
74
66
 
75
- - - -
76
-
77
- #### Breaking changes
78
-
79
- * Starting from version 0.6.0, the `children` method returns an array of nodes instead of a hash. If you still need access to the underlying hash, use `children_tree` instead.
80
-
81
- - - -
82
-
83
- - - -
84
-
85
- #### Deprecation warnings
86
-
87
- * Starting from version 0.6.0, the `branch?` method is deprecated. The `partial_word?` method should be used instead.
88
- * Starting from version 0.5.0, the `has_branch_for?`, `is_word?` and `add_branch_from` methods are deprecated. The methods `branch?`, `word?` and `add` should be used respectively.
89
-
90
- - - -
91
-
92
67
  To add new words to the trie, use `add` or `<<`:
93
68
 
94
69
  ``` ruby
@@ -110,6 +85,13 @@ trie.partial_word? 'partial_word'
110
85
  trie.match? 'partial_word'
111
86
  ```
112
87
 
88
+ To get all the words that start with a particular string, you can use `scan`:
89
+
90
+ ``` ruby
91
+ trie.scan 'hi' # => ['hi', 'high', 'highlight', ...]
92
+ trie.words 'hi' # => ['hi', 'high', 'highlight', ...]
93
+ ```
94
+
113
95
  ### Compression
114
96
 
115
97
  By default, the Rambling Trie works as a Standard Trie.
@@ -152,11 +134,11 @@ You can find further API documentation on the autogenerated [rambling-trie gem R
152
134
 
153
135
  The Rambling Trie has been tested with the following Ruby versions:
154
136
 
137
+ * 2.3.x
138
+ * 2.2.x
155
139
  * 2.1.x
156
- * 2.0.0
157
- * 1.9.3
158
140
 
159
- Ruby 1.8.7 and 1.9.2 are not supported.
141
+ Ruby 1.8.x, 1.9.x and 2.0.x are not supported.
160
142
 
161
143
  ## Contributing to Rambling Trie
162
144
 
@@ -165,7 +147,7 @@ Also, be sure to add tests for any feature you may develop or bug you may fix.
165
147
 
166
148
  ## License and copyright
167
149
 
168
- Copyright (c) 2012-2014 Edgar Gonzalez
150
+ Copyright (c) 2012-2016 Edgar Gonzalez
169
151
 
170
152
  MIT License
171
153
 
@@ -177,15 +159,17 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
177
159
 
178
160
  [badge_fury_badge]: https://badge.fury.io/rb/rambling-trie.svg
179
161
  [badge_fury_link]: https://badge.fury.io/rb/rambling-trie
180
- [code_climate_badge]: https://codeclimate.com/github/gonzedge/rambling-trie.png
181
162
  [code_climage_link]: https://codeclimate.com/github/gonzedge/rambling-trie
182
- [coveralls_badge]: https://coveralls.io/repos/gonzedge/rambling-trie/badge.png
163
+ [code_climate_badge]: https://codeclimate.com/github/gonzedge/rambling-trie/badges/gpa.svg
164
+ [coveralls_badge]: https://img.shields.io/coveralls/gonzedge/rambling-trie.svg
183
165
  [coveralls_link]: https://coveralls.io/r/gonzedge/rambling-trie
184
166
  [gemnasium_badge]: https://gemnasium.com/gonzedge/rambling-trie.svg
185
167
  [gemnasium_link]: https://gemnasium.com/gonzedge/rambling-trie
186
- [travis_ci_badge]: https://secure.travis-ci.org/gonzedge/rambling-trie.svg
187
- [travis_ci_link]: https://travis-ci.org/gonzedge/rambling-trie
188
- [rvm]: https://rvm.io
168
+ [inch_ci_badge]: https://inch-ci.org/github/gonzedge/rambling-trie.svg?branch=master
169
+ [inch_ci_link]: https://inch-ci.org/github/gonzedge/rambling-trie
189
170
  [rbenv]: https://github.com/sstephenson/rbenv
190
171
  [rubydoc]: http://rubydoc.info/gems/rambling-trie
191
172
  [rubydoc_github]: http://rubydoc.info/github/gonzedge/rambling-trie
173
+ [rvm]: https://rvm.io
174
+ [travis_ci_badge]: https://travis-ci.org/gonzedge/rambling-trie.svg
175
+ [travis_ci_link]: https://travis-ci.org/gonzedge/rambling-trie
data/lib/rambling/trie.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'forwardable'
2
2
  %w{
3
- branches compressor enumerable
4
- inspector invalid_operation node
3
+ branches compressor enumerable inspector
4
+ invalid_operation node missing_node
5
5
  plain_text_reader root version
6
- }.map { |file| File.join 'rambling', 'trie', file }.each &method(:require)
6
+ }.each do |file|
7
+ require File.join('rambling', 'trie', file)
8
+ end
7
9
 
8
10
  # General namespace for all Rambling gems.
9
11
  module Rambling
@@ -51,6 +51,38 @@ module Rambling
51
51
  end
52
52
  end
53
53
 
54
+ def closest_node_when_uncompressed chars
55
+ if chars.empty?
56
+ self
57
+ else
58
+ first_letter_sym = chars.slice!(0).to_sym
59
+ if children_tree.has_key? first_letter_sym
60
+ children_tree[first_letter_sym].closest_node_when_uncompressed chars
61
+ else
62
+ Rambling::Trie::MissingNode.new
63
+ end
64
+ end
65
+ end
66
+
67
+ def closest_node_when_compressed chars
68
+ if chars.empty?
69
+ self
70
+ else
71
+ current_length = 0
72
+ current_key, current_key_string = current_key chars.slice!(0)
73
+
74
+ begin
75
+ current_length += 1
76
+
77
+ if current_key_string.length == current_length || chars.empty?
78
+ return children_tree[current_key].closest_node_when_compressed chars
79
+ end
80
+ end while current_key_string[current_length] == chars.slice!(0)
81
+
82
+ Rambling::Trie::MissingNode.new
83
+ end
84
+ end
85
+
54
86
  private
55
87
 
56
88
  def add_to_children_tree word
@@ -0,0 +1,7 @@
1
+ module Rambling
2
+ module Trie
3
+ # A representation of a missing node in the Trie data structure.
4
+ class MissingNode < Node
5
+ end
6
+ end
7
+ end
@@ -57,6 +57,15 @@ module Rambling
57
57
 
58
58
  alias_method :include?, :word?
59
59
 
60
+ # Returns all words that start with the specified characters.
61
+ # @param [String] word the word to look for in the trie.
62
+ # @return [Array] all the words contained in the trie that start with the specified characters.
63
+ def scan word = ''
64
+ closest_node(word).to_a
65
+ end
66
+
67
+ alias_method :words, :scan
68
+
60
69
  private
61
70
 
62
71
  attr_accessor :compressed
@@ -65,6 +74,12 @@ module Rambling
65
74
  method = compressed? ? "#{method}_when_compressed?" : "#{method}_when_uncompressed?"
66
75
  send method, word.chars.to_a
67
76
  end
77
+
78
+ def closest_node word
79
+ method = compressed? ? :closest_node_when_compressed : :closest_node_when_uncompressed
80
+
81
+ send method, word.chars.to_a
82
+ end
68
83
  end
69
84
  end
70
85
  end
@@ -1,6 +1,6 @@
1
1
  module Rambling
2
2
  module Trie
3
3
  # Current version of the rambling-trie.
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
6
6
  end
@@ -20,9 +20,9 @@ Gem::Specification.new do |gem|
20
20
  gem.version = Rambling::Trie::VERSION
21
21
  gem.platform = Gem::Platform::RUBY
22
22
 
23
- gem.add_development_dependency 'rspec', '~> 3.1'
24
- gem.add_development_dependency 'rake', '~> 10.4'
23
+ gem.add_development_dependency 'rspec', '~> 3.4'
24
+ gem.add_development_dependency 'rake', '~> 10.5'
25
25
  gem.add_development_dependency 'ruby-prof', '~> 0.15.2'
26
26
  gem.add_development_dependency 'yard', '~> 0.8.7'
27
- gem.add_development_dependency 'redcarpet', '~> 3.2.1'
27
+ gem.add_development_dependency 'redcarpet', '~> 3.3.4'
28
28
  end
@@ -34,7 +34,7 @@ module Rambling
34
34
 
35
35
  describe '#root?' do
36
36
  it 'returns false' do
37
- expect(subject).to_not be_root
37
+ expect(subject).not_to be_root
38
38
  end
39
39
  end
40
40
 
@@ -51,7 +51,7 @@ module Rambling
51
51
  end
52
52
 
53
53
  it 'is not a terminal node' do
54
- expect(subject).to_not be_terminal
54
+ expect(subject).not_to be_terminal
55
55
  end
56
56
 
57
57
  it 'returns empty string as its word' do
@@ -59,7 +59,7 @@ module Rambling
59
59
  end
60
60
 
61
61
  it 'is not compressed' do
62
- expect(subject).to_not be_compressed
62
+ expect(subject).not_to be_compressed
63
63
  end
64
64
  end
65
65
 
@@ -75,7 +75,7 @@ module Rambling
75
75
  end
76
76
 
77
77
  it 'is not a terminal node' do
78
- expect(subject).to_not be_terminal
78
+ expect(subject).not_to be_terminal
79
79
  end
80
80
 
81
81
  it 'returns empty string as its word' do
@@ -83,7 +83,7 @@ module Rambling
83
83
  end
84
84
 
85
85
  it 'is not compressed' do
86
- expect(subject).to_not be_compressed
86
+ expect(subject).not_to be_compressed
87
87
  end
88
88
  end
89
89
 
@@ -127,7 +127,7 @@ module Rambling
127
127
  end
128
128
 
129
129
  it 'does not mark itself as a terminal node' do
130
- expect(subject).to_not be_terminal
130
+ expect(subject).not_to be_terminal
131
131
  end
132
132
 
133
133
  it 'marks the first child as a terminal node' do
@@ -143,13 +143,13 @@ module Rambling
143
143
  end
144
144
 
145
145
  it 'does not mark any other letter as terminal node' do
146
- expect(subject[:p][:a][:g][:h][:e][:t][:t]).to_not be_terminal
147
- expect(subject[:p][:a][:g][:h][:e][:t]).to_not be_terminal
148
- expect(subject[:p][:a][:g][:h][:e]).to_not be_terminal
149
- expect(subject[:p][:a][:g][:h]).to_not be_terminal
150
- expect(subject[:p][:a][:g]).to_not be_terminal
151
- expect(subject[:p][:a]).to_not be_terminal
152
- expect(subject[:p]).to_not be_terminal
146
+ expect(subject[:p][:a][:g][:h][:e][:t][:t]).not_to be_terminal
147
+ expect(subject[:p][:a][:g][:h][:e][:t]).not_to be_terminal
148
+ expect(subject[:p][:a][:g][:h][:e]).not_to be_terminal
149
+ expect(subject[:p][:a][:g][:h]).not_to be_terminal
150
+ expect(subject[:p][:a][:g]).not_to be_terminal
151
+ expect(subject[:p][:a]).not_to be_terminal
152
+ expect(subject[:p]).not_to be_terminal
153
153
  end
154
154
  end
155
155
  end
@@ -219,7 +219,7 @@ module Rambling
219
219
  end
220
220
 
221
221
  it 'returns false' do
222
- expect(subject).to_not be_compressed
222
+ expect(subject).not_to be_compressed
223
223
  end
224
224
  end
225
225
  end
@@ -19,11 +19,11 @@ module Rambling
19
19
  end
20
20
 
21
21
  it 'is not a terminal node' do
22
- expect(subject).to_not be_terminal
22
+ expect(subject).not_to be_terminal
23
23
  end
24
24
 
25
25
  it 'is not a word' do
26
- expect(subject).to_not be_word
26
+ expect(subject).not_to be_word
27
27
  end
28
28
 
29
29
  context 'with a block' do
@@ -34,11 +34,11 @@ module Rambling
34
34
  end
35
35
 
36
36
  it 'is not a terminal node' do
37
- expect(subject).to_not be_terminal
37
+ expect(subject).not_to be_terminal
38
38
  end
39
39
 
40
40
  it 'is not a word' do
41
- expect(subject).to_not be_word
41
+ expect(subject).not_to be_word
42
42
  end
43
43
 
44
44
  it 'executes the block' do
@@ -205,7 +205,7 @@ module Rambling
205
205
  end
206
206
 
207
207
  it 'is aliased as #include?' do
208
- expect(subject).to_not include 'high'
208
+ expect(subject).not_to include 'high'
209
209
  end
210
210
 
211
211
  context 'and the root has been compressed' do
@@ -279,6 +279,80 @@ module Rambling
279
279
  end
280
280
  end
281
281
 
282
+ describe '#scan' do
283
+ context 'words that match are not contained' do
284
+ before do
285
+ subject << 'hi'
286
+ subject << 'hello'
287
+ subject << 'high'
288
+ subject << 'hell'
289
+ subject << 'highlight'
290
+ subject << 'histerical'
291
+ end
292
+
293
+ it 'returns an array with the words that match' do
294
+ expect(subject.scan 'hi').to eq [
295
+ 'hi',
296
+ 'high',
297
+ 'highlight',
298
+ 'histerical'
299
+ ]
300
+
301
+ expect(subject.scan 'hig').to eq [
302
+ 'high',
303
+ 'highlight'
304
+ ]
305
+ end
306
+
307
+ context 'and the root has been compressed' do
308
+ before do
309
+ subject.compress!
310
+ end
311
+
312
+ it 'returns an array with the words that match' do
313
+ expect(subject.scan 'hi').to eq [
314
+ 'hi',
315
+ 'high',
316
+ 'highlight',
317
+ 'histerical'
318
+ ]
319
+
320
+ expect(subject.scan 'hig').to eq [
321
+ 'high',
322
+ 'highlight'
323
+ ]
324
+ end
325
+
326
+ it 'is aliased as #words' do
327
+ expect(subject.words 'hig').to eq [
328
+ 'high',
329
+ 'highlight'
330
+ ]
331
+ end
332
+ end
333
+ end
334
+
335
+ context 'words that match are not contained' do
336
+ before do
337
+ subject << 'hello'
338
+ end
339
+
340
+ it 'returns an empty array' do
341
+ expect(subject.scan 'hi').to eq []
342
+ end
343
+
344
+ context 'and the root has been compressed' do
345
+ before do
346
+ subject.compress!
347
+ end
348
+
349
+ it 'returns an empty array' do
350
+ expect(subject.scan 'hi').to eq []
351
+ end
352
+ end
353
+ end
354
+ end
355
+
282
356
  describe '#add' do
283
357
  let(:original_word) { 'word' }
284
358
  let(:word) { original_word.clone }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rambling-trie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgar Gonzalez
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-06 00:00:00.000000000 Z
12
+ date: 2016-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -17,28 +17,28 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '3.1'
20
+ version: '3.4'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '3.1'
27
+ version: '3.4'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '10.4'
34
+ version: '10.5'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '10.4'
41
+ version: '10.5'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: ruby-prof
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -73,14 +73,14 @@ dependencies:
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: 3.2.1
76
+ version: 3.3.4
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: 3.2.1
83
+ version: 3.3.4
84
84
  description: The Rambling Trie is a custom implementation of the Trie data structure
85
85
  with Ruby, which includes compression abilities and is designed to be very fast
86
86
  to traverse.
@@ -98,7 +98,7 @@ files:
98
98
  - Gemfile
99
99
  - Guardfile
100
100
  - LICENSE
101
- - README.markdown
101
+ - README.md
102
102
  - Rakefile
103
103
  - assets/dictionaries/words_with_friends.txt
104
104
  - lib/rambling-trie.rb
@@ -108,6 +108,7 @@ files:
108
108
  - lib/rambling/trie/enumerable.rb
109
109
  - lib/rambling/trie/inspector.rb
110
110
  - lib/rambling/trie/invalid_operation.rb
111
+ - lib/rambling/trie/missing_node.rb
111
112
  - lib/rambling/trie/node.rb
112
113
  - lib/rambling/trie/plain_text_reader.rb
113
114
  - lib/rambling/trie/root.rb
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  requirements: []
148
149
  rubyforge_project:
149
- rubygems_version: 2.4.3
150
+ rubygems_version: 2.5.1
150
151
  signing_key:
151
152
  specification_version: 4
152
153
  summary: A custom implementation of the trie data structure.