]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/markov.rb
iplookup plugin: don't block
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / markov.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Markov plugin
5 #
6 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
7 # Copyright:: (C) 2005 Tom Gilbert
8 #
9 # Contribute to chat with random phrases built from word sequences learned
10 # by listening to chat
11
12 class MarkovPlugin < Plugin
13   Config.register Config::BooleanValue.new('markov.enabled',
14     :default => false,
15     :desc => "Enable and disable the plugin")
16   Config.register Config::IntegerValue.new('markov.probability',
17     :default => 25,
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',
21     :default => [],
22     :desc => "Hostmasks and channel names markov should NOT learn from (e.g. idiot*!*@*, #privchan).")
23
24   def initialize
25     super
26     @registry.set_default([])
27     if @registry.has_key?('enabled')
28       @bot.config['markov.enabled'] = @registry['enabled']
29       @registry.delete('enabled')
30     end
31     if @registry.has_key?('probability')
32       @bot.config['markov.probability'] = @registry['probability']
33       @registry.delete('probability')
34     end
35     if @bot.config['markov.ignore_users']
36       debug "moving markov.ignore_users to markov.ignore"
37       @bot.config['markov.ignore'] = @bot.config['markov.ignore_users'].dup
38       @bot.config.delete('markov.ignore_users')
39     end
40     @learning_queue = Queue.new
41     @learning_thread = Thread.new do
42       while s = @learning_queue.pop
43         learn s
44         sleep 0.5
45       end
46     end
47     @learning_thread.priority = -1
48   end
49
50   def cleanup
51     debug 'closing learning thread'
52     @learning_queue.push nil
53     @learning_thread.join
54     debug 'learning thread closed'
55   end
56
57   def generate_string(word1, word2)
58     # limit to max of 50 words
59     output = word1 + " " + word2
60
61     # try to avoid :nonword in the first iteration
62     wordlist = @registry["#{word1} #{word2}"]
63     wordlist.delete(:nonword)
64     if not wordlist.empty?
65       word3 = wordlist[rand(wordlist.length)]
66       output = output + " " + word3
67       word1, word2 = word2, word3
68     end
69
70     49.times do
71       wordlist = @registry["#{word1} #{word2}"]
72       break if wordlist.empty?
73       word3 = wordlist[rand(wordlist.length)]
74       break if word3 == :nonword
75       output = output + " " + word3
76       word1, word2 = word2, word3
77     end
78     return output
79   end
80
81   def help(plugin, topic="")
82     "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)"
83   end
84
85   def clean_str(s)
86     str = s.dup
87     str.gsub!(/^\S+[:,;]/, "")
88     str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
89     return str.strip
90   end
91
92   def probability?
93     return @bot.config['markov.probability']
94   end
95
96   def status(m,params)
97     if @bot.config['markov.enabled']
98       m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
99     else
100       m.reply "markov is currently disabled"
101     end
102   end
103
104   def ignore?(m=nil)
105     return false unless m
106     return true if m.address? or m.private?
107     @bot.config['markov.ignore'].each do |mask|
108       return true if m.channel.downcase == mask.downcase
109       return true if m.source.matches?(mask)
110     end
111     return false
112   end
113
114   def ignore(m, params)
115     action = params[:action]
116     user = params[:option]
117     case action
118     when 'remove':
119       if @bot.config['markov.ignore'].include? user
120         s = @bot.config['markov.ignore']
121         s.delete user
122         @bot.config['ignore'] = s
123         m.reply "#{user} removed"
124       else
125         m.reply "not found in list"
126       end
127     when 'add':
128       if user
129         if @bot.config['markov.ignore'].include?(user)
130           m.reply "#{user} already in list"
131         else
132           @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
133           m.reply "#{user} added to markov ignore list"
134         end
135       else
136         m.reply "give the name of a person or channel to ignore"
137       end
138     when 'list':
139       m.reply "I'm ignoring #{@bot.config['markov.ignore'].join(", ")}"
140     else
141       m.reply "have markov ignore the input from a hostmask or a channel.  usage: markov ignore add <mask or channel>; markov ignore remove <mask or channel>; markov ignore list"
142     end
143   end
144
145   def enable(m, params)
146     @bot.config['markov.enabled'] = true
147     m.okay
148   end
149
150   def probability(m, params)
151     if params[:probability]
152       @bot.config['markov.probability'] = params[:probability].to_i
153       m.okay
154     else
155       m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
156     end
157   end
158
159   def disable(m, params)
160     @bot.config['markov.enabled'] = false
161     m.okay
162   end
163
164   def should_talk
165     return false unless @bot.config['markov.enabled']
166     prob = probability?
167     return true if prob > rand(100)
168     return false
169   end
170
171   def delay
172     1 + rand(5)
173   end
174
175   def random_markov(m, message)
176     return unless should_talk
177
178     word1, word2 = message.split(/\s+/)
179     return unless word1 and word2
180     line = generate_string(word1, word2)
181     return unless line
182     return if line == message
183     @bot.timer.add_once(delay) {
184       m.reply line
185     }
186   end
187
188   def chat(m, params)
189     line = generate_string(params[:seed1], params[:seed2])
190     if line != "#{params[:seed1]} #{params[:seed2]}"
191       m.reply line 
192     else
193       m.reply "I can't :("
194     end
195   end
196
197   def rand_chat(m, params)
198     # pick a random pair from the db and go from there
199     word1, word2 = :nonword, :nonword
200     output = Array.new
201     50.times do
202       wordlist = @registry["#{word1} #{word2}"]
203       break if wordlist.empty?
204       word3 = wordlist[rand(wordlist.length)]
205       break if word3 == :nonword
206       output << word3
207       word1, word2 = word2, word3
208     end
209     if output.length > 1
210       m.reply output.join(" ")
211     else
212       m.reply "I can't :("
213     end
214   end
215   
216   def message(m)
217     return if ignore? m
218
219     # in channel message, the kind we are interested in
220     message = clean_str m.plainmessage
221
222     if m.action?
223       message = "#{m.sourcenick} #{message}"
224     end
225     
226     @learning_queue.push message
227     random_markov(m, message) unless m.replied?
228   end
229
230   def learn(message)
231     # debug "learning #{message}"
232     wordlist = message.split(/\s+/)
233     return unless wordlist.length >= 2
234     word1, word2 = :nonword, :nonword
235     wordlist.each do |word3|
236       k = "#{word1} #{word2}"
237       @registry[k] = @registry[k].push(word3)
238       word1, word2 = word2, word3
239     end
240     k = "#{word1} #{word2}"
241     @registry[k] = @registry[k].push(:nonword)
242   end
243 end
244
245 plugin = MarkovPlugin.new
246 plugin.map 'markov ignore :action :option', :action => "ignore"
247 plugin.map 'markov ignore :action', :action => "ignore"
248 plugin.map 'markov ignore', :action => "ignore"
249 plugin.map 'markov enable', :action => "enable"
250 plugin.map 'markov disable', :action => "disable"
251 plugin.map 'markov status', :action => "status"
252 plugin.map 'chat about :seed1 :seed2', :action => "chat"
253 plugin.map 'chat', :action => "rand_chat"
254 plugin.map 'markov probability [:probability]', :action => "probability",
255            :requirements => {:probability => /^\d+%?$/}