1 class MarkovPlugin < Plugin
4 @registry.set_default([])
8 def generate_string(seedline)
9 # limit to max of 50 words
10 return unless seedline
11 word1, word2 = seedline.split(/\s+/)
12 output = word1 + " " + word2
14 wordlist = @registry["#{word1}/#{word2}"]
15 break if wordlist.empty?
16 word3 = wordlist[rand(wordlist.length)]
17 break if word3 == :nonword
18 output = output + " " + word3
19 word1, word2 = word2, word3
24 def help(plugin, topic="")
25 "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)"
30 str.gsub!(/^\S+[:,;]/, "")
31 str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
36 prob = @registry['probability']
37 prob = 25 if prob.kind_of? Array;
39 prob = 100 if prob > 100
44 enabled = @registry['enabled']
46 m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
48 m.reply "markov is currently disabled"
53 @registry['ignore_users'].each do |mask|
54 return true if Irc.netmaskmatch mask, user
60 if @registry['ignore_users'].nil?
61 @registry['ignore_users'] = []
63 action = params[:action]
64 user = params[:option]
67 if @registry['ignore_users'].include? user
68 s = @registry['ignore_users']
70 @registry['ignore_users'] = s
71 m.reply "#{user} removed"
73 m.reply "not found in list"
77 if @registry['ignore_users'].include?(user)
78 m.reply "#{user} already in list"
80 @registry['ignore_users'] = @registry['ignore_users'].push user
81 m.reply "#{user} added to markov ignore list"
84 m.reply "give the name of a person to ignore"
87 m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
89 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
94 @registry['enabled'] = true
98 def probability(m, params)
99 @registry['probability'] = params[:probability].to_i
103 def disable(m, params)
104 @registry['enabled'] = false
109 return false unless @registry['enabled']
111 return true if prob > rand(100)
115 def random_markov(m, message)
116 return unless should_talk
117 line = generate_string(message)
119 m.reply line unless line == message
123 seed = "#{params[:seed1]} #{params[:seed2]}"
124 line = generate_string seed
132 def rand_chat(m, params)
133 # pick a random pair from the db and go from there
134 word1, word2 = :nonword, :nonword
137 wordlist = @registry["#{word1}/#{word2}"]
138 break if wordlist.empty?
139 word3 = wordlist[rand(wordlist.length)]
140 break if word3 == :nonword
142 word1, word2 = word2, word3
145 m.reply output.join(" ")
152 return unless m.kind_of?(PrivMessage) && m.public?
154 return if ignore? m.source
156 # in channel message, the kind we are interested in
157 message = clean_str m.message
159 wordlist = message.split(/\s+/)
160 return unless wordlist.length >= 2
162 word1, word2 = :nonword, :nonword
163 wordlist.each do |word3|
164 @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
165 word1, word2 = word2, word3
167 @registry["#{word1}/#{word2}"] = [:nonword]
170 random_markov(m, message)
173 plugin = MarkovPlugin.new
174 plugin.map 'markov ignore :action :option', :action => "ignore"
175 plugin.map 'markov ignore :action', :action => "ignore"
176 plugin.map 'markov ignore', :action => "ignore"
177 plugin.map 'markov enable', :action => "enable"
178 plugin.map 'markov disable', :action => "disable"
179 plugin.map 'markov status', :action => "status"
180 plugin.map 'chat about :seed1 :seed2', :action => "chat"
181 plugin.map 'chat', :action => "rand_chat"
182 plugin.map 'markov probability :probability', :action => "probability",
183 :requirements => {:probability => /^\d+$/}