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_users',
22 :desc => "Hostmasks of users to be ignored")
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 @learning_queue = Queue.new
36 @learning_thread = Thread.new do
37 while s = @learning_queue.pop
42 @learning_thread.priority = -1
46 debug 'closing learning thread'
47 @learning_queue.push nil
49 debug 'learning thread closed'
52 def generate_string(word1, word2)
53 # limit to max of 50 words
54 output = word1 + " " + word2
56 # try to avoid :nonword in the first iteration
57 wordlist = @registry["#{word1} #{word2}"]
58 wordlist.delete(:nonword)
59 if not wordlist.empty?
60 word3 = wordlist[rand(wordlist.length)]
61 output = output + " " + word3
62 word1, word2 = word2, word3
66 wordlist = @registry["#{word1} #{word2}"]
67 break if wordlist.empty?
68 word3 = wordlist[rand(wordlist.length)]
69 break if word3 == :nonword
70 output = output + " " + word3
71 word1, word2 = word2, word3
76 def help(plugin, topic="")
77 "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)"
82 str.gsub!(/^\S+[:,;]/, "")
83 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
88 return @bot.config['markov.probability']
92 if @bot.config['markov.enabled']
93 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
95 m.reply "markov is currently disabled"
100 return false unless user
101 @bot.config['markov.ignore_users'].each do |mask|
102 return true if user.matches?(mask)
107 def ignore(m, params)
108 action = params[:action]
109 user = params[:option]
112 if @bot.config['markov.ignore_users'].include? user
113 s = @bot.config['markov.ignore_users']
115 @bot.config['ignore_users'] = s
116 m.reply "#{user} removed"
118 m.reply "not found in list"
122 if @bot.config['markov.ignore_users'].include?(user)
123 m.reply "#{user} already in list"
125 @bot.config['markov.ignore_users'] = @bot.config['markov.ignore_users'].push user
126 m.reply "#{user} added to markov ignore list"
129 m.reply "give the name of a person to ignore"
132 m.reply "I'm ignoring #{@bot.config['markov.ignore_users'].join(", ")}"
134 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
138 def enable(m, params)
139 @bot.config['markov.enabled'] = true
143 def probability(m, params)
144 if params[:probability]
145 @bot.config['markov.probability'] = params[:probability].to_i
148 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
152 def disable(m, params)
153 @bot.config['markov.enabled'] = false
158 return false unless @bot.config['markov.enabled']
160 return true if prob > rand(100)
168 def random_markov(m, message)
169 return unless should_talk
171 word1, word2 = message.split(/\s+/)
172 return unless word1 and word2
173 line = generate_string(word1, word2)
175 return if line == message
176 @bot.timer.add_once(delay) {
182 line = generate_string(params[:seed1], params[:seed2])
183 if line != "#{params[:seed1]} #{params[:seed2]}"
190 def rand_chat(m, params)
191 # pick a random pair from the db and go from there
192 word1, word2 = :nonword, :nonword
195 wordlist = @registry["#{word1} #{word2}"]
196 break if wordlist.empty?
197 word3 = wordlist[rand(wordlist.length)]
198 break if word3 == :nonword
200 word1, word2 = word2, word3
203 m.reply output.join(" ")
210 return unless m.public?
212 return if ignore? m.source
214 # in channel message, the kind we are interested in
215 message = clean_str m.plainmessage
218 message = "#{m.sourcenick} #{message}"
221 @learning_queue.push message
222 random_markov(m, message) unless m.replied?
226 # debug "learning #{message}"
227 wordlist = message.split(/\s+/)
228 return unless wordlist.length >= 2
229 word1, word2 = :nonword, :nonword
230 wordlist.each do |word3|
231 k = "#{word1} #{word2}"
232 @registry[k] = @registry[k].push(word3)
233 word1, word2 = word2, word3
235 k = "#{word1} #{word2}"
236 @registry[k] = @registry[k].push(:nonword)
240 plugin = MarkovPlugin.new
241 plugin.map 'markov ignore :action :option', :action => "ignore"
242 plugin.map 'markov ignore :action', :action => "ignore"
243 plugin.map 'markov ignore', :action => "ignore"
244 plugin.map 'markov enable', :action => "enable"
245 plugin.map 'markov disable', :action => "disable"
246 plugin.map 'markov status', :action => "status"
247 plugin.map 'chat about :seed1 :seed2', :action => "chat"
248 plugin.map 'chat', :action => "rand_chat"
249 plugin.map 'markov probability [:probability]', :action => "probability",
250 :requirements => {:probability => /^\d+%?$/}