Permalink
Browse files

Developers can now define a custom Proc for stemming.

  • Loading branch information...
1 parent be9cb95 commit a7ef8b52b71cf39eb426af3a0fa96521daf6543a @compiled compiled committed Nov 22, 2012
Showing with 39 additions and 1 deletion.
  1. +4 −0 lib/mongoid_search.rb
  2. +2 −1 lib/mongoid_search/util.rb
  3. +16 −0 spec/mongoid_search_spec.rb
  4. +17 −0 spec/util_spec.rb
View
@@ -20,6 +20,10 @@ module Mongoid::Search
mattr_accessor :stem_keywords
@@stem_keywords = false
+ ## Stem procedure
+ mattr_accessor :stem_proc
+ @@stem_proc = Proc.new { |word| word.stem }
+
## Words to ignore
mattr_accessor :ignore_list
@@ignore_list = []
@@ -35,6 +35,7 @@ def self.normalize_keywords(text)
ligatures = Mongoid::Search.ligatures
ignore_list = Mongoid::Search.ignore_list
stem_keywords = Mongoid::Search.stem_keywords
+ stem_proc = Mongoid::Search.stem_proc
return [] if text.blank?
text = text.to_s.
@@ -48,7 +49,7 @@ def self.normalize_keywords(text)
split(' ').
reject { |word| word.size < Mongoid::Search.minimum_word_size }
text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
- text = text.map(&:stem) if stem_keywords
+ text = text.map(&stem_proc) if stem_keywords
text
end
@@ -4,10 +4,19 @@
describe Mongoid::Search do
+ before(:all) do
+ @default_proc = Mongoid::Search.stem_proc
+ end
+
+ after(:all) do
+ Mongoid::Search.stem_proc = @default_proc
+ end
+
before(:each) do
Mongoid::Search.match = :any
Mongoid::Search.stem_keywords = false
Mongoid::Search.ignore_list = nil
+ Mongoid::Search.stem_proc = @default_proc
@product = Product.create :brand => "Apple",
:name => "iPhone",
:tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) },
@@ -104,6 +113,13 @@
@product._keywords.sort.should == ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "ol", "info", "descript", "summari"].sort
end
+ it "should set the _keywords field with custom stemmed words if stem is enabled with a custom lambda" do
+ Mongoid::Search.stem_keywords = true
+ Mongoid::Search.stem_proc = Proc.new { |word| word.upcase }
+ @product.save!
+ @product._keywords.sort.should == ["AMAZING", "APPLE", "AWESOME", "CRADDLE", "DESCRIPTION", "INFO", "IPHONE", "MOBILE", "OLE", "SUMMARY"]
+ end
+
it "should ignore keywords in an ignore list" do
Mongoid::Search.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]
@product.save!
View
@@ -2,9 +2,19 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Mongoid::Search::Util do
+
+ before(:all) do
+ @default_proc = Mongoid::Search.stem_proc
+ end
+
+ after(:all) do
+ Mongoid::Search.stem_proc = @default_proc
+ end
+
before do
Mongoid::Search.stem_keywords = false
Mongoid::Search.ignore_list = ""
+ Mongoid::Search.stem_proc = @default_proc
end
it "should return an empty array if no text is passed" do
@@ -40,6 +50,13 @@
Mongoid::Search::Util.normalize_keywords("A runner running and eating").should == ["runner", "run", "and", "eat"]
end
+ it "should stem keywords using a custom proc" do
+ Mongoid::Search.stem_keywords = true
+ Mongoid::Search.stem_proc = lambda { |word| word.upcase }
+
+ Mongoid::Search::Util.normalize_keywords("A runner running and eating").should == ["RUNNER", "RUNNING", "AND", "EATING"]
+ end
+
it "should ignore keywords from ignore list" do
Mongoid::Search.stem_keywords = true
Mongoid::Search.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]

0 comments on commit a7ef8b5

Please sign in to comment.