Class: Inferx::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/inferx/category.rb,
lib/inferx/category/complementary.rb

Direct Known Subclasses

Complementary

Defined Under Namespace

Classes: Complementary

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, categories, name, size) ⇒ Category

Returns a new instance of Category.

Parameters:

  • redis (Redis)

    an instance of Redis

  • categories (Inferx::Categories)

    the categories

  • name (String)

    a category name

  • size (Integer)

    total of scores



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

#keyString (readonly)

Get key for access to training data of the category on Redis.

Returns:

  • (String)

    the key



28
29
30
# File 'lib/inferx/category.rb', line 28

def key
  @key
end

#nameString (readonly)

Get name of the category.

Returns:

  • (String)

    the name



34
35
36
# File 'lib/inferx/category.rb', line 34

def name
  @name
end

#sizeInteger (readonly)

Get total of scores.

Returns:

  • (Integer)

    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

#allHash<String, Integer>

Get words with scores in the category.

Returns:

  • (Hash<String, Integer>)

    words with scores



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.

Parameters:

  • word (String)

    the word

Returns:

  • (Integer)

    when the word is member, score of the word

  • (nil)

    when the word is not member



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.

Parameters:

  • words (Array<String>)

    an array of words

Returns:

  • (Array<Integer>)

    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.

Parameters:

  • words (Array<String>)

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

Parameters:

  • words (Array<String>)

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