inferx 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'inferx/category/complementary'
3
+
4
+ describe Inferx::Category::Complementary, '#ready_to_inject' do
5
+ it 'calls #inject with the words to inject block' do
6
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
7
+ category.should_receive(:inject).with(%w(word1 word2 word3))
8
+
9
+ category.ready_to_inject do |inject|
10
+ inject[%w(word1)]
11
+ inject[%w(word2)]
12
+ inject[%w(word3)]
13
+ end
14
+ end
15
+ end
16
+
17
+ describe Inferx::Category::Complementary, '#ready_to_eject' do
18
+ it 'calls #eject with the words to eject block' do
19
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
20
+ category.should_receive(:eject).with(%w(word1 word2 word3))
21
+
22
+ category.ready_to_eject do |eject|
23
+ eject[%w(word1)]
24
+ eject[%w(word2)]
25
+ eject[%w(word3)]
26
+ end
27
+ end
28
+ end
29
+
30
+ describe Inferx::Category::Complementary, '#train' do
31
+ before do
32
+ @filtered_categories = categories_stub do |s|
33
+ s.stub!(:inject => {'red' => 5})
34
+ end
35
+
36
+ @categories = categories_stub do |s|
37
+ s.stub!(:except => @filtered_categories)
38
+ end
39
+
40
+ @category = described_class.new(redis_stub, @categories, 'red', 2)
41
+ end
42
+
43
+ it 'calls Inferx::Categories#except with the category name' do
44
+ @categories.should_receive(:except).with('red').and_return(@filtered_categories)
45
+ @category.train(%w(apple strawberry apple strawberry strawberry))
46
+ end
47
+
48
+ it 'calls Inferx::Categories#inject with the words' do
49
+ @filtered_categories.should_receive(:inject).with(%w(apple strawberry apple strawberry strawberry)).and_return('red' => 5)
50
+ @category.train(%w(apple strawberry apple strawberry strawberry))
51
+ end
52
+ end
53
+
54
+ describe Inferx::Category::Complementary, '#untrain' do
55
+ before do
56
+ @filtered_categories = categories_stub do |s|
57
+ s.stub!(:eject => {'red' => 5})
58
+ end
59
+
60
+ @categories = categories_stub do |s|
61
+ s.stub!(:except => @filtered_categories)
62
+ end
63
+
64
+ @category = described_class.new(redis_stub, @categories, 'red', 7)
65
+ end
66
+
67
+ it 'calls Inferx::Categories#except with the category name' do
68
+ @categories.should_receive(:except).with('red').and_return(@filtered_categories)
69
+ @category.untrain(%w(apple strawberry apple strawberry strawberry))
70
+ end
71
+
72
+ it 'calls Inferx::Categories#eject with the words' do
73
+ @filtered_categories.should_receive(:eject).with(%w(apple strawberry apple strawberry strawberry)).and_return('red' => 5)
74
+ @category.untrain(%w(apple strawberry apple strawberry strawberry))
75
+ end
76
+ end
@@ -2,21 +2,18 @@ require 'spec_helper'
2
2
  require 'inferx/category'
3
3
 
4
4
  describe Inferx::Category, '#initialize' do
5
- it 'calls Inferx::Adapter#initialize' do
6
- redis = redis_stub
7
- category = described_class.new(redis, 'red', 2, :namespace => 'example', :manual => true)
8
- category.instance_eval { @redis }.should == redis
9
- category.instance_eval { @namespace }.should == 'example'
10
- category.should be_manual
5
+ it 'sets key for access to training data of the category to key attribute' do
6
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
7
+ category.key.should == 'inferx:categories:red'
11
8
  end
12
9
 
13
- it 'sets the category name to the name attribute' do
14
- category = described_class.new(redis_stub, 'red', 2)
10
+ it 'sets the category name to name attribute' do
11
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
15
12
  category.name.should == 'red'
16
13
  end
17
14
 
18
- it 'sets the size to the size attribute' do
19
- category = described_class.new(redis_stub, 'red', 2)
15
+ it 'sets the size to size attribute' do
16
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
20
17
  category.size.should == 2
