diff options
author | Yaohan Chen <yaohan.chen@gmail.com> | 2008-06-25 15:27:38 -0400 |
---|---|---|
committer | Yaohan Chen <yaohan.chen@gmail.com> | 2008-06-25 15:27:38 -0400 |
commit | 2962912178d5def0f3f4b4d8a42b726bb43e228d (patch) | |
tree | 34031517993d7b09f84f50e4f5d2db3f9e342f4a /data/rbot/plugins/markov.rb | |
parent | 6f9ad0ae1a89404246877bfeffe9ff4d18745b1a (diff) |
markov plugin: do learning in one thread, instead of threading for each message
Diffstat (limited to 'data/rbot/plugins/markov.rb')
-rw-r--r-- | data/rbot/plugins/markov.rb | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/data/rbot/plugins/markov.rb b/data/rbot/plugins/markov.rb index dcf3b776..9e0bfccd 100644 --- a/data/rbot/plugins/markov.rb +++ b/data/rbot/plugins/markov.rb @@ -32,6 +32,19 @@ class MarkovPlugin < Plugin @bot.config['markov.probability'] = @registry['probability'] @registry.delete('probability') end + @learning_queue = Queue.new + @learning_thread = Thread.new do + while s = @learning_queue.pop + learn s + end + end + end + + def cleanup + debug 'closing learning thread' + @learning_queue.push nil + @learning_thread.join + debug 'learning thread closed' end def generate_string(word1, word2) @@ -202,20 +215,22 @@ class MarkovPlugin < Plugin message = "#{m.sourcenick} #{message}" end + @learning_queue.push message + random_markov(m, message) unless m.replied? + end + + def learn(message) + # debug "learning #{message}" wordlist = message.split(/\s+/) return unless wordlist.length >= 2 - Thread.new do - word1, word2 = :nonword, :nonword - wordlist.each do |word3| - k = "#{word1} #{word2}" - @registry[k] = @registry[k].push(word3) - word1, word2 = word2, word3 - end + word1, word2 = :nonword, :nonword + wordlist.each do |word3| k = "#{word1} #{word2}" - @registry[k] = @registry[k].push(:nonword) - - random_markov(m, message) unless m.replied? + @registry[k] = @registry[k].push(word3) + word1, word2 = word2, word3 end + k = "#{word1} #{word2}" + @registry[k] = @registry[k].push(:nonword) end end |