Permalink
Browse files

Make min_complete configurable

  • Loading branch information...
1 parent 30ab62d commit 2a00a26df8aa1de3544da5a6adc9e50550bb58ad @HungYuHei HungYuHei committed May 20, 2013
Showing with 14 additions and 6 deletions.
  1. +7 −1 lib/soulmate.rb
  2. +1 −1 lib/soulmate/helpers.rb
  3. +1 −1 lib/soulmate/loader.rb
  4. +2 −2 lib/soulmate/matcher.rb
  5. +3 −1 test/test_soulmate.rb
View
@@ -12,9 +12,15 @@ module Soulmate
extend self
- MIN_COMPLETE = 2
+ DEFAULT_MIN_COMPLETE = 2
DEFAULT_STOP_WORDS = ["vs", "at", "the"]
+ attr_writer :min_complete
+
+ def min_complete
+ @min_complete ||= DEFAULT_MIN_COMPLETE
+ end
+
def redis=(server)
if server.is_a?(String)
@redis = nil
@@ -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
@@ -61,4 +61,4 @@ def remove(item)
end
end
end
-end
+end
@@ -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
@@ -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 2a00a26

Please sign in to comment.