class ClassifierReborn::BayesRedisBackend

This class provides Redis as the storage backend for the classifier data structures

Public Class Methods

new(options = {}) click to toggle source

The class can be created with the same arguments that the redis gem accepts E.g.,

b = ClassifierReborn::BayesRedisBackend.new
b = ClassifierReborn::BayesRedisBackend.new host: "10.0.1.1", port: 6380, db: 2
b = ClassifierReborn::BayesRedisBackend.new url: "redis://:secret@10.0.1.1:6380/2"

Options available are:

url:                lambda { ENV["REDIS_URL"] }
scheme:             "redis"
host:               "127.0.0.1"
port:               6379
path:               nil
timeout:            5.0
password:           nil
db:                 0
driver:             nil
id:                 nil
tcp_keepalive:      0
reconnect_attempts: 1
inherit_socket:     false
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 29
def initialize(options = {})
  begin # because some people don't have redis installed
    require 'redis'
  rescue LoadError
    raise NoRedisError
  end

  @redis = Redis.new(options)
  @redis.setnx(:total_words, 0)
  @redis.setnx(:total_trainings, 0)
end

Public Instance Methods

add_category(category) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 77
def add_category(category)
  @redis.sadd(:category_keys, category)
end
category_has_trainings?(category) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 65
def category_has_trainings?(category)
  category_training_count(category) > 0
end
category_keys() click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 81
def category_keys
  @redis.smembers(:category_keys).map(&:intern)
end
category_training_count(category) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 57
def category_training_count(category)
  @redis.hget(:category_training_count, category).to_i
end
category_word_count(category) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 69
def category_word_count(category)
  @redis.hget(:category_word_count, category).to_i
end
category_word_frequency(category, word) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 85
def category_word_frequency(category, word)
  @redis.hget(category, word).to_i
end
delete_category_word(category, word) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 93
def delete_category_word(category, word)
  @redis.hdel(category, word)
end
reset() click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 101
def reset
  @redis.flushdb
  @redis.set(:total_words, 0)
  @redis.set(:total_trainings, 0)
end
total_trainings() click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 49
def total_trainings
  @redis.get(:total_trainings).to_i
end
total_words() click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 41
def total_words
  @redis.get(:total_words).to_i
end
update_category_training_count(category, diff) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 61
def update_category_training_count(category, diff)
  @redis.hincrby(:category_training_count, category, diff)
end
update_category_word_count(category, diff) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 73
def update_category_word_count(category, diff)
  @redis.hincrby(:category_word_count, category, diff)
end
update_category_word_frequency(category, word, diff) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 89
def update_category_word_frequency(category, word, diff)
  @redis.hincrby(category, word, diff)
end
update_total_trainings(diff) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 53
def update_total_trainings(diff)
  @redis.incrby(:total_trainings, diff)
end
update_total_words(diff) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 45
def update_total_words(diff)
  @redis.incrby(:total_words, diff)
end
word_in_category?(category, word) click to toggle source
# File lib/classifier-reborn/backends/bayes_redis_backend.rb, line 97
def word_in_category?(category, word)
  @redis.hexists(category, word)
end