Skip to content

Commit

Permalink
Fix text
Browse files Browse the repository at this point in the history
  • Loading branch information
Takahiro Kondo committed Oct 27, 2012
1 parent acf1376 commit 6ad49e6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 52 deletions.
45 changes: 17 additions & 28 deletions lib/inferx/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(redis, options = {})
@except = Set.new
end

# Get key for access to the categories on Redis.
# Get key for access to categories on Redis.
#
# @attribute [r] key
# @return [String] the key
Expand Down Expand Up @@ -67,12 +67,12 @@ def all
all_in_visible.to_a
end

# Get a category according the name.
# Get category according the name.
#
# @param [String] category_name a category name
# @param [String] category_name the category name
# @return [Inferx::Category] the category
def get(category_name)
size = hget(category_name)
size = @redis.hget(@key, category_name)
raise ArgumentError, "#{category_name.inspect} is missing" unless size
raise ArgumentError, "#{category_name.inspect} does not exist in filtered categories" unless all_in_visible.include?(category_name.to_s)
make_category(category_name, size.to_i)
Expand All @@ -84,7 +84,7 @@ def get(category_name)
# @param [Array<String>] category_names category names
def add(*category_names)
@redis.pipelined do
category_names.each { |category_name| hsetnx(category_name, 0) }
category_names.each { |category_name| @redis.hsetnx(@key, category_name, 0) }
@redis.save unless manual?
end
end
Expand All @@ -94,28 +94,28 @@ def add(*category_names)
# @param [Array<String>] category_names category names
def remove(*category_names)
@redis.pipelined do
category_names.each { |category_name| hdel(category_name) }
category_names.each { |category_name| @redis.hdel(@key, category_name) }
@redis.del(*category_names.map(&method(:make_category_key)))
@redis.save unless manual?
end
end

# Determine if the category is defined.
#
# @param [String] category_name a category name
# @param [String] category_name the category name
# @return whether the category is defined
def exists?(category_name)
all_in_visible.include?(category_name.to_s)
end

# Apply process for each category.
#
# @yield a block to be called for every category
# @yield called for every category
# @yieldparam [Inferx::Category] category a category
def each
visible_category_names = all_in_visible

hgetall.each do |category_name, size|
@redis.hgetall(@key).each do |category_name, size|
next unless visible_category_names.include?(category_name)
yield make_category(category_name, size.to_i)
end
Expand All @@ -136,8 +136,9 @@ def inject(words)
associate(category_names, increase) do
@redis.pipelined do
category_names.each do |category_name|
words.each { |word, count| zincrby(category_name, count, word) }
hincrby(category_name, increase)
category_key = make_category_key(category_name)
words.each { |word, count| @redis.zincrby(category_key, count, word) }
@redis.hincrby(@key, category_name, increase)
end

@redis.save unless manual?
Expand All @@ -160,8 +161,9 @@ def eject(words)
associate(category_names, decrease) do |fluctuation|
all_scores = @redis.pipelined do
category_names.each do |category_name|
words.each { |word, count| zincrby(category_name, -count, word) }
zremrangebyscore(category_name, '-inf', 0)
category_key = make_category_key(category_name)
words.each { |word, count| @redis.zincrby(category_key, -count, word) }
@redis.zremrangebyscore(category_key, '-inf', 0)
end
end

Expand All @@ -179,7 +181,7 @@ def eject(words)

@redis.pipelined do
fluctuation.each do |category_name, decrease|
hincrby(category_name, -decrease)
@redis.hincrby(@key, category_name, -decrease)
end

@redis.save unless manual?
Expand All @@ -194,7 +196,7 @@ def filtered(&block)
end

def all_in_visible
all = Set.new(hkeys || [])
all = Set.new(@redis.hkeys(@key) || [])
all &= @filter if @filter
all - @except
end
Expand All @@ -220,18 +222,5 @@ def associate(keys, value, &block)
yield *(block.arity.zero? ? [] : [keys_and_values]) if block_given?
keys_and_values
end

