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')
37 def generate_string(word1, word2)
38 # limit to max of 50 words
39 output = word1 + " " + word2
41 # try to avoid :nonword in the first iteration
42 wordlist = @registry["#{word1} #{word2}"]
43 wordlist.delete(:nonword)
44 if not wordlist.empty?
45 word3 = wordlist[rand(wordlist.length)]
46 output = output + " " + word3
47 word1, word2 = word2, word3
51 wordlist = @registry["#{word1} #{word2}"]
52 break if wordlist.empty?
53 word3 = wordlist[rand(wordlist.length)]
54 break if word3 == :nonword
55 output = output + " " + word3
56 word1, word2 = word2, word3
61 def help(plugin, topic="")
62 "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)"
67 str.gsub!(/^\S+[:,;]/, "")
68 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
73 return @bot.config['markov.probability']
77 if @bot.config['markov.enabled']
78 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
80 m.reply "markov is currently disabled"
85 return false unless user
86 @bot.config['markov.ignore_users'].each do |mask|
87 return true if user.matches?(mask)
93 action = params[:action]
94 user = params[:option]
97 if @bot.config['markov.ignore_users'].include? user
98 s = @bot.config['markov.ignore_users']
100 @bot.config['ignore_users'] = s
101 m.reply "#{user} removed"
103 m.reply "not found in list"
107 if @bot.config['markov.ignore_users'].include?(user)
108 m.reply "#{user} already in list"
110 @bot.config['markov.ignore_users'] = @bot.config['markov.ignore_users'].push user
111 m.reply "#{user} added to markov ignore list"
114 m.reply "give the name of a person to ignore"
117 m.reply "I'm ignoring #{@bot.config['markov.ignore_users'].join(", ")}"
119 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
123 def enable(m, params)
124 @bot.config['markov.enabled'] = true
128 def probability(m, params)
129 if params[:probability]
130 @bot.config['markov.probability'] = params[:probability].to_i
133 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
137 def disable(m, params)
138 @bot.config['markov.enabled'] = false
143 return false unless @bot.config['markov.enabled']
145 return true if prob > rand(100)
153 def random_markov(m, message)
154 return unless should_talk
156 word1, word2 = message.split(/\s+/)
157 line = generate_string(word1, word2)
159 return if line == message
160 @bot.timer.add_once(delay) {
166 line = generate_string(params[:seed1], params[:seed2])
167 if line != "#{params[:seed1]} #{params[:seed2]}"
174 def rand_chat(m, params)
175 # pick a random pair from the db and go from there
176 word1, word2 = :nonword, :nonword
179 wordlist = @registry["#{word1} #{word2}"]
180 break if wordlist.empty?
181 word3 = wordlist[rand(wordlist.length)]
182 break if word3 == :nonword
184 word1, word2 = word2, word3
187 m.reply output.join(" ")
194 return unless m.kind_of?(PrivMessage) && m.public?
196 return if ignore? m.source
198 # in channel message, the kind we are interested in
199 message = clean_str m.message
202 message = "#{m.sourcenick} #{message}"
205 wordlist = message.split(/\s+/)
206 return unless wordlist.length >= 2
208 word1, word2 = :nonword, :nonword
209 wordlist.each do |word3|
210 k = "#{word1} #{word2}"
211 @registry[k] = @registry[k].push(word3)
212 word1, word2 = word2, word3
214 k = "#{word1} #{word2}"
215 @registry[k] = @registry[k].push(:nonword)
217 random_markov(m, message) unless m.replied?
222 plugin = MarkovPlugin.new
223 plugin.map 'markov ignore :action :option', :action => "ignore"
224 plugin.map 'markov ignore :action', :action => "ignore"
225 plugin.map 'markov ignore', :action => "ignore"
226 plugin.map 'markov enable', :action => "enable"
227 plugin.map 'markov disable', :action => "disable"
228 plugin.map 'markov status', :action => "status"
229 plugin.map 'chat about :seed1 :seed2', :action => "chat"
230 plugin.map 'chat', :action => "rand_chat"
231 plugin.map 'markov probability [:probability]', :action => "probability",
232 :requirements => {:probability => /^\d+%?$/}