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 reply = _("markov is currently enabled, %{p}% chance of chipping in") % { :p => probability? }
103 l = @learning_queue.length
104 reply << (_(", %{l} messages in queue") % {:l => l}) if l > 0
106 reply = _("markov is currently disabled")
112 return false unless m
113 return true if m.address? or m.private?
114 @bot.config['markov.ignore'].each do |mask|
115 return true if m.channel.downcase == mask.downcase
116 return true if m.source.matches?(mask)
121 def ignore(m, params)
122 action = params[:action]
123 user = params[:option]
126 if @bot.config['markov.ignore'].include? user
127 s = @bot.config['markov.ignore']
129 @bot.config['ignore'] = s
130 m.reply "#{user} removed"
132 m.reply "not found in list"
136 if @bot.config['markov.ignore'].include?(user)
137 m.reply "#{user} already in list"
139 @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
140 m.reply "#{user} added to markov ignore list"
143 m.reply "give the name of a person or channel to ignore"
146 m.reply "I'm ignoring #{@bot.config['markov.ignore'].join(", ")}"
148 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"
152 def enable(m, params)
153 @bot.config['markov.enabled'] = true
157 def probability(m, params)
158 if params[:probability]
159 @bot.config['markov.probability'] = params[:probability].to_i
162 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
166 def disable(m, params)
167 @bot.config['markov.enabled'] = false
172 return false unless @bot.config['markov.enabled']
174 return true if prob > rand(100)
182 def random_markov(m, message)
183 return unless should_talk
185 word1, word2 = message.split(/\s+/)
186 return unless word1 and word2
187 line = generate_string(word1, word2)
189 # we do nothing if the line we return is just an initial substring
190 # of the line we received
191 return if message.index(line) == 0
192 @bot.timer.add_once(delay) {
193 m.reply line, :nick => false, :to => :public
198 line = generate_string(params[:seed1], params[:seed2])
199 if line != "#{params[:seed1]} #{params[:seed2]}"
206 def rand_chat(m, params)
207 # pick a random pair from the db and go from there
208 word1, word2 = :nonword, :nonword
211 wordlist = @registry["#{word1} #{word2}"]
212 break if wordlist.empty?
213 word3 = wordlist[rand(wordlist.length)]
214 break if word3 == :nonword
216 word1, word2 = word2, word3
219 m.reply output.join(" ")
228 # in channel message, the kind we are interested in
229 message = clean_str m.plainmessage
232 message = "#{m.sourcenick} #{message}"
235 @learning_queue.push message
236 random_markov(m, message) unless m.replied?
240 # debug "learning #{message}"
241 wordlist = message.split(/\s+/)
242 return unless wordlist.length >= 2
243 word1, word2 = :nonword, :nonword
244 wordlist.each do |word3|
245 k = "#{word1} #{word2}"
246 @registry[k] = @registry[k].push(word3)
247 word1, word2 = word2, word3
249 k = "#{word1} #{word2}"
250 @registry[k] = @registry[k].push(:nonword)
254 plugin = MarkovPlugin.new
255 plugin.map 'markov ignore :action :option', :action => "ignore"
256 plugin.map 'markov ignore :action', :action => "ignore"
257 plugin.map 'markov ignore', :action => "ignore"
258 plugin.map 'markov enable', :action => "enable"
259 plugin.map 'markov disable', :action => "disable"
260 plugin.map 'markov status', :action => "status"
261 plugin.map 'chat about :seed1 :seed2', :action => "chat"
262 plugin.map 'chat', :action => "rand_chat"
263 plugin.map 'markov probability [:probability]', :action => "probability",
264 :requirements => {:probability => /^\d+%?$/}
266 plugin.default_auth('ignore', false)
267 plugin.default_auth('probability', false)