%w(hdel hget hgetall hincrby hkeys hsetnx).each do |command|
define_method(command) do |*args|
@redis.__send__(command, @key, *args)
end
end

%w(zincrby zremrangebyscore).each do |command|
define_method(command) do |category_name, *args|
key = make_category_key(category_name)
@redis.__send__(command, key, *args)
end
end
end
end
35 changes: 15 additions & 20 deletions lib/inferx/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def initialize(redis, categories, name, size)
@size = size
end

# Get key for access to training data of the category.
# Get key for access to training data of the category on Redis.
#
# @attribute [r] key
# @return [String] the key
attr_reader :key

# Get a category name.
# Get name of the category.
#
# @attribute [r] name
# @return [String] a category name
# @return [String] the name
attr_reader :name

# Get total of scores.
Expand All @@ -43,7 +43,7 @@ def initialize(redis, categories, name, size)
#
# @return [Hash<String, Integer>] words with scores
def all
words_with_scores = zrevrange(0, -1, :withscores => true)
words_with_scores = @redis.zrevrange(@key, 0, -1, :withscores => true)

if !words_with_scores.empty? and words_with_scores.first.is_a?(Array)
words_with_scores.each { |pair| pair[1] = pair[1].to_i }
Expand All @@ -61,13 +61,13 @@ def all
end
end

# Get score of a word.
# Get score of the word.
#
# @param [String] word a word
# @param [String] word the word
# @return [Integer] when the word is member, score of the word
# @return [nil] when the word is not member
def get(word)
score = zscore(word)
score = @redis.zscore(@key, word)
score ? score.to_i : nil
end
alias [] get
Expand All @@ -76,8 +76,8 @@ def get(word)
#
# @param [Array<String>] words an array of words
def train(words)
increases = @categories.filter(name).inject(words)
@size += increases[name]
increases = @categories.filter(@name).inject(words)
@size += increases[@name]
end

# Prepare to enhance the training data. Use for high performance.
Expand All @@ -90,8 +90,8 @@ def train(words)
#
# @param [Array<String>] words an array of words
def untrain(words)
decreases = @categories.filter(name).eject(words)
@size -= decreases[name]
decreases = @categories.filter(@name).eject(words)
@size -= decreases[@name]
end

# Prepare to attenuate the training data giving words.
Expand All @@ -105,16 +105,11 @@ def untrain(words)
# @param [Array<String>] words an array of words
# @return [Array<Integer>] scores for each word
def scores(words)
scores = @redis.pipelined { words.map(&method(:zscore)) }
scores.map { |score| score ? score.to_i : nil }
end

private

%w(zrevrange zscore).each do |command|
define_method(command) do |*args|
@redis.__send__(command, @key, *args)
scores = @redis.pipelined do
words.map { |word| @redis.zscore(@key, word) }
end

scores.map { |score| score ? score.to_i : nil }
end
end
end
4 changes: 2 additions & 2 deletions lib/inferx/category/complementary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Complementary < Category
#
# @param [Array<String>] words an array of words
def train(words)
@categories.except(name).inject(words)
@categories.except(@name).inject(words)
end

# Attenuate the training data of other categories giving words.
#
# @param [Array<String>] words an array of words
def untrain(words)
@categories.except(name).eject(words)
@categories.except(@name).eject(words)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/inferx/categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
categories.get('red')
end

it 'calles Inferx::Category.new with the instance of Redis and the category name' do
it 'calles Inferx::Category.new with the instance of Redis, the categories, the category name and total of scores' do
categories = described_class.new(@redis)
Inferx::Category.should_receive(:new).with(@redis, categories, 'red', 2)
categories.get('red')
Expand Down
2 changes: 1 addition & 1 deletion spec/inferx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
described_class.new(:namespace => 'example', :manual => true)
end

it "sets an instance of #{described_class}::Categories to the categories attribute" do
it "sets an instance of #{described_class}::Categories to categories attribute" do
redis_stub
inferx = described_class.new
inferx.categories.should be_an(Inferx::Categories)
Expand Down

0 comments on commit 6ad49e6

Please sign in to comment.