21
18
  end
22
19
  end
@@ -27,18 +24,29 @@ describe Inferx::Category, '#all' do
27
24
  s.should_receive(:zrevrange).with('inferx:categories:red', 0, -1, :withscores => true).and_return([])
28
25
  end
29
26
 
30
- category = described_class.new(redis, 'red', 2)
27
+ category = described_class.new(redis, categories_stub, 'red', 2)
31
28
  category.all
32
29
  end
33
30
 
34
31
  it 'returns the words with the score' do
35
32
  redis = redis_stub do |s|
36
- s.stub!(:zrevrange).with('inferx:categories:red', 0, -1, :withscores => true).and_return(%w(apple 2 strawberry 3))
33
+ s.stub!(:zrevrange).with('inferx:categories:red', 0, -1, :withscores => true).and_return([['apple', 2.0], ['strawberry', 3.0]])
37
34
  end
38
35
 
39
- category = described_class.new(redis, 'red', 2)
36
+ category = described_class.new(redis, categories_stub, 'red', 2)
40
37
  category.all.should == {'apple' => 2, 'strawberry' => 3}
41
38
  end
39
+
40
+ context 'when return old format' do
41
+ it 'returns the words with the score' do
42
+ redis = redis_stub do |s|
43
+ s.stub!(:zrevrange).with('inferx:categories:red', 0, -1, :withscores => true).and_return(%w(apple 2 strawberry 3))
44
+ end
45
+
46
+ category = described_class.new(redis, categories_stub, 'red', 2)
47
+ category.all.should == {'apple' => 2, 'strawberry' => 3}
48
+ end
49
+ end
42
50
  end
43
51
 
44
52
  describe Inferx::Category, '#get' do
@@ -47,7 +55,7 @@ describe Inferx::Category, '#get' do
47
55
  s.should_receive(:zscore).with('inferx:categories:red', 'apple')
48
56
  end
49
57
 
50
- category = described_class.new(redis, 'red', 2)
58
+ category = described_class.new(redis, categories_stub, 'red', 2)
51
59
  category.get('apple')
52
60
  end
53
61
 
@@ -56,7 +64,7 @@ describe Inferx::Category, '#get' do
56
64
  s.stub!(:zscore).with('inferx:categories:red', 'apple').and_return('1')
57
65
  end
58
66
 
59
- category = described_class.new(redis, 'red', 2)
67
+ category = described_class.new(redis, categories_stub, 'red', 2)
60
68
  category.get('apple').should == 1
61
69
  end
62
70
 
@@ -66,65 +74,44 @@ describe Inferx::Category, '#get' do
66
74
  s.stub!(:zscore).with('inferx:categories:red', 'strawberry').and_return(nil)
67
75
  end
68
76
 
69
- category = described_class.new(redis, 'red', 2)
77
+ category = described_class.new(redis, categories_stub, 'red', 2)
70
78
  category.get('strawberry').should be_nil
71
79
  end
72
80
  end
73
81
  end
74
82
 
75
83
  describe Inferx::Category, '#train' do
76
- it 'calls Redis#zincrby and Redis#hincrby' do
77
- redis = redis_stub do |s|
78
- s.should_receive(:zincrby).with('inferx:categories:red', 2, 'apple')
79
- s.should_receive(:zincrby).with('inferx:categories:red', 3, 'strawberry')
80
- s.should_receive(:hincrby).with('inferx:categories', 'red', 5)
81
- s.should_receive(:save)
84
+ before do
85
+ @filtered_categories = categories_stub do |s|
86
+ s.stub!(:inject => {'red' => 5})
82
87
  end
83
88
 
84
- category = described_class.new(redis, 'red', 2)
85
- category.train(%w(apple strawberry apple strawberry strawberry))
86
- end
87
-
88
- it 'increases the size attribute' do
89
- redis = redis_stub do |s|
90
- s.stub!(:zincrby)
91
- s.stub!(:hincrby)
89
+ @categories = categories_stub do |s|
90
+ s.stub!(:filter => @filtered_categories)
92
91
  end
