Class: Inferx::Category
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Integer
readonly
Get total of scores.
-
#size ⇒ Integer
readonly
Get total of scores.
Class Method Summary collapse
Instance Method Summary collapse
-
#all ⇒ Hash<String, Integer>
Get words with scores in the category.
-
#get(word) ⇒ Integer?
(also: #[])
Get score of a word.
-
#initialize(redis, name, size, options = {}) ⇒ Category
constructor
A new instance of Category.
-
#scores(words) ⇒ Array<Integer>
Get effectively scores for each word.
-
#train(words) ⇒ Object
Enhance the training data giving words.
-
#untrain(words) ⇒ Object
Attenuate the training data giving words.
Methods inherited from Adapter
#categories_key, #make_categories_key, #make_category_key, #manual?, #spawn
Constructor Details
#initialize(redis, name, size, options = {}) ⇒ Category
Returns a new instance of Category.
20 21 22 23 24 |
# File 'lib/inferx/category.rb', line 20 def initialize(redis, name, size, = {}) super(redis, ) @name = name.to_s @size = size end |
Instance Attribute Details
#name ⇒ Integer (readonly)
Get total of scores.
|
|
# File 'lib/inferx/category.rb', line 26
|
#size ⇒ Integer (readonly)
Get total of scores.
35 |
# File 'lib/inferx/category.rb', line 35 attr_reader :name, :size |
Class Method Details
.ready_for(method_name) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/inferx/category.rb', line 6 def self.ready_for(method_name) define_method("ready_to_#{method_name}") do |&block| all = [] block[lambda { |items| all += items }] __send__(method_name, all) end end |
Instance Method Details
#all ⇒ Hash<String, Integer>
Get words with scores in the category.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/inferx/category.rb', line 40 def all words_with_scores = zrevrange(0, -1, :withscores => true) index = 1 size = words_with_scores.size while index < size words_with_scores[index] = words_with_scores[index].to_i index += 2 end Hash[*words_with_scores] end |
#get(word) ⇒ Integer? Also known as: []
Get score of a word.
58 59 60 61 |
# File 'lib/inferx/category.rb', line 58 def get(word) score = zscore(word) score ? score.to_i : nil end |
#scores(words) ⇒ Array<Integer>
Get effectively scores for each word.
129 130 131 132 |
# File 'lib/inferx/category.rb', line 129 def scores(words) scores = @redis.pipelined { words.map(&method(:zscore)) } scores.map { |score| score ? score.to_i : nil } end |
#train(words) ⇒ Object
Enhance the training data giving words.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/inferx/category.rb', line 67 def train(words) return if words.empty? increase = words.size words = collect(words) @redis.pipelined do words.each { |word, count| zincrby(count, word) } hincrby(name, increase) @redis.save unless manual? end @size += increase end |
#untrain(words) ⇒ Object
Attenuate the training data giving words.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/inferx/category.rb', line 91 def untrain(words) return if words.empty? decrease = words.size words = collect(words) scores = @redis.pipelined do words.each { |word, count| zincrby(-count, word) } zremrangebyscore('-inf', 0) end length = words.size scores[0, length].each do |score| score = score.to_i decrease += score if score < 0 end return unless decrease > 0 @redis.pipelined do hincrby(name, -decrease) @redis.save unless manual? end @size -= decrease end |