Class: Inferx::Category
- Inherits:
-
Object
- Object
- Inferx::Category
- Defined in:
- lib/inferx/category.rb,
lib/inferx/category/complementary.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Complementary
Instance Attribute Summary collapse
-
#key ⇒ String
readonly
Get key for access to training data of the category on Redis.
-
#name ⇒ String
readonly
Get name of the category.
-
#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 the word.
-
#initialize(redis, categories, name, size) ⇒ 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.
Constructor Details
#initialize(redis, categories, name, size) ⇒ Category
Returns a new instance of Category.
16 17 18 19 20 21 22 |
# File 'lib/inferx/category.rb', line 16 def initialize(redis, categories, name, size) @redis = redis @categories = categories @key = "#{categories.key}:#{name}" @name = name.to_s @size = size end |
Instance Attribute Details
#key ⇒ String (readonly)
Get key for access to training data of the category on Redis.
28 29 30 |
# File 'lib/inferx/category.rb', line 28 def key @key end |
#name ⇒ String (readonly)
Get name of the category.
34 35 36 |
# File 'lib/inferx/category.rb', line 34 def name @name end |
#size ⇒ Integer (readonly)
Get total of scores.
40 41 42 |
# File 'lib/inferx/category.rb', line 40 def size @size end |
Class Method Details
.ready_for(method_name) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/inferx/category.rb', line 4 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/inferx/category.rb', line 45 def all 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 } Hash[words_with_scores] else 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 end |
#get(word) ⇒ Integer? Also known as: []
Get score of the word.
69 70 71 72 |
# File 'lib/inferx/category.rb', line 69 def get(word) score = @redis.zscore(@key, word) score ? score.to_i : nil end |
#scores(words) ⇒ Array<Integer>
Get effectively scores for each word.
107 108 109 110 111 112 113 |
# File 'lib/inferx/category.rb', line 107 def scores(words) scores = @redis.pipelined do words.map { |word| @redis.zscore(@key, word) } end scores.map { |score| score ? score.to_i : nil } end |
#train(words) ⇒ Object
Enhance the training data giving words.
78 79 80 81 |
# File 'lib/inferx/category.rb', line 78 def train(words) increases = @categories.filter(@name).inject(words) @size += increases[@name] end |
#untrain(words) ⇒ Object
Attenuate the training data giving words.
92 93 94 95 |
# File 'lib/inferx/category.rb', line 92 def untrain(words) decreases = @categories.filter(@name).eject(words) @size -= decreases[@name] end |