93
92
 
94
- category = described_class.new(redis, 'red', 2)
95
- category.train(%w(apple strawberry apple strawberry strawberry))
96
- category.size.should == 7
93
+ @category = described_class.new(redis_stub, @categories, 'red', 2)
97
94
  end
98
95
 
99
- context 'with no update' do
100
- it 'does not call Redis#hincrby' do
101
- redis = redis_stub do |s|
102
- s.should_not_receive(:hincrby)
103
- s.should_not_receive(:save)
104
- end
105
-
106
- category = described_class.new(redis, 'red', 2)
107
- category.train([])
108
- end
96
+ it 'calls Inferx::Categories#filter with the category name' do
97
+ @categories.should_receive(:filter).with('red').and_return(@filtered_categories)
98
+ @category.train(%w(apple strawberry apple strawberry strawberry))
109
99
  end
110
100
 
111
- context 'with manual save' do
112
- it 'does not call Redis#save' do
113
- redis = redis_stub do |s|
114
- s.stub!(:zincrby)
115
- s.stub!(:hincrby)
116
- s.should_not_receive(:save)
117
- end
101
+ it 'calls Inferx::Categories#inject with the words' do
102
+ @filtered_categories.should_receive(:inject).with(%w(apple strawberry apple strawberry strawberry)).and_return('red' => 5)
103
+ @category.train(%w(apple strawberry apple strawberry strawberry))
104
+ end
118
105
 
119
- category = described_class.new(redis, 'red', 2, :manual => true)
120
- category.train(%w(apple strawberry apple strawberry strawberry))
121
- end
106
+ it 'increases size attribute' do
107
+ @category.train(%w(apple strawberry apple strawberry strawberry))
108
+ @category.size.should == 7
122
109
  end
123
110
  end
124
111
 
125
112
  describe Inferx::Category, '#ready_to_train' do
126
113
  it 'calls #train with the words to train block' do
127
- category = described_class.new(redis_stub, 'red', 2)
114
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
128
115
  category.should_receive(:train).with(%w(word1 word2 word3))
129
116
 
130
117
  category.ready_to_train do |train|
@@ -136,83 +123,37 @@ describe Inferx::Category, '#ready_to_train' do
136
123
  end
137
124
 
138
125
  describe Inferx::Category, '#untrain' do
139
- it 'calls Redis#zincrby, Redis#zremrangebyscore and Redis#hincrby' do
140
- redis = redis_stub do |s|
141
- s.stub!(:pipelined).and_return do |&block|
142
- block.call
143
- %w(3 -2 1)
144
- end
145
-
146
- s.should_receive(:zincrby).with('inferx:categories:red', -2, 'apple')
147
- s.should_receive(:zincrby).with('inferx:categories:red', -3, 'strawberry')
148
- s.should_receive(:zremrangebyscore).with('inferx:categories:red', '-inf', 0)
149
- s.should_receive(:hincrby).with('inferx:categories', 'red', -3)
150
- s.should_receive(:save)
126
+ before do
127
+ @filtered_categories = categories_stub do |s|
128
+ s.stub!(:eject => {'red' => 5})
151
129
  end
152
130
 
153
- category = described_class.new(redis, 'red', 7)
154
- category.untrain(%w(apple strawberry apple strawberry strawberry))
155
- end
156
-
157
- it 'decreases the size attribute' do
158
- redis = redis_stub do |s|
159
- s.stub!(:pipelined).and_return do |&block|
160
- block.call
161
- %w(3 -2 1)
162
- end
163
-
164
- s.stub!(:zincrby)
165
- s.stub!(:zremrangebyscore)
166
- s.stub!(:hincrby)
131
+ @categories = categories_stub do |s|
132
+ s.stub!(:filter => @filtered_categories)
167
133
  end
168
134
 
169
- category = described_class.new(redis, 'red', 7)
170
- category.untrain(%w(apple strawberry apple strawberry strawberry))
171
- category.size.should == 4
135
+ @category = described_class.new(redis_stub, @categories, 'red', 7)
172
136
  end
173
137
 
