Skip to content
Browse files

Merge pull request #31 from HungYuHei/min_complete

Make min_complete configurable
  • Loading branch information...
2 parents 30ab62d + 56cc3e9 commit 249f802d11090627932e5dbe5fde98c68ba03cac @erwaller erwaller committed May 24, 2013
Showing with 61 additions and 45 deletions.
  1. +2 −40 lib/soulmate.rb
  2. +52 −0 lib/soulmate/config.rb
  3. +1 −1 lib/soulmate/helpers.rb
  4. +1 −1 lib/soulmate/loader.rb
  5. +2 −2 lib/soulmate/matcher.rb
  6. +3 −1 test/test_soulmate.rb
View
42 lib/soulmate.rb
@@ -1,50 +1,12 @@
-require 'uri'
require 'multi_json'
-require 'redis'
require 'soulmate/version'
require 'soulmate/helpers'
require 'soulmate/base'
require 'soulmate/matcher'
require 'soulmate/loader'
+require 'soulmate/config'
module Soulmate
-
- extend self
-
- MIN_COMPLETE = 2
- DEFAULT_STOP_WORDS = ["vs", "at", "the"]
-
- def redis=(server)
- if server.is_a?(String)
- @redis = nil
- @redis_url = server
- else
- @redis = server
- end
-
- redis
- end
-
- def redis
- @redis ||= (
- url = URI(@redis_url || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
-
- ::Redis.new({
- :host => url.host,
- :port => url.port,
- :db => url.path[1..-1],
- :password => url.password
- })
- )
- end
-
- def stop_words
- @stop_words ||= DEFAULT_STOP_WORDS
- end
-
- def stop_words=(arr)
- @stop_words = Array(arr).flatten
- end
-
+ extend Config
end
View
52 lib/soulmate/config.rb
@@ -0,0 +1,52 @@
+require 'uri'
+require 'redis'
+
+module Soulmate
+ module Config
+ DEFAULT_MIN_COMPLETE = 2
+ DEFAULT_STOP_WORDS = ["vs", "at", "the"]
+
+ attr_writer :min_complete
+
+ def min_complete
+ @min_complete ||= DEFAULT_MIN_COMPLETE
+ end
+
+ # Accepts:
+ # 1. A Redis URL String 'redis://host:port/db'
+ # 2. An existing instance of Redis, Redis::Namespace, etc.
+ def redis=(server)
+ if server.is_a?(String)
+ @redis = nil
+ @redis_url = server
+ else
+ @redis = server
+ end
+
+ redis
+ end
+
+ # Returns the current Redis connection. If none has been created, will
+ # create a new one.
+ def redis
+ @redis ||= (
+ url = URI(@redis_url || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
+
+ ::Redis.new({
+ :host => url.host,
+ :port => url.port,
+ :db => url.path[1..-1],
+ :password => url.password
+ })
+ )
+ end
+
+ def stop_words
+ @stop_words ||= DEFAULT_STOP_WORDS
+ end
+
+ def stop_words=(arr)
+ @stop_words = Array(arr).flatten
+ end
+ end
+end
View
2 lib/soulmate/helpers.rb
@@ -8,7 +8,7 @@ def prefixes_for_phrase(phrase)
Soulmate.stop_words.include?(w)
end
words.map do |w|
- (MIN_COMPLETE-1..(w.length-1)).map{ |l| w[0..l] }
+ (Soulmate.min_complete-1..(w.length-1)).map{ |l| w[0..l] }
end.flatten.uniq
end
View
2 lib/soulmate/loader.rb
@@ -61,4 +61,4 @@ def remove(item)
end
end
end
-end
+end
View
4 lib/soulmate/matcher.rb
@@ -6,7 +6,7 @@ def matches_for_term(term, options = {})
options = { :limit => 5, :cache => true }.merge(options)
words = normalize(term).split(' ').reject do |w|
- w.size < MIN_COMPLETE or Soulmate.stop_words.include?(w)
+ w.size < Soulmate.min_complete or Soulmate.stop_words.include?(w)
end.sort
return [] if words.empty?
@@ -29,4 +29,4 @@ def matches_for_term(term, options = {})
end
end
end
-end
+end
View
4 test/test_soulmate.rb
@@ -108,7 +108,9 @@ def test_prefixes_for_phrase
assert_equal ["te", "tes", "test", "testi", "testin", "th", "thi", "this"], loader.prefixes_for_phrase("testin' this")
assert_equal ["te", "tes", "test"], loader.prefixes_for_phrase("test test")
assert_equal ["so", "sou", "soul", "soulm", "soulma", "soulmat", "soulmate"], loader.prefixes_for_phrase("SoUlmATE")
-
assert_equal ['测试', '测试中', '测试中文', 'te', 'tes', 'test'], loader.prefixes_for_phrase('测试中文 test')
+
+ Soulmate.min_complete = 4
+ assert_equal ['同华东生', '同华东生产', '同华东生产队', 'abcd', 'abcde'], loader.prefixes_for_phrase('同华东生产队 abcde')
end
end

0 comments on commit 249f802

Please sign in to comment.
Something went wrong with that request. Please try again.