]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
markov: use not only first two first words for building phrase
authorVoker57 <voker57@gmail.com>
Sat, 12 Dec 2009 18:49:33 +0000 (21:49 +0300)
committerVoker57 <voker57@gmail.com>
Mon, 25 Jan 2010 21:41:07 +0000 (00:41 +0300)
data/rbot/plugins/markov.rb

index 1ffda9fa2d3bd82c8974f8c558c6be485da1613c..e6d9d1a87c0bc5ef35650f5b1d8ffa8bdc691b0b 100755 (executable)
@@ -506,6 +506,16 @@ class MarkovPlugin < Plugin
     return false
   end
 
+  # Generates all sequence pairs from array
+  # seq_pairs [1,2,3,4] == [ [1,2], [2,3], [3,4]]
+  def seq_pairs(arr)
+    res = []
+    0.upto(arr.size-2) do |i|
+      res << [arr[i], arr[i+1]]
+    end
+    res
+  end
+
   def set_delay(m, params)
     if params[:delay] == "off"
       @bot.config["markov.delay"] = 0
@@ -532,14 +542,32 @@ class MarkovPlugin < Plugin
   def random_markov(m, message)
     return unless (should_talk or (m.address? and  @bot.config['markov.answer_addressed'] > rand(100)))
 
-    word1, word2 = clean_str(message).split(/\s+/)
-    return unless word1 and word2
-    line = generate_string(word1.intern, word2.intern)
-    return unless line
-    # we do nothing if the line we return is just an initial substring
-    # of the line we received
-    return if message.index(line) == 0
-    reply_delay m, line
+    words = clean_str(message).split(/\s+/)
+    if words.length < 2
+      line = generate_string words.first, nil
+
+      if line
+        return if message.index(line) == 0
+        reply_delay m, line
+        return
+      end
+    else
+      pairs = seq_pairs(words).sort_by { rand }
+      pairs.each do |word1, word2|
+        line = generate_string(word1.intern, word2.intern)
+        if line and message.index(line) != 0
+          reply_delay m, line
+          return
+        end
+      end
+      words.sort_by { rand }.each do |word|
+        line = generate_string word.first, nil
+        if line and message.index(line) != 0
+          reply_delay m, line
+          return
+        end
+      end
+    end
   end
 
   def chat(m, params)