1 class MarkovPlugin < Plugin
4 @registry.set_default([])
10 # limit to max of 50 words
11 return unless @lastline
12 word1, word2 = @lastline.split(/\s+/)
13 output = word1 + " " + word2
15 wordlist = @registry["#{word1}/#{word2}"]
16 word3 = wordlist[rand(wordlist.length)]
17 break if word3 == :nonword
18 output = output + " " + word3
19 word1, word2 = word2, word3
28 def help(plugin, topic="")
29 "markov plugin: listens to chat to build a markov chain, with which it can (perhaps) attempt to (inanely) contribute to 'discussion'. Sort of.. Will get a *lot* better after listening to a lot of chat. usage: 'markov' to attempt to say something relevant to the last line of chat, if it can."
44 def disable(m, params)
50 return false unless @enabled
52 return false if rand(2) == 1
57 return unless should_talk
59 puts "got line #{line}"
60 m.reply line unless line == @lastline
64 return unless m.kind_of?(PrivMessage) && m.public?
66 message = clean_str m.message
67 # in channel message, the kind we are interested in
68 wordlist = message.split(/\s+/)
69 return unless wordlist.length > 2
71 word1, word2 = :nonword, :nonword
72 wordlist.each do |word3|
73 @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
74 word1, word2 = word2, word3
76 @registry["#{word1}/#{word2}"] = [:nonword]
80 plugin = MarkovPlugin.new
81 plugin.map 'markov enable', :action => "enable"
82 plugin.map 'markov disable', :action => "disable"