174
- context 'with no update' do
175
- it 'does not call Redis#zremrangebyscore and Redis#hincrby' do
176
- redis = redis_stub do |s|
177
- s.stub!(:pipelined).and_return do |&block|
178
- block.call
179
- %w(-2 -3 2)
180
- end
181
-
182
- s.stub!(:zincrby)
183
- s.stub!(:zremrangebyscore)
184
- s.should_not_receive(:hincrby)
185
- s.should_not_receive(:save)
186
- end
187
-
188
- category = described_class.new(redis, 'red', 7)
189
- category.untrain(%w(apple strawberry apple strawberry strawberry))
190
- end
138
+ it 'calls Inferx::Categories#filter with the category name' do
139
+ @categories.should_receive(:filter).with('red').and_return(@filtered_categories)
140
+ @category.untrain(%w(apple strawberry apple strawberry strawberry))
191
141
  end
192
142
 
193
- context 'with manual save' do
194
- it 'does not call Redis#save' do
195
- redis = redis_stub do |s|
196
- s.stub!(:pipelined).and_return do |&block|
197
- block.call
198
- %w(3 -2 1)
199
- end
200
-
201
- s.stub!(:zincrby)
202
- s.stub!(:zremrangebyscore)
203
- s.stub!(:hincrby)
204
- s.should_not_receive(:save)
205
- end
143
+ it 'calls Inferx::Categories#eject with the words' do
144
+ @filtered_categories.should_receive(:eject).with(%w(apple strawberry apple strawberry strawberry)).and_return('red' => 5)
145
+ @category.untrain(%w(apple strawberry apple strawberry strawberry))
146
+ end
206
147
 
207
- category = described_class.new(redis, 'red', 7, :manual => true)
208
- category.untrain(%w(apple strawberry apple strawberry strawberry))
209
- end
148
+ it 'decreases size attribute' do
149
+ @category.untrain(%w(apple strawberry apple strawberry strawberry))
150
+ @category.size.should == 2
210
151
  end
211
152
  end
212
153
 
213
154
  describe Inferx::Category, '#ready_to_untrain' do
214
155
  it 'calls #untrain with the words to untrain block' do
215
- category = described_class.new(redis_stub, 'red', 2)
156
+ category = described_class.new(redis_stub, categories_stub, 'red', 2)
216
157
  category.should_receive(:untrain).with(%w(word1 word2 word3))
217
158
 
218
159
  category.ready_to_untrain do |untrain|
@@ -230,7 +171,7 @@ describe Inferx::Category, '#scores' do
230
171
  s.should_receive(:zscore).with('inferx:categories:red', 'strawberry')
231
172
  end
232
173
 
233
- category = described_class.new(redis, 'red', 2)
174
+ category = described_class.new(redis, categories_stub, 'red', 2)
234
175
  category.scores(%w(apple strawberry))
235
176
  end
236
177
 
@@ -239,7 +180,7 @@ describe Inferx::Category, '#scores' do
239
180
  s.stub!(:pipelined => %w(2 3))
240
181
  end
241
182
 
242
- category = described_class.new(redis, 'red', 2)
183
+ category = described_class.new(redis, categories_stub, 'red', 2)
243
184
  scores = category.scores(%w(apple strawberry))
244
185
  scores.should == [2, 3]
245
186
  end
@@ -18,19 +18,11 @@ describe Inferx, '#initialize' do
18
18
  described_class.new(:namespace => 'example', :manual => true)
19
19
  end
20
20
 
21
- it "sets an instance of #{described_class}::Categories to the categories attribute" do
21
+ it "sets an instance of #{described_class}::Categories to categories attribute" do
22
22
  redis_stub
23
23
  inferx = described_class.new
24
24
  inferx.categories.should be_an(Inferx::Categories)
25
25
  end
26
-
27
- context 'with :complementary option' do
28
- it "sets an instance of #{described_class}::Complementary::Categories to the categories attribute" do
29
- redis_stub
30
- inferx = described_class.new(:complementary => true)
31
- inferx.categories.should be_an(Inferx::Complementary::Categories)
32
- end
33
- end
34
26
  end
