Class: Inferx::Category

Inherits:
Adapter show all
Defined in:
lib/inferx/category.rb

Direct Known Subclasses

Inferx::Complementary::Category

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • redis (Redis)

    an instance of Redis

  • name (String)

    a category name

  • size (Integer)

    total of scores

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :namespace (String)

    namespace of keys to be used to Redis

  • :manual (Boolean)

    whether manual save, defaults to false



20
21
22
23
24
# File 'lib/inferx/category.rb', line 20

def initialize(redis, name, size, options = {})
  super(redis, options)
  @name = name.to_s
  @size = size
end

Instance Attribute Details

#nameInteger (readonly)

Get total of scores.

Returns:

  • (Integer)

    total of scores



# File 'lib/inferx/category.rb', line 26

#sizeInteger (readonly)

Get total of scores.

Returns:

  • (Integer)

    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

#allHash<String, Integer>

Get words with scores in the category.

Returns:

  • (Hash<String, Integer>)

    words with scores



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.

Parameters:

  • word (String)

    a word

Returns:

  • (Integer)

    when the word is member, score of the word

  • (nil)

    when the word is not member



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.

Parameters:

  • words (Array<String>)

    an array of words

Returns:

  • (Array<Integer>)

    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.

Parameters:

  • words (Array<String>)

    an array of 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.

Parameters:

  • words (Array<String>)

    an array of 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