- 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
+ return unless should_talk(m)
+
+ words = clean_message(m).split(/\s+/)
+ if words.length < 2
+ line = generate_string words.first, nil
+
+ if line and 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, word2)
+ 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