35
27
 
36
28
  describe Inferx, '#score' do
@@ -6,3 +6,10 @@ def redis_stub
6
6
  Redis.stub!(:new => s) if defined? Redis
7
7
  end
8
8
  end
9
+
10
+ def categories_stub
11
+ stub.tap do |s|
12
+ s.stub!(:key => 'inferx:categories')
13
+ yield s if block_given?
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inferx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-23 00:00:00.000000000 Z
12
+ date: 2012-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -58,17 +58,13 @@ files:
58
58
  - Rakefile
59
59
  - inferx.gemspec
60
60
  - lib/inferx.rb
61
- - lib/inferx/adapter.rb
62
61
  - lib/inferx/categories.rb
63
62
  - lib/inferx/category.rb
64
- - lib/inferx/complementary/categories.rb
65
- - lib/inferx/complementary/category.rb
63
+ - lib/inferx/category/complementary.rb
66
64
  - lib/inferx/version.rb
67
- - spec/inferx/adapter_spec.rb
68
65
  - spec/inferx/categories_spec.rb
66
+ - spec/inferx/category/complementary_spec.rb
69
67
  - spec/inferx/category_spec.rb
70
- - spec/inferx/complementary/categories_spec.rb
71
- - spec/inferx/complementary/category_spec.rb
72
68
  - spec/inferx_spec.rb
73
69
  - spec/spec_helper.rb
74
70
  homepage: ''
@@ -96,11 +92,9 @@ signing_key:
96
92
  specification_version: 3
97
93
  summary: Naive Bayes classifier, the training data on Redis
98
94
  test_files:
99
- - spec/inferx/adapter_spec.rb
100
95
  - spec/inferx/categories_spec.rb
96
+ - spec/inferx/category/complementary_spec.rb
101
97
  - spec/inferx/category_spec.rb
102
- - spec/inferx/complementary/categories_spec.rb
103
- - spec/inferx/complementary/category_spec.rb
104
98
  - spec/inferx_spec.rb
105
99
  - spec/spec_helper.rb
106
100
  has_rdoc:
@@ -1,64 +0,0 @@
1
- class Inferx
2
- class Adapter
3
-
4
- # @param [Redis] redis an instance of Redis
5
- # @param [Hash] options
6
- # @option options [String] :namespace namespace of keys to be used to Redis
7
- # @option options [Boolean] :manual whether manual save, defaults to false
8
- def initialize(redis, options = {})
9
- @redis = redis
10
- @options = options
11
- @namespace = options[:namespace]
12
- @manual = !!options[:manual]
13
- end
14
-
15
- # Determine if manual save.
16
- #
17
- # @return [Boolean] whether or not manual save
18
- def manual?
19
- @manual
20
- end
21
-
22
- # Get the key for access to categories.
23
- #
24
- # @return [String] the key
25
- def categories_key
26
- @categories_key ||= make_categories_key
27
- end
28
-
29
- # Make the key for access to categories.
30
- #
31
- # @return [String] the key
32
- def make_categories_key
33
- parts = %w(inferx categories)
34
- parts.insert(1, @namespace) if @namespace
35
- parts.join(':')
36
- end
37
-
38
- # Make the key for access to scores stored each by word.
39
- #
40
- # @param [String] category_name a category name
41
- # @return [String] the key
42
- def make_category_key(category_name)
43
- "#{categories_key}:#{category_name}"
44
- end
45
-
46
- # Spawn an instance of any class.
47
- #
48
- # @param [Class] klass any class, constructor takes the instance of Redis to
49
- # first argument, and takes the options to last argument
50
- # @param [Array] args any arguments
51
- # @return [Object] a instance of the class
52
- def spawn(klass, *args)
53
- klass.new(@redis, *args, @options)
54
- end
55
-
56
- protected
57
-
58
- %w(hdel hexists hget hgetall hincrby hkeys hsetnx).each do |command|
59
- define_method(command) do |*args|
60
- @redis.__send__(command, categories_key, *args)
61
- end
62
- end
63
- end
64
- end