X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fmarkov.rb;h=de33136e7c428cdb71811be7a8f8760495aa941c;hb=7b7f1309e8c3dbc3bb4408d56489ae5fba77d57a;hp=b0e84cb564e37f0041d35a2da25bf8fe1e8b3f01;hpb=c2d9108e50ca073aa0670755682a355d8f04f697;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/markov.rb b/data/rbot/plugins/markov.rb index b0e84cb5..de33136e 100644 --- a/data/rbot/plugins/markov.rb +++ b/data/rbot/plugins/markov.rb @@ -2,16 +2,25 @@ class MarkovPlugin < Plugin def initialize super @registry.set_default([]) + @registry['enabled'] = false unless @registry.has_key?('enabled') @lastline = false end - def generate_string(seedline) + def generate_string(word1, word2) # limit to max of 50 words - return unless seedline - word1, word2 = seedline.split(/\s+/) output = word1 + " " + word2 - 50.times do - wordlist = @registry["#{word1}/#{word2}"] + + # try to avoid :nonword in the first iteration + wordlist = @registry["#{word1} #{word2}"] + wordlist.delete(:nonword) + if not wordlist.empty? + word3 = wordlist[rand(wordlist.length)] + output = output + " " + word3 + word1, word2 = word2, word3 + end + + 49.times do + wordlist = @registry["#{word1} #{word2}"] break if wordlist.empty? word3 = wordlist[rand(wordlist.length)] break if word3 == :nonword @@ -50,7 +59,11 @@ class MarkovPlugin < Plugin end def ignore?(user=nil) - return @registry['ignore_users'].include?(user) + return false unless user + @registry['ignore_users'].each do |mask| + return true if user.matches?(mask) + end + return false end def ignore(m, params) @@ -109,17 +122,25 @@ class MarkovPlugin < Plugin return false end + def delay + 1 + rand(5) + end + def random_markov(m, message) return unless should_talk - line = generate_string(message) + + word1, word2 = message.split(/\s+/) + line = generate_string(word1, word2) return unless line - m.reply line unless line == message + return if line == message + @bot.timer.add_once(delay, m) {|m| + m.reply line + } end def chat(m, params) - seed = "#{params[:seed1]} #{params[:seed2]}" - line = generate_string seed - if line != seed + line = generate_string(params[:seed1], params[:seed2]) + if line != "#{params[:seed1]} #{params[:seed2]}" m.reply line else m.reply "I can't :(" @@ -131,7 +152,7 @@ class MarkovPlugin < Plugin word1, word2 = :nonword, :nonword output = Array.new 50.times do - wordlist = @registry["#{word1}/#{word2}"] + wordlist = @registry["#{word1} #{word2}"] break if wordlist.empty? word3 = wordlist[rand(wordlist.length)] break if word3 == :nonword @@ -152,23 +173,26 @@ class MarkovPlugin < Plugin # in channel message, the kind we are interested in message = clean_str m.message - - # we respond first. otherwise if we add this line to the db first, and - # it's fairly unique, there's a good chance we'll just parrot it back - # here. - random_markov(m, message) + + if m.action? + message = "#{m.sourcenick} #{message}" + end wordlist = message.split(/\s+/) - return unless wordlist.length > 2 + return unless wordlist.length >= 2 @lastline = message word1, word2 = :nonword, :nonword wordlist.each do |word3| - @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3) + @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(word3) word1, word2 = word2, word3 end - @registry["#{word1}/#{word2}"] = [:nonword] + @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(:nonword) + + return if m.replied? + random_markov(m, message) end end + plugin = MarkovPlugin.new plugin.map 'markov ignore :action :option', :action => "ignore" plugin.map 'markov ignore :action', :action => "ignore" @@ -179,4 +203,4 @@ plugin.map 'markov status', :action => "status" plugin.map 'chat about :seed1 :seed2', :action => "chat" plugin.map 'chat', :action => "rand_chat" plugin.map 'markov probability :probability', :action => "probability", - :requirements => {:probability => /^\d+$/} + :requirements => {:probability => /^\d+%?$/}