picky 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/picky/application.rb +8 -0
- data/lib/picky/extensions/array.rb +15 -0
- data/lib/picky/index/bundle.rb +0 -3
- data/lib/picky/index/category.rb +4 -1
- data/lib/picky/loader.rb +3 -5
- data/lib/picky/query/allocation.rb +6 -6
- data/lib/picky/query/base.rb +14 -6
- data/lib/picky/query/combination.rb +13 -9
- data/lib/picky/query/combinations.rb +12 -16
- data/lib/picky/query/weigher.rb +11 -8
- data/lib/picky/query/weights.rb +7 -10
- data/lib/picky/routing.rb +11 -0
- data/lib/tasks/application.rake +0 -1
- data/lib/tasks/routes.rake +7 -0
- data/spec/lib/extensions/array_spec.rb +22 -1
- data/spec/lib/index/category_spec.rb +1 -3
- data/spec/lib/query/allocation_spec.rb +11 -15
- data/spec/lib/query/combination_spec.rb +1 -2
- data/spec/lib/query/combinations_spec.rb +18 -20
- metadata +4 -3
data/lib/picky/application.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
class Application
|
4
4
|
class << self
|
5
5
|
|
6
|
+
attr_reader :apps
|
7
|
+
|
6
8
|
# Finalize the subclass as soon as it
|
7
9
|
# has finished loading.
|
8
10
|
#
|
@@ -50,5 +52,11 @@ class Application
|
|
50
52
|
@queries ||= Configuration::Queries.new
|
51
53
|
end
|
52
54
|
|
55
|
+
# TODO Add more info.
|
56
|
+
#
|
57
|
+
def to_s
|
58
|
+
"#{self.name}:\n#{routing}"
|
59
|
+
end
|
60
|
+
|
53
61
|
end
|
54
62
|
end
|
@@ -12,6 +12,21 @@ class Array
|
|
12
12
|
result
|
13
13
|
end
|
14
14
|
end
|
15
|
+
# Around 10% faster than the above.
|
16
|
+
#
|
17
|
+
# Returns a copy.
|
18
|
+
#
|
19
|
+
def clustered_uniq_fast
|
20
|
+
result = []
|
21
|
+
self.inject(nil) do |last, element|
|
22
|
+
if last == element
|
23
|
+
last
|
24
|
+
else
|
25
|
+
result << element && element
|
26
|
+
end
|
27
|
+
end
|
28
|
+
result
|
29
|
+
end
|
15
30
|
|
16
31
|
# Accesses a random element of this array.
|
17
32
|
#
|
data/lib/picky/index/bundle.rb
CHANGED
@@ -174,15 +174,12 @@ module Index
|
|
174
174
|
load_weights
|
175
175
|
end
|
176
176
|
def load_index
|
177
|
-
timed_exclaim "Loading the index for #{identifier} from the cache."
|
178
177
|
self.index = files.load_index
|
179
178
|
end
|
180
179
|
def load_similarity
|
181
|
-
timed_exclaim "Loading the similarity for #{identifier} from the cache."
|
182
180
|
self.similarity = files.load_similarity
|
183
181
|
end
|
184
182
|
def load_weights
|
185
|
-
timed_exclaim "Loading the weights for #{identifier} from the cache."
|
186
183
|
self.weights = files.load_weights
|
187
184
|
end
|
188
185
|
|
data/lib/picky/index/category.rb
CHANGED
@@ -29,12 +29,15 @@ module Index
|
|
29
29
|
# Loads the index from cache.
|
30
30
|
#
|
31
31
|
def load_from_cache
|
32
|
+
timed_exclaim "Loading index #{identifier}."
|
32
33
|
exact.load
|
33
34
|
partial.load
|
34
35
|
end
|
35
36
|
|
37
|
+
# TODO Move to initializer?
|
38
|
+
#
|
36
39
|
def identifier
|
37
|
-
"#{type.name} #{name}"
|
40
|
+
@identifier ||= "#{type.name} #{name}"
|
38
41
|
end
|
39
42
|
|
40
43
|
# Generates all caches for this category.
|
data/lib/picky/loader.rb
CHANGED
@@ -44,10 +44,6 @@ module Loader
|
|
44
44
|
# Load the user's application.
|
45
45
|
#
|
46
46
|
def self.load_application
|
47
|
-
# Load the user's application.
|
48
|
-
#
|
49
|
-
exclaim 'Loading Application.'
|
50
|
-
|
51
47
|
# Add lib dir to load path.
|
52
48
|
#
|
53
49
|
# add_lib_dir
|
@@ -72,13 +68,15 @@ module Loader
|
|
72
68
|
|
73
69
|
# Finalize the applications.
|
74
70
|
#
|
71
|
+
# TODO Problem: Reload Routes.
|
72
|
+
#
|
75
73
|
Application.finalize_apps
|
76
74
|
|
77
75
|
# TODO Rewrite
|
78
76
|
#
|
79
77
|
Query::Qualifiers.instance.prepare
|
80
78
|
|
81
|
-
exclaim "Application loaded."
|
79
|
+
exclaim "Application #{Application.apps.map(&:name).join(', ')} loaded."
|
82
80
|
end
|
83
81
|
|
84
82
|
# Loads the framework.
|
@@ -4,13 +4,13 @@ module Query
|
|
4
4
|
#
|
5
5
|
class Allocation
|
6
6
|
|
7
|
-
attr_reader
|
8
|
-
|
9
|
-
|
7
|
+
attr_reader :count, :ids, :score, :combinations, :result_type
|
8
|
+
|
10
9
|
#
|
11
10
|
#
|
12
|
-
def initialize combinations
|
11
|
+
def initialize combinations, result_type
|
13
12
|
@combinations = combinations
|
13
|
+
@result_type = result_type
|
14
14
|
end
|
15
15
|
|
16
16
|
def hash
|
@@ -24,7 +24,7 @@ module Query
|
|
24
24
|
# Scores its combinations and caches the result.
|
25
25
|
#
|
26
26
|
def calculate_score weights
|
27
|
-
@score
|
27
|
+
@score ||= @combinations.calculate_score(weights)
|
28
28
|
end
|
29
29
|
|
30
30
|
# Asks the combinations for the (intersected) ids.
|
@@ -61,7 +61,7 @@ module Query
|
|
61
61
|
# Transform the allocation into result form.
|
62
62
|
#
|
63
63
|
def to_result
|
64
|
-
[self.result_type, self.score, self.count, @combinations.to_result, self.ids] if
|
64
|
+
[self.result_type, self.score, self.count, @combinations.to_result, self.ids] if self.count > 0
|
65
65
|
end
|
66
66
|
|
67
67
|
# Json representation of this allocation.
|
data/lib/picky/query/base.rb
CHANGED
@@ -15,9 +15,9 @@ module Query
|
|
15
15
|
def initialize *index_types
|
16
16
|
options = Hash === index_types.last ? index_types.pop : {}
|
17
17
|
@index_types = index_types
|
18
|
-
@weigher = Weigher.new
|
19
|
-
@tokenizer =
|
20
|
-
@weights =
|
18
|
+
@weigher = options[:weigher] || Weigher.new(index_types)
|
19
|
+
@tokenizer = options[:tokenizer] || Tokenizers::Default::Query
|
20
|
+
@weights = options[:weights] || Weights.new
|
21
21
|
end
|
22
22
|
|
23
23
|
# Convenience method.
|
@@ -63,13 +63,21 @@ module Query
|
|
63
63
|
#
|
64
64
|
# TODO Smallify.
|
65
65
|
#
|
66
|
+
# TODO Rename: allocations
|
67
|
+
#
|
66
68
|
def sorted_allocations tokens
|
67
69
|
# Get the allocations.
|
68
70
|
#
|
71
|
+
# TODO Pass in reduce_to_amount (aka max_allocations)
|
72
|
+
#
|
73
|
+
# TODO uniq, score, sort in there
|
74
|
+
#
|
69
75
|
allocations = @weigher.allocations_for tokens
|
70
76
|
|
71
77
|
# Callbacks.
|
72
78
|
#
|
79
|
+
# TODO Reduce before sort?
|
80
|
+
#
|
73
81
|
reduce allocations
|
74
82
|
remove_from allocations
|
75
83
|
|
@@ -99,12 +107,12 @@ module Query
|
|
99
107
|
def remove_from allocations
|
100
108
|
allocations.remove(identifiers_to_remove) if remove_identifiers?
|
101
109
|
end
|
102
|
-
# Override.
|
110
|
+
# Override. TODO No, redesign.
|
103
111
|
#
|
104
112
|
def identifiers_to_remove
|
105
|
-
@identifiers_to_remove
|
113
|
+
@identifiers_to_remove ||= []
|
106
114
|
end
|
107
|
-
|
115
|
+
|
108
116
|
# Packs the sorted allocations into results.
|
109
117
|
#
|
110
118
|
# This generates the id intersections. Lots of work going on.
|
@@ -9,13 +9,13 @@ module Query
|
|
9
9
|
#
|
10
10
|
class Combination
|
11
11
|
|
12
|
-
attr_reader :token, :bundle
|
12
|
+
attr_reader :token, :bundle, :category_name
|
13
13
|
|
14
14
|
def initialize token, category
|
15
|
-
@token
|
16
|
-
@
|
17
|
-
@bundle
|
18
|
-
@text
|
15
|
+
@token = token
|
16
|
+
@category_name = category.name
|
17
|
+
@bundle = category.bundle_for token
|
18
|
+
@text = @token.text # don't want to use reset_similar already
|
19
19
|
end
|
20
20
|
|
21
21
|
# Note: Required for uniq!
|
@@ -26,20 +26,24 @@ module Query
|
|
26
26
|
|
27
27
|
# Returns the weight of this combination.
|
28
28
|
#
|
29
|
+
# TODO Really cache?
|
30
|
+
#
|
29
31
|
def weight
|
30
|
-
@weight
|
32
|
+
@weight ||= @bundle.weight(@text)
|
31
33
|
end
|
32
34
|
|
33
35
|
# Returns an array of ids for the given text.
|
34
36
|
#
|
37
|
+
# TODO Really cache?
|
38
|
+
#
|
35
39
|
def ids
|
36
|
-
@ids
|
40
|
+
@ids ||= @bundle.ids(@text)
|
37
41
|
end
|
38
42
|
|
39
43
|
# The identifier for this combination.
|
40
44
|
#
|
41
45
|
def identifier
|
42
|
-
@
|
46
|
+
@category_name
|
43
47
|
end
|
44
48
|
|
45
49
|
# Is the identifier in the given identifiers?
|
@@ -62,7 +66,7 @@ module Query
|
|
62
66
|
# "exact title:Peter*:peter"
|
63
67
|
#
|
64
68
|
def to_s
|
65
|
-
"#{bundle.
|
69
|
+
"#{bundle.identifier} #{to_result.join(':')}"
|
66
70
|
end
|
67
71
|
|
68
72
|
end
|
@@ -2,16 +2,15 @@ module Query
|
|
2
2
|
|
3
3
|
# Combinations are a number of Combination-s.
|
4
4
|
#
|
5
|
-
# They are
|
5
|
+
# They are the core of an allocation.
|
6
6
|
#
|
7
7
|
class Combinations
|
8
8
|
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :combinations
|
10
10
|
|
11
11
|
delegate :empty?, :to => :@combinations
|
12
12
|
|
13
|
-
def initialize
|
14
|
-
@type = type # TODO Remove.
|
13
|
+
def initialize combinations = []
|
15
14
|
@combinations = combinations
|
16
15
|
end
|
17
16
|
|
@@ -22,13 +21,12 @@ module Query
|
|
22
21
|
# Uses user specific weights to calculate a score for the combinations.
|
23
22
|
#
|
24
23
|
def calculate_score weights
|
25
|
-
|
26
|
-
@score + add_score(weights) # TODO Ok to just cache the weights?
|
24
|
+
total_score + weighted_score(weights)
|
27
25
|
end
|
28
|
-
def
|
26
|
+
def total_score
|
29
27
|
@combinations.sum &:weight
|
30
28
|
end
|
31
|
-
def
|
29
|
+
def weighted_score weights
|
32
30
|
weights.score @combinations
|
33
31
|
end
|
34
32
|
|
@@ -58,20 +56,18 @@ module Query
|
|
58
56
|
# this precondition for a fast algorithm is always given.
|
59
57
|
#
|
60
58
|
id_arrays.sort! { |this_array, that_array| this_array.size <=> that_array.size }
|
61
|
-
|
59
|
+
|
62
60
|
# Call the optimized C algorithm.
|
63
61
|
#
|
64
62
|
Performant::Array.memory_efficient_intersect id_arrays
|
65
63
|
end
|
66
|
-
|
64
|
+
|
65
|
+
# Wrap the combinations into an allocation with the result_type
|
67
66
|
#
|
68
|
-
|
69
|
-
|
70
|
-
allocation = Allocation.new self
|
71
|
-
allocation.result_type = @type.result_type # TODO Rewrite.
|
72
|
-
allocation
|
67
|
+
def pack_into_allocation result_type
|
68
|
+
Allocation.new self, result_type
|
73
69
|
end
|
74
|
-
|
70
|
+
|
75
71
|
# Filters the tokens and identifiers such that only identifiers
|
76
72
|
# that are passed in, remain, including their tokens.
|
77
73
|
#
|
data/lib/picky/query/weigher.rb
CHANGED
@@ -22,16 +22,12 @@ module Query
|
|
22
22
|
|
23
23
|
# Optimization for ignoring tokens that allocate to nothing and
|
24
24
|
# can be ignored.
|
25
|
-
# For example in a
|
25
|
+
# For example in a special search, where "florian" is not
|
26
26
|
# mapped to city, zip, or category.
|
27
27
|
#
|
28
28
|
possible_combinations.compact!
|
29
29
|
expanded_combinations = expand_combinations_from possible_combinations
|
30
30
|
|
31
|
-
# TODO Rewrite.
|
32
|
-
#
|
33
|
-
# expanded_combinations.map! { |expanded_combination| Combinations.new(index, expanded_combination) }
|
34
|
-
|
35
31
|
#
|
36
32
|
#
|
37
33
|
next previous_allocations if expanded_combinations.empty?
|
@@ -51,15 +47,22 @@ module Query
|
|
51
47
|
# [c,e]
|
52
48
|
# ]
|
53
49
|
#
|
54
|
-
expanded_combinations = expanded_combinations.shift.zip
|
50
|
+
expanded_combinations = expanded_combinations.shift.zip *expanded_combinations
|
55
51
|
|
56
52
|
# Wrap into a real combination.
|
57
53
|
#
|
58
|
-
expanded_combinations.map! { |expanded_combination| Combinations.new(
|
54
|
+
# expanded_combinations.map! { |expanded_combination| Combinations.new(expanded_combination).pack_into_allocation(index.result_type) }
|
59
55
|
|
60
56
|
# Add the possible allocations to the ones we already have.
|
61
57
|
#
|
62
|
-
previous_allocations + expanded_combinations.map(&:pack_into_allocation)
|
58
|
+
# previous_allocations + expanded_combinations.map(&:pack_into_allocation)
|
59
|
+
|
60
|
+
|
61
|
+
# Add the wrapped possible allocations to the ones we already have.
|
62
|
+
#
|
63
|
+
previous_allocations + expanded_combinations.map! do |expanded_combination|
|
64
|
+
Combinations.new(expanded_combination).pack_into_allocation(index.result_type) # TODO Do not extract result_type. Remove pack_into_allocation.
|
65
|
+
end
|
63
66
|
end)
|
64
67
|
end
|
65
68
|
|
data/lib/picky/query/weights.rb
CHANGED
@@ -35,20 +35,17 @@ module Query
|
|
35
35
|
#
|
36
36
|
# Just kidding. It's far more complicated than that. Ha ha ha ha ;)
|
37
37
|
#
|
38
|
-
|
38
|
+
# Note: Cache this if more complicated weighings become necessary.
|
39
|
+
#
|
39
40
|
def score combinations
|
40
|
-
# TODO
|
41
|
+
# TODO Beautify?
|
41
42
|
#
|
42
|
-
|
43
|
+
# weight_for combinations.map(&:category).clustered_uniq_fast.map!(&:name)
|
43
44
|
|
44
|
-
#
|
45
|
-
# mapping is not necessary anymore.
|
45
|
+
# TODO combinations could cluster uniq as combinations are added (since combinations don't change).
|
46
46
|
#
|
47
|
-
|
48
|
-
categories.map! &:name
|
49
|
-
weight_for categories
|
50
|
-
end
|
47
|
+
weight_for combinations.map(&:category_name).clustered_uniq_fast
|
51
48
|
end
|
52
|
-
|
49
|
+
|
53
50
|
end
|
54
51
|
end
|
data/lib/picky/routing.rb
CHANGED
@@ -142,4 +142,15 @@ class Routing
|
|
142
142
|
String === url ? %r{#{url}} : url
|
143
143
|
end
|
144
144
|
|
145
|
+
# TODO Beautify.
|
146
|
+
#
|
147
|
+
def to_s
|
148
|
+
routes.instance_variable_get(:@routes).map do |route|
|
149
|
+
path_info = route.conditions[:path_info]
|
150
|
+
anchored = Rack::Mount::Utils.regexp_anchored?(path_info)
|
151
|
+
anchored_ok = anchored ? "\u2713" : " "
|
152
|
+
"#{anchored_ok} #{path_info.source}"
|
153
|
+
end.join "\n"
|
154
|
+
end
|
155
|
+
|
145
156
|
end
|
data/lib/tasks/application.rake
CHANGED
@@ -17,7 +17,25 @@ describe Array do
|
|
17
17
|
left.should be_empty
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
|
+
describe "clustered_uniq_fast" do
|
22
|
+
it "should generate a new array" do
|
23
|
+
ary = [:test1, :test2, :test1]
|
24
|
+
ary.clustered_uniq_fast.object_id.should_not == ary.object_id
|
25
|
+
end
|
26
|
+
it "should not change clusteredly unique arrays" do
|
27
|
+
[:test1, :test2, :test1].clustered_uniq_fast.should == [:test1, :test2, :test1]
|
28
|
+
end
|
29
|
+
it "should not skip interspersed elements" do
|
30
|
+
[:test1, :test1, :test2, :test1].clustered_uniq_fast.should == [:test1, :test2, :test1]
|
31
|
+
end
|
32
|
+
it "should work like uniq if no interspersed elements exist" do
|
33
|
+
[:test1, :test1, :test2, :test2, :test3].clustered_uniq_fast.should == [:test1, :test2, :test3]
|
34
|
+
end
|
35
|
+
it "is fast" do
|
36
|
+
performance_of { [:test1, :test1, :test2, :test2, :test3].clustered_uniq_fast }.should < 0.00001
|
37
|
+
end
|
38
|
+
end
|
21
39
|
describe "clustered_uniq" do
|
22
40
|
it "should generate a new array" do
|
23
41
|
ary = [:test1, :test2, :test1]
|
@@ -32,6 +50,9 @@ describe Array do
|
|
32
50
|
it "should work like uniq if no interspersed elements exist" do
|
33
51
|
[:test1, :test1, :test2, :test2, :test3].clustered_uniq.should == [:test1, :test2, :test3]
|
34
52
|
end
|
53
|
+
it "is fast" do
|
54
|
+
performance_of { [:test1, :test1, :test2, :test2, :test3].clustered_uniq }.should < 0.00001
|
55
|
+
end
|
35
56
|
end
|
36
57
|
|
37
58
|
end
|
@@ -15,6 +15,7 @@ describe Index::Category do
|
|
15
15
|
|
16
16
|
@partial = stub :partial, :dump => nil
|
17
17
|
@category.stub! :partial => @partial
|
18
|
+
@category.stub! :exclaim
|
18
19
|
end
|
19
20
|
|
20
21
|
describe 'dump_caches' do
|
@@ -179,9 +180,6 @@ describe Index::Category do
|
|
179
180
|
end
|
180
181
|
|
181
182
|
describe 'generate_caches' do
|
182
|
-
before(:each) do
|
183
|
-
@category.stub! :exclaim
|
184
|
-
end
|
185
183
|
it 'should call three method in order' do
|
186
184
|
@category.should_receive(:generate_caches_from_source).once.with().ordered
|
187
185
|
@category.should_receive(:generate_partial).once.with().ordered
|
@@ -4,7 +4,7 @@ describe Query::Allocation do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
@combinations = stub :combinations
|
7
|
-
@allocation = Query::Allocation.new @combinations
|
7
|
+
@allocation = Query::Allocation.new @combinations, :some_result_type
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "eql?" do
|
@@ -30,12 +30,11 @@ describe Query::Allocation do
|
|
30
30
|
context "allocation.count > 0" do
|
31
31
|
before(:each) do
|
32
32
|
@allocation.stub! :count => 10
|
33
|
-
@allocation.stub! :result_type => :result_type
|
34
33
|
@allocation.stub! :score => :score
|
35
34
|
@allocation.stub! :ids => :ids
|
36
35
|
end
|
37
36
|
it "represents correctly" do
|
38
|
-
@allocation.to_s.should == "Allocation:
|
37
|
+
@allocation.to_s.should == "Allocation: some_result_type, score, 10, combinations_result, ids"
|
39
38
|
end
|
40
39
|
end
|
41
40
|
end
|
@@ -105,9 +104,8 @@ describe Query::Allocation do
|
|
105
104
|
describe 'to_result' do
|
106
105
|
context 'with few combinations' do
|
107
106
|
before(:each) do
|
108
|
-
@allocation = Query::Allocation.new stub(:combinations, :ids => [1,2,3], :to_result => [:some_result])
|
107
|
+
@allocation = Query::Allocation.new stub(:combinations, :ids => [1,2,3], :to_result => [:some_result]), :some_result_type
|
109
108
|
@allocation.instance_variable_set :@score, :some_score
|
110
|
-
@allocation.result_type = :some_result_type
|
111
109
|
end
|
112
110
|
context 'with ids' do
|
113
111
|
it 'should output an array of information' do
|
@@ -120,9 +118,8 @@ describe Query::Allocation do
|
|
120
118
|
context 'with results' do
|
121
119
|
before(:each) do
|
122
120
|
combinations = stub :combinations, :ids => [1,2,3], :to_result => [:some_result1, :some_result2]
|
123
|
-
@allocation = Query::Allocation.new combinations
|
121
|
+
@allocation = Query::Allocation.new combinations, :some_result_type
|
124
122
|
@allocation.instance_variable_set :@score, :some_score
|
125
|
-
@allocation.result_type = :some_result_type
|
126
123
|
end
|
127
124
|
context 'with ids' do
|
128
125
|
it 'should output an array of information' do
|
@@ -134,7 +131,7 @@ describe Query::Allocation do
|
|
134
131
|
end
|
135
132
|
context 'without results' do
|
136
133
|
before(:each) do
|
137
|
-
@allocation = Query::Allocation.new stub(:combinations, :ids => [], :to_result => [])
|
134
|
+
@allocation = Query::Allocation.new stub(:combinations, :ids => [], :to_result => []), :some_result_type
|
138
135
|
@allocation.instance_variable_set :@score, :some_score
|
139
136
|
end
|
140
137
|
it 'should return nil' do
|
@@ -147,9 +144,8 @@ describe Query::Allocation do
|
|
147
144
|
|
148
145
|
describe 'to_json' do
|
149
146
|
before(:each) do
|
150
|
-
@allocation = Query::Allocation.new stub(:combination, :ids => [1,2,3,4,5,6,7], :to_result => [:some_result1, :some_result2])
|
147
|
+
@allocation = Query::Allocation.new stub(:combination, :ids => [1,2,3,4,5,6,7], :to_result => [:some_result1, :some_result2]), :some_result_type
|
151
148
|
@allocation.instance_variable_set :@score, :some_score
|
152
|
-
@allocation.result_type = :some_result_type
|
153
149
|
end
|
154
150
|
it 'should output the correct json string' do
|
155
151
|
@allocation.process! 20, 0
|
@@ -168,9 +164,9 @@ describe Query::Allocation do
|
|
168
164
|
|
169
165
|
describe "<=>" do
|
170
166
|
it "should sort higher first" do
|
171
|
-
first = Query::Allocation.new []
|
167
|
+
first = Query::Allocation.new [], :some_result_type
|
172
168
|
first.instance_variable_set :@score, 20
|
173
|
-
second = Query::Allocation.new []
|
169
|
+
second = Query::Allocation.new [], :some_result_type
|
174
170
|
second.instance_variable_set :@score, 10
|
175
171
|
|
176
172
|
first.<=>(second).should == -1
|
@@ -179,11 +175,11 @@ describe Query::Allocation do
|
|
179
175
|
|
180
176
|
describe "sort!" do
|
181
177
|
it "should sort correctly" do
|
182
|
-
first = Query::Allocation.new :whatever
|
178
|
+
first = Query::Allocation.new :whatever, :some_result_type
|
183
179
|
first.instance_variable_set :@score, 20
|
184
|
-
second = Query::Allocation.new :whatever
|
180
|
+
second = Query::Allocation.new :whatever, :some_result_type
|
185
181
|
second.instance_variable_set :@score, 10
|
186
|
-
third = Query::Allocation.new :whatever
|
182
|
+
third = Query::Allocation.new :whatever, :some_result_type
|
187
183
|
third.instance_variable_set :@score, 5
|
188
184
|
|
189
185
|
allocations = [second, third, first]
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
describe 'Query::Combination' do
|
6
6
|
|
7
7
|
before(:each) do
|
8
|
-
@bundle = stub :bundle
|
8
|
+
@bundle = stub :bundle, :identifier => :bundle_name
|
9
9
|
@token = stub :token, :text => :some_text, :partial => false, :similar? => true
|
10
10
|
@category = stub :category, :bundle_for => @bundle, :name => :some_category_name
|
11
11
|
|
@@ -14,7 +14,6 @@ describe 'Query::Combination' do
|
|
14
14
|
|
15
15
|
describe "to_s" do
|
16
16
|
it "shows the combination's info" do
|
17
|
-
@bundle.stub! :name => :bundle_name
|
18
17
|
@token.stub! :to_result => :token_result
|
19
18
|
|
20
19
|
@combination.to_s.should == 'bundle_name some_category_name:token_result'
|
@@ -7,17 +7,15 @@ describe 'Query::Combinations' do
|
|
7
7
|
before(:each) do
|
8
8
|
@combinations_ary = stub :combinations_ary
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
@combinations = Query::Combinations.new type, @combinations_ary
|
10
|
+
@combinations = Query::Combinations.new @combinations_ary
|
13
11
|
end
|
14
12
|
|
15
13
|
describe "pack_into_allocation" do
|
16
14
|
it "return an Allocation" do
|
17
|
-
@combinations.pack_into_allocation.should be_kind_of(Query::Allocation)
|
15
|
+
@combinations.pack_into_allocation(:some_result_type).should be_kind_of(Query::Allocation)
|
18
16
|
end
|
19
17
|
it "returns an Allocation with specific result_type" do
|
20
|
-
@combinations.pack_into_allocation.result_type.should == :
|
18
|
+
@combinations.pack_into_allocation(:some_result_type).result_type.should == :some_result_type
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
@@ -28,54 +26,54 @@ describe 'Query::Combinations' do
|
|
28
26
|
|
29
27
|
@combinations_ary = [@combination1, @combination2]
|
30
28
|
|
31
|
-
@combinations = Query::Combinations.new
|
29
|
+
@combinations = Query::Combinations.new @combinations_ary
|
32
30
|
end
|
33
31
|
it "resultifies the combinations" do
|
34
32
|
@combinations.to_result.should == [:result1, :result2]
|
35
33
|
end
|
36
34
|
end
|
37
35
|
|
38
|
-
describe "
|
36
|
+
describe "weighted_score" do
|
39
37
|
it "uses the weights' score method" do
|
40
38
|
weights = stub :weights
|
41
39
|
weights.should_receive(:score).once.with @combinations_ary
|
42
40
|
|
43
|
-
@combinations.
|
41
|
+
@combinations.weighted_score weights
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
47
|
-
describe "
|
45
|
+
describe "total_score" do
|
48
46
|
before(:each) do
|
49
47
|
@combination1 = stub :combination1, :weight => 3.14
|
50
48
|
@combination2 = stub :combination2, :weight => 2.76
|
51
49
|
|
52
50
|
@combinations_ary = [@combination1, @combination2]
|
53
51
|
|
54
|
-
@combinations = Query::Combinations.new
|
52
|
+
@combinations = Query::Combinations.new @combinations_ary
|
55
53
|
end
|
56
54
|
it "sums the scores" do
|
57
|
-
@combinations.
|
55
|
+
@combinations.total_score.should == 5.90
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
59
|
describe "calculate_score" do
|
62
60
|
before(:each) do
|
63
|
-
@combinations.stub! :
|
64
|
-
@combinations.stub! :
|
61
|
+
@combinations.stub! :total_score => 0
|
62
|
+
@combinations.stub! :weighted_score => 0
|
65
63
|
end
|
66
64
|
it "first sums, then weighs" do
|
67
|
-
@combinations.should_receive(:
|
68
|
-
@combinations.should_receive(:
|
65
|
+
@combinations.should_receive(:total_score).once.ordered.and_return 0
|
66
|
+
@combinations.should_receive(:weighted_score).once.ordered.and_return 0
|
69
67
|
|
70
68
|
@combinations.calculate_score :anything
|
71
69
|
end
|
72
70
|
it "calls sum_score" do
|
73
|
-
@combinations.should_receive(:
|
71
|
+
@combinations.should_receive(:total_score).once.with.and_return 0
|
74
72
|
|
75
73
|
@combinations.calculate_score :anything
|
76
74
|
end
|
77
75
|
it "calls sum_score" do
|
78
|
-
@combinations.should_receive(:
|
76
|
+
@combinations.should_receive(:weighted_score).once.with(:weights).and_return 0
|
79
77
|
|
80
78
|
@combinations.calculate_score :weights
|
81
79
|
end
|
@@ -95,7 +93,7 @@ describe 'Query::Combinations' do
|
|
95
93
|
@combination2 = stub :combination2, :in? => true
|
96
94
|
@combination3 = stub :combination3, :in? => true
|
97
95
|
|
98
|
-
@combinations = Query::Combinations.new
|
96
|
+
@combinations = Query::Combinations.new [@combination1, @combination2, @combination3]
|
99
97
|
end
|
100
98
|
it 'should remove the combinations' do
|
101
99
|
@combinations.remove([:any]).should == [@combination1]
|
@@ -108,7 +106,7 @@ describe 'Query::Combinations' do
|
|
108
106
|
@combination2 = stub :combination2, :in? => true
|
109
107
|
@combination3 = stub :combination3, :in? => true
|
110
108
|
|
111
|
-
@combinations = Query::Combinations.new
|
109
|
+
@combinations = Query::Combinations.new [@combination1, @combination2, @combination3]
|
112
110
|
end
|
113
111
|
it 'should filter the combinations' do
|
114
112
|
@combinations.keep([:any]).should == [@combination2, @combination3]
|
@@ -120,7 +118,7 @@ describe 'Query::Combinations' do
|
|
120
118
|
@combination1 = stub :combination1
|
121
119
|
@combination2 = stub :combination2
|
122
120
|
@combination3 = stub :combination3
|
123
|
-
@combinations = Query::Combinations.new
|
121
|
+
@combinations = Query::Combinations.new [@combination1, @combination2, @combination3]
|
124
122
|
end
|
125
123
|
it "should intersect correctly" do
|
126
124
|
@combination1.should_receive(:ids).once.with.and_return (1..100_000).to_a
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 4
|
9
|
+
version: 0.9.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Florian Hanke
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-30 00:00:00 +02:00
|
18
18
|
default_executable: picky
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/tasks/cache.rake
|
130
130
|
- lib/tasks/framework.rake
|
131
131
|
- lib/tasks/index.rake
|
132
|
+
- lib/tasks/routes.rake
|
132
133
|
- lib/tasks/server.rake
|
133
134
|
- lib/tasks/shortcuts.rake
|
134
135
|
- lib/tasks/solr.rake
|