redis_autocomplete 0.1.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.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +24 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/redis_autocomplete.rb +45 -0
- data/spec/redis_autocomplete_spec.rb +70 -0
- data/spec/spec_helper.rb +12 -0
- metadata +154 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem 'redis'
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development, :test do
|
11
|
+
gem "rspec", "~> 2.3.0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.5.2"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
redis (2.1.1)
|
13
|
+
rspec (2.3.0)
|
14
|
+
rspec-core (~> 2.3.0)
|
15
|
+
rspec-expectations (~> 2.3.0)
|
16
|
+
rspec-mocks (~> 2.3.0)
|
17
|
+
rspec-core (2.3.1)
|
18
|
+
rspec-expectations (2.3.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.3.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bundler (~> 1.0.0)
|
27
|
+
jeweler (~> 1.5.2)
|
28
|
+
rcov
|
29
|
+
redis
|
30
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Joe Johnston
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= About
|
2
|
+
|
3
|
+
RedisAutocomplete is a simple gem for extremely fast autocomplete suggestions. Just add words to it and ask
|
4
|
+
it to make suggestions given a search prefix.
|
5
|
+
|
6
|
+
The algorithm used is orginally from Salvatore (antirez) Sanfilippo, the Redis author.
|
7
|
+
http://antirez.com/post/autocomplete-with-redis.html
|
8
|
+
|
9
|
+
RedisAutocomplete was written and maintained by simple10[http://github.com/simple10] and Empact[http://github.com/Empact] for http://connect.me.
|
10
|
+
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
# initialize with the name of the redis set you want to use
|
15
|
+
r = RedisAutocomplete.new(:tags)
|
16
|
+
r.add_words(%w[developer, designer, dude, architect, baker, banker])
|
17
|
+
r.suggest('de')
|
18
|
+
# ['developer', 'designer']
|
19
|
+
|
20
|
+
== Copyright
|
21
|
+
|
22
|
+
Copyright (c) 2011 Joe Johnston. See LICENSE.txt for
|
23
|
+
further details.
|
24
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "redis_autocomplete"
|
16
|
+
gem.homepage = "http://github.com/connectme/redis_autocomplete"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Use Redis for fast autocomplete suggestions}
|
19
|
+
gem.description = %Q{RedisAutocomplete is a simple gem for extremely fast autocomplete suggestions. Just add words to it and ask it to make suggestions given a search prefix.
|
20
|
+
|
21
|
+
The algorithm used is orginally from Salvatore (antirez) Sanfilippo, the Redis author. http://antirez.com/post/autocomplete-with-redis.html
|
22
|
+
|
23
|
+
RedisAutocomplete is maintained by simple10[http://github.com/simple10] and Empact[http://github.com/Empact] for http://connect.me.}
|
24
|
+
gem.email = "joe@simple10.com"
|
25
|
+
gem.authors = ["Joe Johnston (simple10)", "Ben Woosley (Empact)", "Salvatore Sanfilippo (antirez)"]
|
26
|
+
gem.add_runtime_dependency 'redis'
|
27
|
+
gem.add_development_dependency 'rspec'
|
28
|
+
end
|
29
|
+
Jeweler::RubygemsDotOrgTasks.new
|
30
|
+
|
31
|
+
require 'rspec/core'
|
32
|
+
require 'rspec/core/rake_task'
|
33
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
34
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
35
|
+
end
|
36
|
+
|
37
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
38
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
39
|
+
spec.rcov = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :spec
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "redis_autocomplete #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
class RedisAutocomplete
|
4
|
+
attr_reader :redis, :terminal
|
5
|
+
|
6
|
+
def initialize(set, disallowed_chars = /[^a-zA-Z0-9_-]/, terminal = '+')
|
7
|
+
@set = set
|
8
|
+
@redis = Redis.new
|
9
|
+
@disallowed_chars = disallowed_chars
|
10
|
+
@terminal = terminal
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_word(word)
|
14
|
+
w = word.gsub(@disallowed_chars, '')
|
15
|
+
(1..(w.length)).each { |i| @redis.zadd(@set, 0, w.slice(0, i)) }
|
16
|
+
@redis.zadd(@set, 0, "#{w}#{@terminal}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_words(*words)
|
20
|
+
words.flatten.compact.uniq.each { |word| add_word word }
|
21
|
+
end
|
22
|
+
|
23
|
+
def suggest(prefix, count = 10)
|
24
|
+
results = []
|
25
|
+
rangelen = 50 # This is not random, try to get replies < MTU size
|
26
|
+
start = @redis.zrank(@set, prefix)
|
27
|
+
return [] if !start
|
28
|
+
while results.length != count
|
29
|
+
range = @redis.zrange(@set, start, start+rangelen-1)
|
30
|
+
start += rangelen
|
31
|
+
break if !range || range.length == 0
|
32
|
+
range.each do |entry|
|
33
|
+
minlen = [entry.length, prefix.length].min
|
34
|
+
if entry.slice(0, minlen) != prefix.slice(0, minlen)
|
35
|
+
count = results.count
|
36
|
+
break
|
37
|
+
end
|
38
|
+
if entry[-1] == @terminal and results.length != count
|
39
|
+
results << entry.chomp(@terminal)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
return results
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
#require 'redis_autocomplete'
|
4
|
+
|
5
|
+
describe RedisAutocomplete do
|
6
|
+
before do
|
7
|
+
@names = %w[
|
8
|
+
ynes
|
9
|
+
ynez
|
10
|
+
yoko
|
11
|
+
yolanda
|
12
|
+
yolande
|
13
|
+
yolane
|
14
|
+
yolanthe
|
15
|
+
aaren
|
16
|
+
aarika
|
17
|
+
abagael
|
18
|
+
abagail
|
19
|
+
catherine
|
20
|
+
cathi
|
21
|
+
cathie
|
22
|
+
cathleen
|
23
|
+
cathlene
|
24
|
+
cathrin
|
25
|
+
cathrine
|
26
|
+
cathryn
|
27
|
+
cathy
|
28
|
+
cathyleen
|
29
|
+
cati
|
30
|
+
catie
|
31
|
+
catina
|
32
|
+
catlaina
|
33
|
+
catlee
|
34
|
+
catlin
|
35
|
+
]
|
36
|
+
@set = :test_female_names
|
37
|
+
@r = RedisAutocomplete.new(@set)
|
38
|
+
#todo: drop female_names set
|
39
|
+
@r.add_words(@names)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#suggest" do
|
43
|
+
it "should include words matching prefix" do
|
44
|
+
@r.suggest('c').should == %w[
|
45
|
+
catherine
|
46
|
+
cathi
|
47
|
+
cathie
|
48
|
+
cathleen
|
49
|
+
cathlene
|
50
|
+
cathrin
|
51
|
+
cathrine
|
52
|
+
cathryn
|
53
|
+
cathy
|
54
|
+
cathyleen
|
55
|
+
]
|
56
|
+
end
|
57
|
+
it "should not include words not matching prefix" do
|
58
|
+
@r.suggest('cati').should_not include('cathy')
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when a max count is supplied" do
|
62
|
+
it "should not include more than 10 matches" do
|
63
|
+
@r.suggest('c').length.should == 10
|
64
|
+
end
|
65
|
+
it "should not include more matches than the supplied count" do
|
66
|
+
@r.suggest('c', 4).length.should == 4
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'redis_autocomplete'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_autocomplete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joe Johnston (simple10)
|
9
|
+
- Ben Woosley (Empact)
|
10
|
+
- Salvatore Sanfilippo (antirez)
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
|
15
|
+
date: 2011-02-10 00:00:00 -08:00
|
16
|
+
default_executable:
|
17
|
+
dependencies:
|
18
|
+
- !ruby/object:Gem::Dependency
|
19
|
+
name: redis
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: "0"
|
26
|
+
type: :runtime
|
27
|
+
prerelease: false
|
28
|
+
version_requirements: *id001
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rspec
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.3.0
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: *id002
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: bundler
|
42
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: *id003
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: jeweler
|
53
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.5.2
|
59
|
+
type: :development
|
60
|
+
prerelease: false
|
61
|
+
version_requirements: *id004
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rcov
|
64
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: *id005
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: redis
|
75
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
type: :runtime
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: *id006
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: *id007
|
95
|
+
description: |-
|
96
|
+
RedisAutocomplete is a simple gem for extremely fast autocomplete suggestions. Just add words to it and ask it to make suggestions given a search prefix.
|
97
|
+
|
98
|
+
The algorithm used is orginally from Salvatore (antirez) Sanfilippo, the Redis author. http://antirez.com/post/autocomplete-with-redis.html
|
99
|
+
|
100
|
+
RedisAutocomplete is maintained by simple10[http://github.com/simple10] and Empact[http://github.com/Empact] for http://connect.me.
|
101
|
+
email: joe@simple10.com
|
102
|
+
executables: []
|
103
|
+
|
104
|
+
extensions: []
|
105
|
+
|
106
|
+
extra_rdoc_files:
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.rdoc
|
109
|
+
files:
|
110
|
+
- .document
|
111
|
+
- .rspec
|
112
|
+
- Gemfile
|
113
|
+
- Gemfile.lock
|
114
|
+
- LICENSE.txt
|
115
|
+
- README.rdoc
|
116
|
+
- Rakefile
|
117
|
+
- VERSION
|
118
|
+
- lib/redis_autocomplete.rb
|
119
|
+
- spec/redis_autocomplete_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
has_rdoc: true
|
122
|
+
homepage: http://github.com/connectme/redis_autocomplete
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 512640415964739179
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: "0"
|
145
|
+
requirements: []
|
146
|
+
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 1.5.0
|
149
|
+
signing_key:
|
150
|
+
specification_version: 3
|
151
|
+
summary: Use Redis for fast autocomplete suggestions
|
152
|
+
test_files:
|
153
|
+
- spec/redis_autocomplete_spec.rb
|
154
|
+
- spec/spec_helper.rb
|