Class: ClassifierReborn::BayesRedisBackend
- Inherits:
-
Object
- Object
- ClassifierReborn::BayesRedisBackend
- Defined in:
- lib/classifier-reborn/backends/bayes_redis_backend.rb
Overview
This class provides Redis as the storage backend for the classifier data structures
Instance Method Summary collapse
- #add_category(category) ⇒ Object
- #category_has_trainings?(category) ⇒ Boolean
- #category_keys ⇒ Object
- #category_training_count(category) ⇒ Object
- #category_word_count(category) ⇒ Object
- #category_word_frequency(category, word) ⇒ Object
- #delete_category_word(category, word) ⇒ Object
-
#initialize(options = {}) ⇒ BayesRedisBackend
constructor
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://:[email protected]:6380/2”.
- #reset ⇒ Object
- #total_trainings ⇒ Object
- #total_words ⇒ Object
- #update_category_training_count(category, diff) ⇒ Object
- #update_category_word_count(category, diff) ⇒ Object
- #update_category_word_frequency(category, word, diff) ⇒ Object
- #update_total_trainings(diff) ⇒ Object
- #update_total_words(diff) ⇒ Object
- #word_in_category?(category, word) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ BayesRedisBackend
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://:[email protected]: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
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 31 def initialize( = {}) begin # because some people don't have redis installed require 'redis' rescue LoadError raise NoRedisError end @redis = Redis.new() @redis.setnx(:total_words, 0) @redis.setnx(:total_trainings, 0) end |
Instance Method Details
#add_category(category) ⇒ Object
79 80 81 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 79 def add_category(category) @redis.sadd(:category_keys, category) end |
#category_has_trainings?(category) ⇒ Boolean
67 68 69 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 67 def category_has_trainings?(category) category_training_count(category) > 0 end |
#category_keys ⇒ Object
83 84 85 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 83 def category_keys @redis.smembers(:category_keys).map(&:intern) end |
#category_training_count(category) ⇒ Object
59 60 61 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 59 def category_training_count(category) @redis.hget(:category_training_count, category).to_i end |
#category_word_count(category) ⇒ Object
71 72 73 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 71 def category_word_count(category) @redis.hget(:category_word_count, category).to_i end |
#category_word_frequency(category, word) ⇒ Object
87 88 89 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 87 def category_word_frequency(category, word) @redis.hget(category, word).to_i end |
#delete_category_word(category, word) ⇒ Object
95 96 97 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 95 def delete_category_word(category, word) @redis.hdel(category, word) end |
#reset ⇒ Object
103 104 105 106 107 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 103 def reset @redis.flushdb @redis.set(:total_words, 0) @redis.set(:total_trainings, 0) end |
#total_trainings ⇒ Object
51 52 53 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 51 def total_trainings @redis.get(:total_trainings).to_i end |
#total_words ⇒ Object
43 44 45 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 43 def total_words @redis.get(:total_words).to_i end |
#update_category_training_count(category, diff) ⇒ Object
63 64 65 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 63 def update_category_training_count(category, diff) @redis.hincrby(:category_training_count, category, diff) end |
#update_category_word_count(category, diff) ⇒ Object
75 76 77 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 75 def update_category_word_count(category, diff) @redis.hincrby(:category_word_count, category, diff) end |
#update_category_word_frequency(category, word, diff) ⇒ Object
91 92 93 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 91 def update_category_word_frequency(category, word, diff) @redis.hincrby(category, word, diff) end |
#update_total_trainings(diff) ⇒ Object
55 56 57 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 55 def update_total_trainings(diff) @redis.incrby(:total_trainings, diff) end |
#update_total_words(diff) ⇒ Object
47 48 49 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 47 def update_total_words(diff) @redis.incrby(:total_words, diff) end |
#word_in_category?(category, word) ⇒ Boolean
99 100 101 |
# File 'lib/classifier-reborn/backends/bayes_redis_backend.rb', line 99 def word_in_category?(category, word) @redis.hexists(category, word) end |