Class: Inferx::Complementary::Category

Inherits:
Inferx::Category show all
Defined in:
lib/inferx/complementary/category.rb

Instance Attribute Summary

Attributes inherited from Inferx::Category

#name, #size

Instance Method Summary collapse

Methods inherited from Inferx::Category

#all, #get, #initialize, ready_for, #scores

Methods inherited from Adapter

#categories_key, #initialize, #make_categories_key, #make_category_key, #manual?, #spawn

Constructor Details

This class inherits a constructor from Inferx::Category

Instance Method Details

#ejectObject

Eject the words from the training data of the category.

Parameters:

  • words (Array<String>)

    an array of words



15
# File 'lib/inferx/complementary/category.rb', line 15

alias eject untrain

#injectObject

Inject the words to the training data of the category.

Parameters:

  • words (Array<String>)

    an array of words



10
# File 'lib/inferx/complementary/category.rb', line 10

alias inject train

#train(words) ⇒ Object

Enhance the training data of other categories giving words.

Parameters:

  • words (Array<String>)

    an array of words



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inferx/complementary/category.rb', line 36

def train(words)
  return if words.empty?

  increase = words.size
  words = collect(words)

  category_names = get_other_category_names
  return if category_names.empty?

  @redis.pipelined do
    category_names.each do |category_name|
      category_key = make_category_key(category_name)
      words.each { |word, count| @redis.zincrby(category_key, count, word) }
      hincrby(category_name, increase)
    end

    @redis.save unless manual?
  end
end

#untrain(words) ⇒ Object

Attenuate the training data of other categories giving words.

Parameters:

  • words (Array<String>)

    an array of words



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inferx/complementary/category.rb', line 59

def untrain(words)
  return if words.empty?

  decrease = words.size
  words = collect(words)

  category_names = get_other_category_names
  return if category_names.empty?

  scores = @redis.pipelined do
    category_names.each do |category_name|
      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

  length = words.size
  decreases_by_category = {}

  category_names.each_with_index do |category_name, index|
    decrease_by_category = decrease

    scores[index * (length + 1), length].each do |score|
      score = score.to_i
      decrease_by_category += score if score < 0
    end

    decreases_by_category[category_name] = decrease_by_category if decrease_by_category > 0
  end

  return if decreases_by_category.empty?

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

    @redis.save unless manual?
  end
end