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
15 @registry.set_default([])
16 @registry['enabled'] = false unless @registry.has_key?('enabled')
20 def generate_string(word1, word2)
21 # limit to max of 50 words
22 output = word1 + " " + word2
24 # try to avoid :nonword in the first iteration
25 wordlist = @registry["#{word1} #{word2}"]
26 wordlist.delete(:nonword)
27 if not wordlist.empty?
28 word3 = wordlist[rand(wordlist.length)]
29 output = output + " " + word3
30 word1, word2 = word2, word3
34 wordlist = @registry["#{word1} #{word2}"]
35 break if wordlist.empty?
36 word3 = wordlist[rand(wordlist.length)]
37 break if word3 == :nonword
38 output = output + " " + word3
39 word1, word2 = word2, word3
44 def help(plugin, topic="")
45 "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)"
50 str.gsub!(/^\S+[:,;]/, "")
51 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
56 prob = @registry['probability']
57 prob = 25 if prob.kind_of? Array;
59 prob = 100 if prob > 100
64 enabled = @registry['enabled']
66 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
68 m.reply "markov is currently disabled"
73 return false unless user
74 @registry['ignore_users'].each do |mask|
75 return true if user.matches?(mask)
81 if @registry['ignore_users'].nil?
82 @registry['ignore_users'] = []
84 action = params[:action]
85 user = params[:option]
88 if @registry['ignore_users'].include? user
89 s = @registry['ignore_users']
91 @registry['ignore_users'] = s
92 m.reply "#{user} removed"
94 m.reply "not found in list"
98 if @registry['ignore_users'].include?(user)
99 m.reply "#{user} already in list"
101 @registry['ignore_users'] = @registry['ignore_users'].push user
102 m.reply "#{user} added to markov ignore list"
105 m.reply "give the name of a person to ignore"
108 m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
110 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
114 def enable(m, params)
115 @registry['enabled'] = true
119 def probability(m, params)
120 if params[:probability]
121 @registry['probability'] = params[:probability].to_i
124 m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
128 def disable(m, params)
129 @registry['enabled'] = false
134 return false unless @registry['enabled']
136 return true if prob > rand(100)
144 def random_markov(m, message)
145 return unless should_talk
147 word1, word2 = message.split(/\s+/)
148 line = generate_string(word1, word2)
150 return if line == message
151 @bot.timer.add_once(delay) {
157 line = generate_string(params[:seed1], params[:seed2])
158 if line != "#{params[:seed1]} #{params[:seed2]}"
165 def rand_chat(m, params)
166 # pick a random pair from the db and go from there
167 word1, word2 = :nonword, :nonword
170 wordlist = @registry["#{word1} #{word2}"]
171 break if wordlist.empty?
172 word3 = wordlist[rand(wordlist.length)]
173 break if word3 == :nonword
175 word1, word2 = word2, word3
178 m.reply output.join(" ")
185 return unless m.kind_of?(PrivMessage) && m.public?
187 return if ignore? m.source
189 # in channel message, the kind we are interested in
190 message = clean_str m.message
193 message = "#{m.sourcenick} #{message}"
196 wordlist = message.split(/\s+/)
197 return unless wordlist.length >= 2
199 word1, word2 = :nonword, :nonword
200 wordlist.each do |word3|
201 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(word3)
202 word1, word2 = word2, word3
204 @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(:nonword)
207 random_markov(m, message)
211 plugin = MarkovPlugin.new
212 plugin.map 'markov ignore :action :option', :action => "ignore"
213 plugin.map 'markov ignore :action', :action => "ignore"
214 plugin.map 'markov ignore', :action => "ignore"
215 plugin.map 'markov enable', :action => "enable"
216 plugin.map 'markov disable', :action => "disable"
217 plugin.map 'markov status', :action => "status"
218 plugin.map 'chat about :seed1 :seed2', :action => "chat"
219 plugin.map 'chat', :action => "rand_chat"
220 plugin.map 'markov probability [:probability]', :action => "probability",
221 :requirements => {:probability => /^\d+%?$/}