inferx 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -55,7 +55,7 @@ class Inferx
55
55
 
56
56
  protected
57
57
 
58
- %w(hdel hget hgetall hincrby hkeys hsetnx).each do |command|
58
+ %w(hdel hexists hget hgetall hincrby hkeys hsetnx).each do |command|
59
59
  define_method(command) do |*args|
60
60
  @redis.__send__(command, categories_key, *args)
61
61
  end
@@ -44,6 +44,14 @@ class Inferx
44
44
  end
45
45
  end
46
46
 
47
+ # Determine if the category is defined.
48
+ #
49
+ # @param [String] category_name a category name
50
+ # @return whether the category is defined
51
+ def exists?(category_name)
52
+ hexists(category_name)
53
+ end
54
+
47
55
  # Apply process for each category.
48
56
  #
49
57
  # @yield a block to be called for every category
@@ -3,6 +3,14 @@ require 'inferx/adapter'
3
3
  class Inferx
4
4
  class Category < Adapter
5
5
 
6
+ def self.ready_for(method_name)
7
+ define_method("ready_to_#{method_name}") do |&block|
8
+ all = []
9
+ block[lambda { |items| all += items }]
10
+ __send__(method_name, all)
11
+ end
12
+ end
13
+
6
14
  # @param [Redis] redis an instance of Redis
7
15
  # @param [String] name a category name
8
16
  # @param [Integer] size total of scores
@@ -75,9 +83,7 @@ class Inferx
75
83
  #
76
84
  # @yield [train] process something
77
85
  # @yieldparam [Proc] train enhance the training data giving words
78
- def ready_to_train(&process)
79
- train(aggregate(&process))
80
- end
86
+ ready_for :train
81
87
 
82
88
  # Attenuate the training data giving words.
83
89
  #
@@ -114,9 +120,7 @@ class Inferx
114
120
  #
115
121
  # @yield [untrain] process something
116
122
  # @yieldparam [Proc] untrain attenuate the training data giving words
117
- def ready_to_untrain(&process)
118
- untrain(aggregate(&process))
119
- end
123
+ ready_for :untrain
120
124
 
121
125
  # Get effectively scores for each word.
122
126
  #
@@ -143,11 +147,5 @@ class Inferx
143
147
  hash
144
148
  end
145
149
  end
146
-
147
- def aggregate
148
- all = []
149
- yield lambda { |items| all += items }
150
- all
151
- end
152
150
  end
153
151
  end
@@ -4,6 +4,32 @@ class Inferx
4
4
  module Complementary
5
5
  class Category < Inferx::Category
6
6
 
7
+ # Inject the words to the training data of the category.
8
+ #
9
+ # @param [Array<String>] words an array of words
10
+ alias inject train
11
+
12
+ # Eject the words from the training data of the category.
13
+ #
14
+ # @param [Array<String>] words an array of words
15
+ alias eject untrain
16
+
17
+ # Prepare to inject the words to the training data of the category. Use
18
+ # for high performance.
19
+ #
20
+ # @yield [train] process something
21
+ # @yieldparam [Proc] inject inject the words to the training data of the
22
+ # category
23
+ ready_for :inject
24
+
25
+ # Prepare to eject the words from the training data of the category. Use
26
+ # for high performance.
27
+ #
28
+ # @yield [train] process something
29
+ # @yieldparam [Proc] eject eject the words from the training data of the
30
+ # category
31
+ ready_for :eject
32
+
7
33
  # Enhance the training data of other categories giving words.
8
34
  #
9
35
  # @param [Array<String>] words an array of words
@@ -1,3 +1,3 @@
1
1
  class Inferx
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -151,6 +151,35 @@ describe Inferx::Categories, '#remove' do
151
151
  end
152
152
  end
153
153
 
154
+ describe Inferx::Categories, '#exists?' do
155
+ it 'calls Redis#hexists' do
156
+ redis = redis_stub.tap do |s|
157
+ s.should_receive(:hexists).with('inferx:categories', 'red')
158
+ end
159
+
160
+ categories = described_class.new(redis)
161
+ categories.exists?('red')
162
+ end
163
+
164
+ it 'returns true if the category is defined' do
165
+ redis = redis_stub.tap do |s|
166
+ s.stub!(:hexists => true)
167
+ end
168
+
169
+ categories = described_class.new(redis)
170
+ categories.should be_exists('red')
171
+ end
172
+
173
+ it 'returns false if the category is not defined' do
174
+ redis = redis_stub.tap do |s|
175
+ s.stub!(:hexists => false)
176
+ end
177
+
178
+ categories = described_class.new(redis)
179
+ categories.should_not be_exists('red')
180
+ end
181
+ end
182
+
154
183
  describe Inferx::Categories, '#each' do
155
184
  before do
156
185
  @redis = redis_stub do |s|
@@ -1,5 +1,31 @@
1
1
  require 'inferx/complementary/category'
2
2
 
3
+ describe Inferx::Complementary::Category, '#ready_to_inject' do
4
+ it 'calls #inject with the words to inject block' do
5
+ category = described_class.new(redis_stub, 'red', 2)
6
+ category.should_receive(:inject).with(%w(word1 word2 word3))
7
+
8
+ category.ready_to_inject do |inject|
9
+ inject[%w(word1)]
10
+ inject[%w(word2)]
11
+ inject[%w(word3)]
12
+ end
13
+ end
14
+ end
15
+
16
+ describe Inferx::Complementary::Category, '#ready_to_eject' do
17
+ it 'calls #eject with the words to eject block' do
18
+ category = described_class.new(redis_stub, 'red', 2)
19
+ category.should_receive(:eject).with(%w(word1 word2 word3))
20
+
21
+ category.ready_to_eject do |eject|
22
+ eject[%w(word1)]
23
+ eject[%w(word2)]
24
+ eject[%w(word3)]
25
+ end
26
+ end
27
+ end
28
+
3
29
  describe Inferx::Complementary::Category, '#train' do
4
30
  it 'calls Redis#zincrby and Redis#hincrby for other categories' do
5
31
  redis = redis_stub do |s|
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.2
4
+ version: 0.2.3
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-22 00:00:00.000000000 Z
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis