1 class MarkovPlugin < Plugin
4 @registry.set_default([])
5 @registry['enabled'] = false unless @registry.has_key?('enabled')
9 def generate_string(word1, word2)
10 # limit to max of 50 words
11 output = word1 + " " + word2
13 # try to avoid :nonword in the first iteration
14 wordlist = @registry["#{word1} #{word2}"]
15 wordlist.delete(:nonword)
16 if not wordlist.empty?
17 word3 = wordlist[rand(wordlist.length)]
18 output = output + " " + word3
19 word1, word2 = word2, word3
23 wordlist = @registry["#{word1} #{word2}"]
24 break if wordlist.empty?
25 word3 = wordlist[rand(wordlist.length)]
26 break if word3 == :nonword
27 output = output + " " + word3
28 word1, word2 = word2, word3
33 def help(plugin, topic="")
34 "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. other options to markov: 'ignore' => ignore a hostmask (accept no input), 'status' => show current status, 'probability' => set the % chance of rbot responding to input, 'chat' => try and say something intelligent, 'chat about <foo> <bar>' => riff on a word pair (if possible)"
39 str.gsub!(/^\S+[:,;]/, "")
40 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
45 prob = @registry['probability']
46 prob = 25 if prob.kind_of? Array;
48 prob = 100 if prob > 100
53 enabled = @registry['enabled']
55 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
57 m.reply "markov is currently disabled"
62 @registry['ignore_users'].each do |mask|
63 return true if Irc.netmaskmatch mask, user
69 if @registry['ignore_users'].nil?
70 @registry['ignore_users'] = []
72 action = params[:action]
73 user = params[:option]
76 if @registry['ignore_users'].include? user
77 s = @registry['ignore_users']
79 @registry['ignore_users'] = s
80 m.reply "#{user} removed"
82 m.reply "not found in list"
86 if @registry['ignore_users'].include?(user)
87 m.reply "#{user} already in list"
89 @registry['ignore_users'] = @registry['ignore_users'].push user
90 m.reply "#{user} added to markov ignore list"
93 m.reply "give the name of a person to ignore"
96 m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
98 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
102 def enable(m, params)
103 @registry['enabled'] = true
107 def probability(m, params)
108 @registry['probability'] = params[:probability].to_i
112 def disable(m, params)
113 @registry['enabled'] = false
118 return false unless @registry['enabled']
120 return true if prob > rand(100)
128 def random_markov(m, message)
129 return unless should_talk
131 word1, word2 = message.split(/\s+/)
132 line = generate_string(word1, word2)
134 return if line == message
135 @bot.timer.add_once(delay, m) {|m|
141 line = generate_string(params[:seed1], params[:seed2])
142 if line != "#{params[:seed1]} #{params[:seed2]}"
149 def rand_chat(m, params)
150 # pick a random pair from the db and go from there
151 word1, word2 = :nonword, :nonword
154 wordlist = @registry["#{word1} #{word2}"]
155 break if wordlist.empty?
156 word3 = wordlist[rand(wordlist.length)]
157 break if word3 == :nonword
159 word1, word2 = word2, word3
162 m.reply output.join(" ")
169 return unless m.kind_of?(PrivMessage) && m.public?
171 return if ignore? m.source
173 # in channel message, the kind we are interested in
174 message = clean_str m.message
177 message = "#{m.sourcenick} #{message}"
180 wordlist = message.split(/\s+/)
181 return unless wordlist.length >= 2
183 word1, word2 = :nonword, :nonword
184 wordlist.each do |word3|
185 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(word3)
186 word1, word2 = word2, word3
188 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(:nonword)
191 random_markov(m, message)
194 plugin = MarkovPlugin.new
195 plugin.map 'markov ignore :action :option', :action => "ignore"
196 plugin.map 'markov ignore :action', :action => "ignore"
197 plugin.map 'markov ignore', :action => "ignore"
198 plugin.map 'markov enable', :action => "enable"
199 plugin.map 'markov disable', :action => "disable"
200 plugin.map 'markov status', :action => "status"
201 plugin.map 'chat about :seed1 :seed2', :action => "chat"
202 plugin.map 'chat', :action => "rand_chat"
203 plugin.map 'markov probability :probability', :action => "probability",
204 :requirements => {:probability => /^\d+%?$/}