mongomapper_ext 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongomapper_ext/filter.rb +60 -14
- data/mongomapper_ext.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
@@ -30,14 +30,55 @@ module MongoMapperExt
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def filter(query, opts = {})
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
stemmer = nil
|
34
|
+
original_words = Set.new(query.downcase.split)
|
35
|
+
language = opts.delete(:language) || 'en'
|
36
|
+
|
37
|
+
stemmed = []
|
38
|
+
if defined?(Lingua)
|
39
|
+
stemmer = Lingua::Stemmer.new(:language => language)
|
40
|
+
original_words.each do |word|
|
41
|
+
stemmed_word = stemmer.stem(word)
|
42
|
+
stemmed << stemmed_word unless original_words.include?(stemmed_word)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
regex = (original_words+stemmed).map do |k|
|
47
|
+
/^#{Regexp.escape(k)}/
|
48
|
+
end
|
49
|
+
|
36
50
|
if opts[:per_page]
|
37
|
-
self.paginate(opts.deep_merge(:conditions => {:_keywords =>
|
51
|
+
result = self.paginate(opts.deep_merge(:conditions => {:_keywords => regex }))
|
52
|
+
pagination = MongoMapper::Plugins::Pagination::Proxy.new(result.total_entries, result.current_page, result.per_page)
|
53
|
+
|
54
|
+
pagination.subject = result.sort_by do |e|
|
55
|
+
evaluate_result(e._keywords, original_words, stemmed) * -1
|
56
|
+
end
|
57
|
+
|
58
|
+
pagination
|
38
59
|
else
|
39
|
-
self.all(opts.deep_merge(:conditions => {:_keywords =>
|
60
|
+
self.all(opts.deep_merge(:conditions => {:_keywords => regex })).sort_by do |e|
|
61
|
+
evaluate_result(e._keywords, original_words, stemmed)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
def evaluate_result(keywords, original_words, stemmed)
|
68
|
+
score = 0.0
|
69
|
+
original_words.each do |word|
|
70
|
+
if keywords.include?(word)
|
71
|
+
score += 15.0
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
stemmed.each do |word|
|
76
|
+
if keywords.include?(word)
|
77
|
+
score += 1.0 + word.length
|
78
|
+
end
|
40
79
|
end
|
80
|
+
|
81
|
+
score
|
41
82
|
end
|
42
83
|
end
|
43
84
|
|
@@ -72,18 +113,23 @@ module MongoMapperExt
|
|
72
113
|
words = []
|
73
114
|
val.downcase.split.each do |word|
|
74
115
|
next if word.length < 3
|
75
|
-
next if word =~ %r{'|"|\/|\\}
|
76
116
|
next if stop_words.include?(word)
|
77
117
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
118
|
+
deword = word.split(%r{'|"|\/|\\|\?|\+|_|\-|\>|\>})
|
119
|
+
|
120
|
+
deword.each do |word|
|
121
|
+
next if word.empty?
|
122
|
+
|
123
|
+
stem = word
|
124
|
+
if stemmer
|
125
|
+
stem = stemmer.stem(word)
|
126
|
+
end
|
82
127
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
128
|
+
if stem && stem != word
|
129
|
+
words += [stem, word]
|
130
|
+
else
|
131
|
+
words << word
|
132
|
+
end
|
87
133
|
end
|
88
134
|
end
|
89
135
|
|
data/mongomapper_ext.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongomapper_ext}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["David A. Cuadrado"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-20}
|
13
13
|
s.description = %q{MongoMapper extensions}
|
14
14
|
s.email = %q{krawek@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 2
|
9
|
+
version: 0.2.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- David A. Cuadrado
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-20 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|