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')
38 def generate_string(word1, word2)
39 # limit to max of 50 words
40 output = word1 + " " + word2
42 # try to avoid :nonword in the first iteration
43 wordlist = @registry["#{word1} #{word2}"]
44 wordlist.delete(:nonword)
45 if not wordlist.empty?
46 word3 = wordlist[rand(wordlist.length)]
47 output = output + " " + word3
48 word1, word2 = word2, word3
52 wordlist = @registry["#{word1} #{word2}"]
53 break if wordlist.empty?
54 word3 = wordlist[rand(wordlist.length)]
55 break if word3 == :nonword
56 output = output + " " + word3
57 word1, word2 = word2, word3
62 def help(plugin, topic="")
63 "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)"
68 str.gsub!(/^\S+[:,;]/, "")
69 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
74 return @bot.config['markov.probability']
78 if @bot.config['markov.enabled']
79 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
81 m.reply "markov is currently disabled"
86 return false unless user
87 @bot.config['markov.ignore_users'].each do |mask|
88 return true if user.matches?(mask)
94 action = params[:action]
95 user = params[:option]
98 if @bot.config['markov.ignore_users'].include? user
99 s = @bot.config['markov.ignore_users']
101 @bot.config['ignore_users'] = s
102 m.reply "#{user} removed"
104 m.reply "not found in list"
108 if @bot.config['markov.ignore_users'].include?(user)
109 m.reply "#{user} already in list"
111 @bot.config['markov.ignore_users'] = @bot.config['markov.ignore_users'].push user
112 m.reply "#{user} added to markov ignore list"
115 m.reply "give the name of a person to ignore"
118 m.reply "I'm ignoring #{@bot.config['markov.ignore_users'].join(", ")}"
120 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
124 def enable(m, params)
125 @bot.config['markov.enabled'] = true
129 def probability(m, params)
130 if params[:probability]
131 @bot.config['markov.probability'] = params[:probability].to_i
134 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
138 def disable(m, params)
139 @bot.config['markov.enabled'] = false
144 return false unless @bot.config['markov.enabled']
146 return true if prob > rand(100)
154 def random_markov(m, message)
155 return unless should_talk
157 word1, word2 = message.split(/\s+/)
158 line = generate_string(word1, word2)
160 return if line == message
161 @bot.timer.add_once(delay) {
167 line = generate_string(params[:seed1], params[:seed2])
168 if line != "#{params[:seed1]} #{params[:seed2]}"
175 def rand_chat(m, params)
176 # pick a random pair from the db and go from there
177 word1, word2 = :nonword, :nonword
180 wordlist = @registry["#{word1} #{word2}"]
181 break if wordlist.empty?
182 word3 = wordlist[rand(wordlist.length)]
183 break if word3 == :nonword
185 word1, word2 = word2, word3
188 m.reply output.join(" ")
195 return unless m.kind_of?(PrivMessage) && m.public?
197 return if ignore? m.source
199 # in channel message, the kind we are interested in
200 message = clean_str m.message
203 message = "#{m.sourcenick} #{message}"
206 wordlist = message.split(/\s+/)
207 return unless wordlist.length >= 2
209 word1, word2 = :nonword, :nonword
210 wordlist.each do |word3|
211 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(word3)
212 word1, word2 = word2, word3
214 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(:nonword)
217 random_markov(m, message)
221 plugin = MarkovPlugin.new
222 plugin.map 'markov ignore :action :option', :action => "ignore"
223 plugin.map 'markov ignore :action', :action => "ignore"
224 plugin.map 'markov ignore', :action => "ignore"
225 plugin.map 'markov enable', :action => "enable"
226 plugin.map 'markov disable', :action => "disable"
227 plugin.map 'markov status', :action => "status"
228 plugin.map 'chat about :seed1 :seed2', :action => "chat"
229 plugin.map 'chat', :action => "rand_chat"
230 plugin.map 'markov probability [:probability]', :action => "probability",
231 :requirements => {:probability => /^\d+%?$/}