From 2962912178d5def0f3f4b4d8a42b726bb43e228d Mon Sep 17 00:00:00 2001 From: Yaohan Chen Date: Wed, 25 Jun 2008 15:27:38 -0400 Subject: markov plugin: do learning in one thread, instead of threading for each message --- data/rbot/plugins/markov.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'data/rbot') 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 -- cgit v1.2.3