sliding_window 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +32 -0
  3. data/lib/sliding_window.rb +58 -0
  4. metadata +130 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7064b873b87732312ca43a607a89453258225da9
4
+ data.tar.gz: 7c38068b3bb1c1944f4deacc703f92640cf233eb
5
+ SHA512:
6
+ metadata.gz: aecb490e34a82542c0dd7c2b33401c572a0ecd11b9fc84a39bac3359cc2adff7352204f7481bb8923dac891df8de069c5076d03fd0c4d1cf4c8a8cff9338e1a2
7
+ data.tar.gz: 9c37dabb5be2dcfd607c0c4c82f8bc6684b5ee3b637b90f96cdc70390170baa43e2578dde6f69b628756a681a4a554c0ff82dae8b0767b665217146d17dd400a
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Sliding Window [![Build Status](https://travis-ci.org/nathankleyn/sliding_windowsvg)](https://travis-ci.org/nathankleyn/sliding_window) [![RubyGems Version Of gitignore_rb](https://img.shields.io/gem/v/sliding_window.svg)](https://rubygems.org/gems/sliding_window)
2
+
3
+ Implements the sliding window algorithm. This is useful for traversing phrases during NEL processing.
4
+
5
+ ## Usage
6
+
7
+ The Gem is called `sliding_window` and you can it is available via [RubyGems](https://rubygems.org/gems/sliding_window):
8
+
9
+ ```sh
10
+ gem install sliding_window
11
+ ```
12
+
13
+ ## Examples
14
+
15
+ A simple example is as follows:
16
+
17
+ ```ruby
18
+ require 'sliding_window'
19
+
20
+ mapping = {
21
+ 'The Queen' => 'Queen Elizabeth II',
22
+ 'Queen' => 'Queen (band)',
23
+ 'Buckingham Palace' => 'Buckingham Palace'
24
+ }
25
+
26
+ sliding_window = SlidingWindow.new(%w{The Queen attended a concert at Buckingham Palace where Queen played some music}) do |window|
27
+ key = window.join(' ')
28
+ mapping[key] if mapping.include?(key)
29
+ end
30
+
31
+ sliding_window.to_a # => ['Queen Elizabeth II', 'Buckingham Palace', 'Queen (band)']
32
+ ```
@@ -0,0 +1,58 @@
1
+ # Implementation of the sliding window algorithm.
2
+ class SlidingWindow
3
+ include Enumerable
4
+
5
+ # Initialize the sliding window. Given a buffer to slide over, the maximum
6
+ # size of the window, and a block to test whether the window matches anything,
7
+ # enumerate the sliding window instance to get at the matches.
8
+ #
9
+ # buffer - The buffer to slide the window over. It can be any type, as long as
10
+ # it responds to `#[]` and `#size`.
11
+ # max_size - The maximum allowed length of the window. Defaults to 5.
12
+ # block - A block which will test whether the position of the current window
13
+ # matches anything of interest. The block is passed the current window
14
+ # into the buffer and is expected to return an object if the window
15
+ # matches (it can be anything), otherwise `nil`. The objects returned
16
+ # by this block will be yielded to the enumerable.
17
+ def initialize(buffer, max_size = 5, &block)
18
+ fail('The given buffer must respond to #[] and #size.') unless buffer.respond_to?(:[]) && buffer.respond_to?(:size)
19
+ fail('The maximum buffer size must be greater than or equal to one.') if max_size.nil? || max_size < 1
20
+ fail('A block is required to indicate whether a window matches anything of interest.') unless block_given?
21
+
22
+ @buffer = buffer
23
+ @max_size = max_size
24
+ @mapper = block
25
+ end
26
+
27
+ # Enumerable yields each time the window matches on something. The internal
28
+ # window is slid forward as expected.
29
+ def each
30
+ position = (0..@max_size - 1)
31
+
32
+ loop do
33
+ window = @buffer[position]
34
+ match = @mapper.call(window)
35
+
36
+ if match.nil?
37
+ position = (position.begin..position.end - 1)
38
+ position = slide_unmatched_window(position) if position.end < position.begin
39
+ else
40
+ yield match
41
+ position = slide_matched_window(position)
42
+ end
43
+
44
+ break if position.first >= @buffer.size
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def slide_unmatched_window(position)
51
+ start = position.begin + 1
52
+ (start..start + @max_size)
53
+ end
54
+
55
+ def slide_matched_window(position)
56
+ (position.end + 1..position.end + @max_size)
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sliding_window
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Kleyn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coveralls
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: filewatcher
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.34.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.34.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.1
97
+ description: Ruby implementation of a sliding window algorithm.
98
+ email: nathan@nathankleyn.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - README.md
104
+ - lib/sliding_window.rb
105
+ homepage: https://github.com/username/sliding_window
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.5.1
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Ruby implementation of a sliding window algorithm.
129
+ test_files: []
130
+ has_rdoc: