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 return @registry['ignore_users'].include?(user)
57 if @registry['ignore_users'].nil?
58 @registry['ignore_users'] = []
60 action = params[:action]
61 user = params[:option]
64 if @registry['ignore_users'].include? user
65 s = @registry['ignore_users']
67 @registry['ignore_users'] = s
68 m.reply "#{user} removed"
70 m.reply "not found in list"
74 if @registry['ignore_users'].include?(user)
75 m.reply "#{user} already in list"
77 @registry['ignore_users'] = @registry['ignore_users'].push user
78 m.reply "#{user} added to markov ignore list"
81 m.reply "give the name of a person to ignore"
84 m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
86 m.reply "have markov ignore the input from a hostmask. usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
91 @registry['enabled'] = true
95 def probability(m, params)
96 @registry['probability'] = params[:probability].to_i
100 def disable(m, params)
101 @registry['enabled'] = false
106 return false unless @registry['enabled']
108 return true if prob > rand(100)
112 def random_markov(m, message)
113 return unless should_talk
114 line = generate_string(message)
116 m.reply line unless line == message
120 seed = "#{params[:seed1]} #{params[:seed2]}"
121 line = generate_string seed
129 def rand_chat(m, params)
130 # pick a random pair from the db and go from there
131 word1, word2 = :nonword, :nonword
134 wordlist = @registry["#{word1}/#{word2}"]
135 break if wordlist.empty?
136 word3 = wordlist[rand(wordlist.length)]
137 break if word3 == :nonword
139 word1, word2 = word2, word3
142 m.reply output.join(" ")
149 return unless m.kind_of?(PrivMessage) && m.public?
151 return if ignore? m.source
153 # in channel message, the kind we are interested in
154 message = clean_str m.message
156 # we respond first. otherwise if we add this line to the db first, and
157 # it's fairly unique, there's a good chance we'll just parrot it back
159 random_markov(m, message)
161 wordlist = message.split(/\s+/)
162 return unless wordlist.length > 2
164 word1, word2 = :nonword, :nonword
165 wordlist.each do |word3|
166 @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
167 word1, word2 = word2, word3
169 @registry["#{word1}/#{word2}"] = [:nonword]
172 plugin = MarkovPlugin.new
173 plugin.map 'markov ignore :action :option', :action => "ignore"
174 plugin.map 'markov ignore :action', :action => "ignore"
175 plugin.map 'markov ignore', :action => "ignore"
176 plugin.map 'markov enable', :action => "enable"
177 plugin.map 'markov disable', :action => "disable"
178 plugin.map 'markov status', :action => "status"
179 plugin.map 'chat about :seed1 :seed2', :action => "chat"
180 plugin.map 'chat', :action => "rand_chat"
181 plugin.map 'markov probability :probability', :action => "probability",
182 :requirements => {:probability => /^\d+$/}