Class: Classifier::RedisStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/classifier/redis_store.rb

Overview

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang, categories) ⇒ RedisStore

Returns a new instance of RedisStore.



21
22
23
24
25
26
27
28
# File 'lib/classifier/redis_store.rb', line 21

def initialize(lang, categories)
	$redis = Redis.new
	@names = []
	@lang = lang
	categories.each_with_index do |category, index|
		@names << category.prepare_category_name
	end
end

Instance Attribute Details

#namesObject

Returns the value of attribute names.



19
20
21
# File 'lib/classifier/redis_store.rb', line 19

def names
  @names
end

Instance Method Details

#decrObject



71
72
73
# File 'lib/classifier/redis_store.rb', line 71

def decr
	$redis.decrby redis_key(category, word), count.to_i
end

#decr_total(count) ⇒ Object



75
76
77
# File 'lib/classifier/redis_store.rb', line 75

def decr_total(count)
	$redis.decrby redis_total_key, count.to_i
end

#each(&block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/classifier/redis_store.rb', line 79

def each(&block)
	#return enum_for(__method__) if block.nil?
	@names.each do |category|
		if block_given?
			block.call(category, get_by_wild_keys(category))
		else
			yield category
		end
	end
end

#escape_category(category) ⇒ Object



100
101
102
# File 'lib/classifier/redis_store.rb', line 100

def escape_category(category)
	category.to_s.gsub(" ", "_").downcase
end

#escape_langObject



108
109
110
# File 'lib/classifier/redis_store.rb', line 108

def escape_lang
	@lang.to_s.downcase
end

#escape_word(word) ⇒ Object



104
105
106
# File 'lib/classifier/redis_store.rb', line 104

def escape_word(word)
	word.to_s.force_encoding('UTF-8')
end

#get(category, word) ⇒ Object



54
55
56
57
# File 'lib/classifier/redis_store.rb', line 54

def get(category, word)
	val = $redis.get redis_key(category, word)
	val.nil? ? nil : val.to_i
end

#get_by_key(key) ⇒ Object



120
121
122
123
# File 'lib/classifier/redis_store.rb', line 120

def get_by_key(key)
	val = $redis.get(key)
	val.is_a?(String) ? eval(val) : val
end

#get_by_wild_keys(category) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/classifier/redis_store.rb', line 112

def get_by_wild_keys(category)
	wildlings = []
	$redis.keys("#{escape_lang}:#{escape_category(category)}:*").each do |key|
		wildlings << get_by_key(key).to_i
	end
	wildlings
end

#incr(category, word, count) ⇒ Object



63
64
65
# File 'lib/classifier/redis_store.rb', line 63

def incr(category, word, count)
	$redis.incrby redis_key(category, word), count.to_i
end

#incr_total(count) ⇒ Object



67
68
69
# File 'lib/classifier/redis_store.rb', line 67

def incr_total(count)
	$redis.incrby redis_total_key, count.to_i
end

#init(category, word) ⇒ Object



30
31
32
33
34
# File 'lib/classifier/redis_store.rb', line 30

def init(category, word)
	if !key_for?(category, word)
		insert(category, word, 0)
	end
end

#init_totalObject



36
37
38
# File 'lib/classifier/redis_store.rb', line 36

def init_total
	$redis.set redis_total_key, 0
end

#insert(category, word, val) ⇒ Object



50
51
52
# File 'lib/classifier/redis_store.rb', line 50

def insert(category, word, val)
	$redis.set(redis_key(category, word), "#{val}")
end

#key_for?(category, word) ⇒ Boolean Also known as: has_word?

Returns:

  • (Boolean)


44
45
46
# File 'lib/classifier/redis_store.rb', line 44

def key_for?(category, word)
	$redis.exists(redis_key(category, word))
end

#redis_key(category, word) ⇒ Object

protected



92
93
94
# File 'lib/classifier/redis_store.rb', line 92

def redis_key(category, word)
	"#{escape_lang}:#{escape_category(category)}:#{escape_word(word)}"
end

#redis_total_keyObject



96
97
98
# File 'lib/classifier/redis_store.rb', line 96

def redis_total_key
	"redis_bayes_store_#{@lang}"
end

#remove(category, word) ⇒ Object



59
60
61
# File 'lib/classifier/redis_store.rb', line 59

def remove(category, word)
	$redis.del redis_key(category, word)
end

#total_wordsObject



40
41
42
# File 'lib/classifier/redis_store.rb', line 40

def total_words
	$redis.get(redis_total_key).to_i
end