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).")
23 Config.register Config::IntegerValue.new('markov.max_words',
25 :validate => Proc.new { |v| (0..100).include? v },
26 :desc => "Maximum number of words the bot should put in a sentence")
30 @registry.set_default([])
31 if @registry.has_key?('enabled')
32 @bot.config['markov.enabled'] = @registry['enabled']
33 @registry.delete('enabled')
35 if @registry.has_key?('probability')
36 @bot.config['markov.probability'] = @registry['probability']
37 @registry.delete('probability')
39 if @bot.config['markov.ignore_users']
40 debug "moving markov.ignore_users to markov.ignore"
41 @bot.config['markov.ignore'] = @bot.config['markov.ignore_users'].dup
42 @bot.config.delete('markov.ignore_users'.to_sym)
44 @learning_queue = Queue.new
45 @learning_thread = Thread.new do
46 while s = @learning_queue.pop
51 @learning_thread.priority = -1
55 debug 'closing learning thread'
56 @learning_queue.push nil
58 debug 'learning thread closed'
61 def generate_string(word1, word2)
62 # limit to max of markov.max_words words
63 output = word1 + " " + word2
65 # try to avoid :nonword in the first iteration
66 wordlist = @registry["#{word1} #{word2}"]
67 wordlist.delete(:nonword)
68 if not wordlist.empty?
69 word3 = wordlist[rand(wordlist.length)]
70 output = output + " " + word3
71 word1, word2 = word2, word3
74 (@bot.config['markov.max_words'] - 1).times do
75 wordlist = @registry["#{word1} #{word2}"]
76 break if wordlist.empty?
77 word3 = wordlist[rand(wordlist.length)]
78 break if word3 == :nonword
79 output = output + " " + word3
80 word1, word2 = word2, word3
85 def help(plugin, topic="")
86 "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)"
91 str.gsub!(/^\S+[:,;]/, "")
92 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
97 return @bot.config['markov.probability']
101 if @bot.config['markov.enabled']
102 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
104 m.reply "markov is currently disabled"
109 return false unless m
110 return true if m.address? or m.private?
111 @bot.config['markov.ignore'].each do |mask|
112 return true if m.channel.downcase == mask.downcase
113 return true if m.source.matches?(mask)
118 def ignore(m, params)
119 action = params[:action]
120 user = params[:option]
123 if @bot.config['markov.ignore'].include? user
124 s = @bot.config['markov.ignore']
126 @bot.config['ignore'] = s
127 m.reply "#{user} removed"
129 m.reply "not found in list"
133 if @bot.config['markov.ignore'].include?(user)
134 m.reply "#{user} already in list"
136 @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
137 m.reply "#{user} added to markov ignore list"
140 m.reply "give the name of a person or channel to ignore"
143 m.reply "I'm ignoring #{@bot.config['markov.ignore'].join(", ")}"
145 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"
149 def enable(m, params)
150 @bot.config['markov.enabled'] = true
154 def probability(m, params)
155 if params[:probability]
156 @bot.config['markov.probability'] = params[:probability].to_i
159 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
163 def disable(m, params)
164 @bot.config['markov.enabled'] = false
169 return false unless @bot.config['markov.enabled']
171 return true if prob > rand(100)
179 def random_markov(m, message)
180 return unless should_talk
182 word1, word2 = message.split(/\s+/)
183 return unless word1 and word2
184 line = generate_string(word1, word2)
186 # we do nothing if the line we return is just an initial substring
187 # of the line we received
188 return if message.index(line) == 0
189 @bot.timer.add_once(delay) {
195 line = generate_string(params[:seed1], params[:seed2])
196 if line != "#{params[:seed1]} #{params[:seed2]}"
203 def rand_chat(m, params)
204 # pick a random pair from the db and go from there
205 word1, word2 = :nonword, :nonword
208 wordlist = @registry["#{word1} #{word2}"]
209 break if wordlist.empty?
210 word3 = wordlist[rand(wordlist.length)]
211 break if word3 == :nonword
213 word1, word2 = word2, word3
216 m.reply output.join(" ")
225 # in channel message, the kind we are interested in
226 message = clean_str m.plainmessage
229 message = "#{m.sourcenick} #{message}"
232 @learning_queue.push message
233 random_markov(m, message) unless m.replied?
237 # debug "learning #{message}"
238 wordlist = message.split(/\s+/)
239 return unless wordlist.length >= 2
240 word1, word2 = :nonword, :nonword
241 wordlist.each do |word3|
242 k = "#{word1} #{word2}"
243 @registry[k] = @registry[k].push(word3)
244 word1, word2 = word2, word3
246 k = "#{word1} #{word2}"
247 @registry[k] = @registry[k].push(:nonword)
251 plugin = MarkovPlugin.new
252 plugin.map 'markov ignore :action :option', :action => "ignore"
253 plugin.map 'markov ignore :action', :action => "ignore"
254 plugin.map 'markov ignore', :action => "ignore"
255 plugin.map 'markov enable', :action => "enable"
256 plugin.map 'markov disable', :action => "disable"
257 plugin.map 'markov status', :action => "status"
258 plugin.map 'chat about :seed1 :seed2', :action => "chat"
259 plugin.map 'chat', :action => "rand_chat"
260 plugin.map 'markov probability [:probability]', :action => "probability",
261 :requirements => {:probability => /^\d+%?$/}