4 # :title: Markov plugin
6 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
7 # Copyright:: (C) 2005 Tom Gilbert
9 # Contribute to chat with random phrases built from word sequences learned
10 # by listening to chat
12 class MarkovPlugin < Plugin
13 Config.register Config::BooleanValue.new('markov.enabled',
15 :desc => "Enable and disable the plugin")
16 Config.register Config::IntegerValue.new('markov.probability',
18 :validate => Proc.new { |v| (0..100).include? v },
19 :desc => "Percentage chance of markov plugin chipping in")
20 Config.register Config::ArrayValue.new('markov.ignore',
22 :desc => "Hostmasks and channel names markov should NOT learn from (e.g. idiot*!*@*, #privchan).")
26 @registry.set_default([])
27 if @registry.has_key?('enabled')
28 @bot.config['markov.enabled'] = @registry['enabled']
29 @registry.delete('enabled')
31 if @registry.has_key?('probability')
32 @bot.config['markov.probability'] = @registry['probability']
33 @registry.delete('probability')
35 if @bot.config['markov.ignore_users']
36 debug "moving markov.ignore_users to markov.ignore"
37 @bot.config['markov.ignore'] = @bot.config['markov.ignore_users'].dup
38 @bot.config.delete('markov.ignore_users')
40 @learning_queue = Queue.new
41 @learning_thread = Thread.new do
42 while s = @learning_queue.pop
47 @learning_thread.priority = -1
51 debug 'closing learning thread'
52 @learning_queue.push nil
54 debug 'learning thread closed'
57 def generate_string(word1, word2)
58 # limit to max of 50 words
59 output = word1 + " " + word2
61 # try to avoid :nonword in the first iteration
62 wordlist = @registry["#{word1} #{word2}"]
63 wordlist.delete(:nonword)
64 if not wordlist.empty?
65 word3 = wordlist[rand(wordlist.length)]
66 output = output + " " + word3
67 word1, word2 = word2, word3
71 wordlist = @registry["#{word1} #{word2}"]
72 break if wordlist.empty?
73 word3 = wordlist[rand(wordlist.length)]
74 break if word3 == :nonword
75 output = output + " " + word3
76 word1, word2 = word2, word3
81 def help(plugin, topic="")
82 "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 [<chance>]' => set the % chance of rbot responding to input, or display the current probability, 'chat' => try and say something intelligent, 'chat about <foo> <bar>' => riff on a word pair (if possible)"
87 str.gsub!(/^\S+[:,;]/, "")
88 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
93 return @bot.config['markov.probability']
97 if @bot.config['markov.enabled']
98 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
100 m.reply "markov is currently disabled"
105 return false unless m
106 return true if m.address? or m.private?
107 @bot.config['markov.ignore'].each do |mask|
108 return true if m.channel.downcase == mask.downcase
109 return true if m.source.matches?(mask)
114 def ignore(m, params)
115 action = params[:action]
116 user = params[:option]
119 if @bot.config['markov.ignore'].include? user
120 s = @bot.config['markov.ignore']
122 @bot.config['ignore'] = s
123 m.reply "#{user} removed"
125 m.reply "not found in list"
129 if @bot.config['markov.ignore'].include?(user)
130 m.reply "#{user} already in list"
132 @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
133 m.reply "#{user} added to markov ignore list"
136 m.reply "give the name of a person or channel to ignore"
139 m.reply "I'm ignoring #{@bot.config['markov.ignore'].join(", ")}"
141 m.reply "have markov ignore the input from a hostmask or a channel. usage: markov ignore add <mask or channel>; markov ignore remove <mask or channel>; markov ignore list"
145 def enable(m, params)
146 @bot.config['markov.enabled'] = true
150 def probability(m, params)
151 if params[:probability]
152 @bot.config['markov.probability'] = params[:probability].to_i
155 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
159 def disable(m, params)
160 @bot.config['markov.enabled'] = false
165 return false unless @bot.config['markov.enabled']
167 return true if prob > rand(100)
175 def random_markov(m, message)
176 return unless should_talk
178 word1, word2 = message.split(/\s+/)
179 return unless word1 and word2
180 line = generate_string(word1, word2)
182 return if line == message
183 @bot.timer.add_once(delay) {
189 line = generate_string(params[:seed1], params[:seed2])
190 if line != "#{params[:seed1]} #{params[:seed2]}"
197 def rand_chat(m, params)
198 # pick a random pair from the db and go from there
199 word1, word2 = :nonword, :nonword
202 wordlist = @registry["#{word1} #{word2}"]
203 break if wordlist.empty?
204 word3 = wordlist[rand(wordlist.length)]
205 break if word3 == :nonword
207 word1, word2 = word2, word3
210 m.reply output.join(" ")
219 # in channel message, the kind we are interested in
220 message = clean_str m.plainmessage
223 message = "#{m.sourcenick} #{message}"
226 @learning_queue.push message
227 random_markov(m, message) unless m.replied?
231 # debug "learning #{message}"
232 wordlist = message.split(/\s+/)
233 return unless wordlist.length >= 2
234 word1, word2 = :nonword, :nonword
235 wordlist.each do |word3|
236 k = "#{word1} #{word2}"
237 @registry[k] = @registry[k].push(word3)
238 word1, word2 = word2, word3
240 k = "#{word1} #{word2}"
241 @registry[k] = @registry[k].push(:nonword)
245 plugin = MarkovPlugin.new
246 plugin.map 'markov ignore :action :option', :action => "ignore"
247 plugin.map 'markov ignore :action', :action => "ignore"
248 plugin.map 'markov ignore', :action => "ignore"
249 plugin.map 'markov enable', :action => "enable"
250 plugin.map 'markov disable', :action => "disable"
251 plugin.map 'markov status', :action => "status"
252 plugin.map 'chat about :seed1 :seed2', :action => "chat"
253 plugin.map 'chat', :action => "rand_chat"
254 plugin.map 'markov probability [:probability]', :action => "probability",
255 :requirements => {:probability => /^\d+%?$/}