class Qwik::SearchWordsDB
Constants
- NUM_SIZE
- Word
Public Class Methods
new(path, config)
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 14 def initialize(path, config) @config = config @path = path @word_index = Hash.new @recent_list = Array.new @normalized_list = Array.new @lock = Mutex.new read end
Public Instance Methods
delete(word)
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 24 def delete(word) w = word.to_sym @lock.synchronize { em = @word_index[w] if em @word_index.delete(w) @recent_list.delete(em) save end } end
get()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 61 def get @recent_list[0...@config[:search_word_display_num]] end
get_normalized()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 65 def get_normalized @normalized_list end
put(words)
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 36 def put(words) if words.class == String words = [words] end @lock.synchronize { words.each {|word| w = word.to_sym em = @word_index[w] if em @recent_list.delete(em) em.count +=1 else em = Word.new(w, 1, Time.new) if @config[:search_word_max_num] < @recent_list.size em = @recent_list.delete_at(-1) @word_index.delete(em.word) end @word_index[em.word] = em end @recent_list.unshift(em) } save } end
Private Instance Methods
min_max()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 112 def min_max return [0, 0] if @recent_list[0].nil? min = max = @recent_list[0].count @recent_list[1...@config[:search_word_display_num]].each{|em| if max < em.count max = em.count elsif em.count < min min = em.count end } return [min, max] end
normalize()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 102 def normalize min,max = min_max diff = max - min + 0.1 @normalized_list = @recent_list[0...@config[:search_word_display_num]].map{|em| norm = (em.count - min) / diff * NUM_SIZE Word.new(em.word, norm.to_i, em.time) } end
path()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 97 def path "#{@path}/_SearchWords.txt" end
read()
click to toggle source
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 80 def read begin cont = File.new(path).read @lock.synchronize { cont.each_line {|line| time, count, word = line.chomp.split(/ /) em = Word.new(word.to_sym, count.to_i, Time.at(time.to_i)) @recent_list.push(em) @word_index[em.word] = em } normalize } rescue Errno::ENOENT # do nothing end end
save()
click to toggle source
should be called from synchronized block
# File vendor/qwik/lib/qwik/db-searchwords.rb, line 71 def save f = File.new(path,"w") @recent_list.each {|em| f.puts "#{em.time.to_i} #{em.count} #{em.word}" } f.close normalize end