X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fmarkov.rb;h=17446f6d7be4310e97c3d45cf91d716d1a6145fe;hb=6fb82f2c8ae244cb098f2eae78a7d3143f3e2069;hp=dcf3b77679dc7e5ad304fa96a74f5efd8ba16d3b;hpb=e00ea339ca3baaa9cfe74f6cec85c36e83307b61;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/markov.rb b/data/rbot/plugins/markov.rb index dcf3b776..17446f6d 100644 --- a/data/rbot/plugins/markov.rb +++ b/data/rbot/plugins/markov.rb @@ -32,6 +32,21 @@ 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 + sleep 0.5 + end + end + @learning_thread.priority = -1 + end + + def cleanup + debug 'closing learning thread' + @learning_queue.push nil + @learning_thread.join + debug 'learning thread closed' end def generate_string(word1, word2) @@ -154,6 +169,7 @@ class MarkovPlugin < Plugin return unless should_talk word1, word2 = message.split(/\s+/) + return unless word1 and word2 line = generate_string(word1, word2) return unless line return if line == message @@ -196,26 +212,28 @@ class MarkovPlugin < Plugin return if ignore? m.source # in channel message, the kind we are interested in - message = clean_str m.message + message = clean_str m.plainmessage if m.action? 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