summaryrefslogtreecommitdiff
path: root/data/rbot/plugins
diff options
context:
space:
mode:
authorVoker57 <voker57@gmail.com>2009-12-12 21:49:33 +0300
committerVoker57 <voker57@gmail.com>2010-01-26 00:41:07 +0300
commitfa683e65dd0108da044074a66a5068f71a3fb904 (patch)
treef0023a8c0f294c089aa906a66f758560212136f2 /data/rbot/plugins
parentca71ea8695b6810ececa7cd9caeec09aae57d751 (diff)
markov: use not only first two first words for building phrase
Diffstat (limited to 'data/rbot/plugins')
-rwxr-xr-xdata/rbot/plugins/markov.rb44
1 files changed, 36 insertions, 8 deletions
diff --git a/data/rbot/plugins/markov.rb b/data/rbot/plugins/markov.rb
index 1ffda9fa..e6d9d1a8 100755
--- a/data/rbot/plugins/markov.rb
+++ b/data/rbot/plugins/markov.rb
@@ -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)