1 class MarkovPlugin < Plugin
4 @registry.set_default([])
5 @registry['enabled'] = false unless @registry.has_key?('enabled')
9 def generate_string(seedline)
10 # limit to max of 50 words
11 return unless seedline
12 word1, word2 = seedline.split(/\s+/)
13 output = word1 + " " + word2
15 wordlist = @registry["#{word1}/#{word2}"]
16 break if wordlist.empty?
17 word3 = wordlist[rand(wordlist.length)]
18 break if word3 == :nonword
19 output = output + " " + word3
20 word1, word2 = word2, word3
25 def help(plugin, topic="")
26 "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' => set the % chance of rbot responding to input, 'chat' => try and say something intelligent, 'chat about <foo> <bar>' => riff on a word pair (if possible)"
31 str.gsub!(/^\S+[:,;]/, "")
32 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
37 prob = @registry['probability']
38 prob = 25 if prob.kind_of? Array;
40 prob = 100 if prob > 100
45 enabled = @registry['enabled']
47 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
49 m.reply "markov is currently disabled"
54 @registry['ignore_users'].each do |mask|
55 return true if Irc.netmaskmatch mask, user
61 if @registry['ignore_users'].nil?
62 @registry['ignore_users'] = []
64 action = params[:action]
65 user = params[:option]
68 if @registry['ignore_users'].include? user
69 s = @registry['ignore_users']
71 @registry['ignore_users'] = s
72 m.reply "#{user} removed"
74 m.reply "not found in list"
78 if @registry['ignore_users'].include?(user)
79 m.reply "#{user} already in list"
81 @registry['ignore_users'] = @registry['ignore_users'].push user
82 m.reply "#{user} added to markov ignore list"
85 m.reply "give the name of a person to ignore"
88 m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
90 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
95 @registry['enabled'] = true
99 def probability(m, params)
100 @registry['probability'] = params[:probability].to_i
104 def disable(m, params)
105 @registry['enabled'] = false
110 return false unless @registry['enabled']
112 return true if prob > rand(100)
120 def random_markov(m, message)
121 return unless should_talk
122 line = generate_string(message)
124 return if line == message
125 @bot.timer.add_once(delay, m) {|m|
131 seed = "#{params[:seed1]} #{params[:seed2]}"
132 line = generate_string seed
140 def rand_chat(m, params)
141 # pick a random pair from the db and go from there
142 word1, word2 = :nonword, :nonword
145 wordlist = @registry["#{word1}/#{word2}"]
146 break if wordlist.empty?
147 word3 = wordlist[rand(wordlist.length)]
148 break if word3 == :nonword
150 word1, word2 = word2, word3
153 m.reply output.join(" ")
160 return unless m.kind_of?(PrivMessage) && m.public?
162 return if ignore? m.source
164 # in channel message, the kind we are interested in
165 message = clean_str m.message
168 message = "#{m.sourcenick} #{message}"
171 wordlist = message.split(/\s+/)
172 return unless wordlist.length >= 2
174 word1, word2 = :nonword, :nonword
175 wordlist.each do |word3|
176 @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
177 word1, word2 = word2, word3
179 @registry["#{word1}/#{word2}"] = [:nonword]
182 random_markov(m, message)
185 plugin = MarkovPlugin.new
186 plugin.map 'markov ignore :action :option', :action => "ignore"
187 plugin.map 'markov ignore :action', :action => "ignore"
188 plugin.map 'markov ignore', :action => "ignore"
189 plugin.map 'markov enable', :action => "enable"
190 plugin.map 'markov disable', :action => "disable"
191 plugin.map 'markov status', :action => "status"
192 plugin.map 'chat about :seed1 :seed2', :action => "chat"
193 plugin.map 'chat', :action => "rand_chat"
194 plugin.map 'markov probability :probability', :action => "probability",
195 :requirements => {:probability => /^\d+$/}