X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fmarkov.rb;h=17446f6d7be4310e97c3d45cf91d716d1a6145fe;hb=6fb82f2c8ae244cb098f2eae78a7d3143f3e2069;hp=60ecafec65cf5cf5097c44301c30ffadd6d5f951;hpb=bf2a5a05b86773da8351ae01d94b0fecd5c4519a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/markov.rb b/data/rbot/plugins/markov.rb index 60ecafec..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 @@ -190,18 +206,24 @@ class MarkovPlugin < Plugin end end - def listen(m) - return unless m.kind_of?(PrivMessage) && m.public? + def message(m) + return unless m.public? return if m.address? 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 word1, word2 = :nonword, :nonword @@ -212,9 +234,6 @@ class MarkovPlugin < Plugin end k = "#{word1} #{word2}" @registry[k] = @registry[k].push(:nonword) - - return if m.replied? - random_markov(m, message) end end