]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/markov.rb
markov: made delay an option, fixed non-replied status
[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   Config.register Config::IntegerValue.new('markov.max_words',
24     :default => 50,
25     :validate => Proc.new { |v| (0..100).include? v },
26     :desc => "Maximum number of words the bot should put in a sentence")
27   Config.register Config::IntegerValue.new('markov.learn_delay',
28     :default => 0.5,
29     :validate => Proc.new { |v| v >= 0 },
30     :desc => "Time the learning thread spends sleeping after learning a line. If set to zero, learning from files can be very CPU intensive, but also faster.")
31    Config.register Config::IntegerValue.new('markov.delay',
32     :default => true,
33     :validate => Proc.new { |v| v >= 0 },
34     :desc => "Wait short time before contributing to conversation.")
35
36   MARKER = :"\r\n"
37
38   # upgrade a registry entry from 0.9.14 and earlier, converting the Arrays
39   # into Hashes of weights
40   def upgrade_entry(k, logfile)
41     logfile.puts "\t#{k.inspect}"
42     logfile.flush
43     logfile.fsync
44
45     ar = @registry[k]
46
47     # wipe the current key
48     @registry.delete(k)
49
50     # discard empty keys
51     if ar.empty?
52       logfile.puts "\tEMPTY"
53       return
54     end
55
56     # otherwise, proceed
57     logfile.puts "\t#{ar.inspect}"
58
59     # re-encode key to UTF-8 and cleanup as needed
60     words = k.split.map do |w|
61       BasicUserMessage.strip_formatting(
62         @bot.socket.filter.in(w)
63       ).sub(/\001$/,'')
64     end
65
66     # old import that failed to split properly?
67     if words.length == 1 and words.first.include? '/'
68       # split at the last /
69       unsplit = words.first
70       at = unsplit.rindex('/')
71       words = [unsplit[0,at], unsplit[at+1..-1]]
72     end
73
74     # if any of the re-split/re-encoded words have spaces,
75     # or are empty, we would get a chain we can't convert,
76     # so drop it
77     if words.first.empty? or words.first.include?(' ') or
78       words.last.empty? or words.last.include?(' ')
79       logfile.puts "\tSKIPPED"
80       return
81     end
82
83     # former unclean CTCP, we can't convert this
84     if words.first[0] == 1
85       logfile.puts "\tSKIPPED"
86       return
87     end
88
89     # nonword CTCP => SKIP
90     # someword CTCP => nonword someword
91     if words.last[0] == 1
92       if words.first == "nonword"
93         logfile.puts "\tSKIPPED"
94         return
95       end
96       words.unshift MARKER
97       words.pop
98     end
99
100     # intern the old keys
101     words.map! do |w|
102       ['nonword', MARKER].include?(w) ? MARKER : w.chomp("\001")
103     end
104
105     newkey = words.join(' ')
106     logfile.puts "\t#{newkey.inspect}"
107
108     # the new key exists already, so we want to merge
109     if k != newkey and @registry.key? newkey
110       ar2 = @registry[newkey]
111       logfile.puts "\tMERGE"
112       logfile.puts "\t\t#{ar2.inspect}"
113       ar.push(*ar2)
114       # and get rid of the key
115       @registry.delete(newkey)
116     end
117
118     total = 0
119     hash = Hash.new(0)
120
121     @chains_mutex.synchronize do
122       if @chains.key? newkey
123         ar2 = @chains[newkey]
124         total += ar2.first
125         hash.update ar2.last
126       end
127
128       ar.each do |word|
129         case word
130         when :nonword
131           # former marker
132           sym = MARKER
133         else
134           # we convert old words into UTF-8, cleanup, resplit if needed,
135           # and only get the first word. we may lose some data for old
136           # missplits, but this is the best we can do
137           w = BasicUserMessage.strip_formatting(
138             @bot.socket.filter.in(word).split.first
139           )
140           case w
141           when /^\001\S+$/, "\001", ""
142             # former unclean CTCP or end of CTCP
143             next
144           else
145             # intern after clearing leftover end-of-actions if present
146             sym = w.chomp("\001").intern
147           end
148         end
149         hash[sym] += 1
150         total += 1
151       end
152       if hash.empty?
153         logfile.puts "\tSKIPPED"
154         return
155       end
156       logfile.puts "\t#{[total, hash].inspect}"
157       @chains[newkey] = [total, hash]
158     end
159   end
160
161   def upgrade_registry
162     # we load all the keys and then iterate over this array because
163     # running each() on the registry and updating it at the same time
164     # doesn't work
165     keys = @registry.keys
166     # no registry, nothing to do
167     return if keys.empty?
168
169     ki = 0
170     log "starting markov database conversion thread (v1 to v2, #{keys.length} keys)"
171
172     keys.each { |k| @upgrade_queue.push k }
173     @upgrade_queue.push nil
174
175     @upgrade_thread = Thread.new do
176       logfile = File.open(@bot.path('markov-conversion.log'), 'a')
177       logfile.puts "=== conversion thread started #{Time.now} ==="
178       while k = @upgrade_queue.pop
179         ki += 1
180         logfile.puts "Key #{ki} (#{@upgrade_queue.length} in queue):"
181         begin
182           upgrade_entry(k, logfile)
183         rescue Exception => e
184           logfile.puts "=== ERROR ==="
185           logfile.puts e.pretty_inspect
186           logfile.puts "=== EREND ==="
187         end
188         sleep @bot.config['markov.learn_delay'] unless @bot.config['markov.learn_delay'].zero?
189       end
190       logfile.puts "=== conversion thread stopped #{Time.now} ==="
191       logfile.close
192     end
193     @upgrade_thread.priority = -1
194   end
195
196   attr_accessor :chains
197
198   def initialize
199     super
200     @registry.set_default([])
201     if @registry.has_key?('enabled')
202       @bot.config['markov.enabled'] = @registry['enabled']
203       @registry.delete('enabled')
204     end
205     if @registry.has_key?('probability')
206       @bot.config['markov.probability'] = @registry['probability']
207       @registry.delete('probability')
208     end
209     if @bot.config['markov.ignore_users']
210       debug "moving markov.ignore_users to markov.ignore"
211       @bot.config['markov.ignore'] = @bot.config['markov.ignore_users'].dup
212       @bot.config.delete('markov.ignore_users'.to_sym)
213     end
214
215     @chains = @registry.sub_registry('v2')
216     @chains.set_default([])
217     @chains_mutex = Mutex.new
218
219     @upgrade_queue = Queue.new
220     @upgrade_thread = nil
221     upgrade_registry
222
223     @learning_queue = Queue.new
224     @learning_thread = Thread.new do
225       while s = @learning_queue.pop
226         learn_line s
227         sleep @bot.config['markov.learn_delay'] unless @bot.config['markov.learn_delay'].zero?
228       end
229     end
230     @learning_thread.priority = -1
231   end
232
233   def cleanup
234     if @upgrade_thread and @upgrade_thread.alive?
235       debug 'closing conversion thread'
236       @upgrade_queue.clear
237       @upgrade_queue.push nil
238       @upgrade_thread.join
239       debug 'conversion thread closed'
240     end
241
242     debug 'closing learning thread'
243     @learning_queue.push nil
244     @learning_thread.join
245     debug 'learning thread closed'
246   end
247
248   # if passed a pair, pick a word from the registry using the pair as key.
249   # otherwise, pick a word from an given list
250   def pick_word(word1, word2=MARKER)
251     if word1.kind_of? Array
252       wordlist = word1
253     else
254       k = "#{word1} #{word2}"
255       return MARKER unless @chains.key? k
256       wordlist = @chains[k]
257     end
258     total = wordlist.first
259     hash = wordlist.last
260     return MARKER if total == 0
261     return hash.keys.first if hash.length == 1
262     hit = rand(total)
263     ret = MARKER
264     hash.each do |k, w|
265       hit -= w
266       if hit < 0
267         ret = k
268         break
269       end
270     end
271     return ret
272   end
273
274   def generate_string(word1, word2)
275     # limit to max of markov.max_words words
276     if word2
277       output = "#{word1} #{word2}"
278     else
279       output = word1.to_s
280     end
281
282     if @chains.key? output
283       wordlist = @chains[output]
284       wordlist.last.delete(MARKER)
285     else
286       output.downcase!
287       keys = []
288       @chains.each_key(output) do |key|
289         if key.downcase.include? output
290           keys << key
291         else
292           break
293         end
294       end
295       if keys.empty?
296         keys = @chains.keys.select { |k| k.downcase.include? output }
297       end
298       return nil if keys.empty?
299       while key = keys.delete_one
300         wordlist = @chains[key]
301         wordlist.last.delete(MARKER)
302         unless wordlist.empty?
303           output = key
304           # split using / / so that we can properly catch the marker
305           word1, word2 = output.split(/ /).map {|w| w.intern}
306           break
307         end
308       end
309     end
310
311     word3 = pick_word(wordlist)
312     return nil if word3 == MARKER
313
314     output << " #{word3}"
315     word1, word2 = word2, word3
316
317     (@bot.config['markov.max_words'] - 1).times do
318       word3 = pick_word(word1, word2)
319       break if word3 == MARKER
320       output << " #{word3}"
321       word1, word2 = word2, word3
322     end
323     return output
324   end
325
326   def help(plugin, topic="")
327     topic, subtopic = topic.split
328
329     case topic
330     when "delay"
331       "markov delay <value> => Set message delay"
332     when "ignore"
333       case subtopic
334       when "add"
335         "markov ignore add <hostmask|channel> => ignore a hostmask or a channel"
336       when "list"
337         "markov ignore list => show ignored hostmasks and channels"
338       when "remove"
339         "markov ignore remove <hostmask|channel> => unignore a hostmask or channel"
340       else
341         "ignore hostmasks or channels -- topics: add, remove, list"
342       end
343     when "status"
344       "markov status => show if markov is enabled, probability and amount of messages in queue for learning"
345     when "probability"
346       "markov probability [<percent>] => set the % chance of rbot responding to input, or display the current probability"
347     when "chat"
348       case subtopic
349       when "about"
350         "markov chat about <word> [<another word>] => talk about <word> or riff on a word pair (if possible)"
351       else
352         "markov chat => try to say something intelligent"
353       end
354     else
355       "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: 'chat' to attempt to say something relevant to the last line of chat, if it can -- help topics: ignore, readonly, delay, status, probability, chat, chat about"
356     end
357   end
358
359   def clean_str(s)
360     str = s.dup
361     str.gsub!(/^\S+[:,;]/, "")
362     str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
363     return str.strip
364   end
365
366   def probability?
367     return @bot.config['markov.probability']
368   end
369
370   def status(m,params)
371     if @bot.config['markov.enabled']
372       reply = _("markov is currently enabled, %{p}% chance of chipping in") % { :p => probability? }
373       l = @learning_queue.length
374       reply << (_(", %{l} messages in queue") % {:l => l}) if l > 0
375       l = @upgrade_queue.length
376       reply << (_(", %{l} chains to upgrade") % {:l => l}) if l > 0
377     else
378       reply = _("markov is currently disabled")
379     end
380     m.reply reply
381   end
382
383   def ignore?(m=nil)
384     return false unless m
385     return true if m.address? or m.private?
386     @bot.config['markov.ignore'].each do |mask|
387       return true if m.channel.downcase == mask.downcase
388       return true if m.source.matches?(mask)
389     end
390     return false
391   end
392
393   def ignore(m, params)
394     action = params[:action]
395     user = params[:option]
396     case action
397     when 'remove'
398       if @bot.config['markov.ignore'].include? user
399         s = @bot.config['markov.ignore']
400         s.delete user
401         @bot.config['ignore'] = s
402         m.reply _("%{u} removed") % { :u => user }
403       else
404         m.reply _("not found in list")
405       end
406     when 'add'
407       if user
408         if @bot.config['markov.ignore'].include?(user)
409           m.reply _("%{u} already in list") % { :u => user }
410         else
411           @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
412           m.reply _("%{u} added to markov ignore list") % { :u => user }
413         end
414       else
415         m.reply _("give the name of a person or channel to ignore")
416       end
417     when 'list'
418       m.reply _("I'm ignoring %{ignored}") % { :ignored => @bot.config['markov.ignore'].join(", ") }
419     else
420       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")
421     end
422   end
423
424   def enable(m, params)
425     @bot.config['markov.enabled'] = true
426     m.okay
427   end
428
429   def probability(m, params)
430     if params[:probability]
431       @bot.config['markov.probability'] = params[:probability].to_i
432       m.okay
433     else
434       m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
435     end
436   end
437
438   def disable(m, params)
439     @bot.config['markov.enabled'] = false
440     m.okay
441   end
442
443   def should_talk
444     return false unless @bot.config['markov.enabled']
445     prob = probability?
446     return true if prob > rand(100)
447     return false
448   end
449
450   def set_delay(m, params)
451     if params[:delay] == "off"
452       @bot.config["markov.delay"] = 0
453       m.okay
454     elsif !params[:delay]
455       m.reply _("Message delay is %{delay}" % { :delay => @bot.config["markov.delay"]})
456     else
457       @bot.config["markov.delay"] = params[:delay].to_i
458       m.okay
459     end
460   end
461
462   def reply_delay(m, line)
463     m.replied = true
464     if @bot.config['markov.delay'] > 0
465       @bot.timer.add_once(@bot.config['markov.delay']) {
466         m.reply line, :nick => false, :to => :public
467       }
468     else
469       m.reply line, :nick => false, :to => :public
470     end
471   end
472
473   def random_markov(m, message)
474     return unless should_talk
475
476     word1, word2 = clean_str(message).split(/\s+/)
477     return unless word1 and word2
478     line = generate_string(word1.intern, word2.intern)
479     return unless line
480     # we do nothing if the line we return is just an initial substring
481     # of the line we received
482     return if message.index(line) == 0
483     reply_delay m, line
484   end
485
486   def chat(m, params)
487     line = generate_string(params[:seed1], params[:seed2])
488     if line and line != [params[:seed1], params[:seed2]].compact.join(" ")
489       m.reply line
490     else
491       m.reply _("I can't :(")
492     end
493   end
494
495   def rand_chat(m, params)
496     # pick a random pair from the db and go from there
497     word1, word2 = MARKER, MARKER
498     output = Array.new
499     @bot.config['markov.max_words'].times do
500       word3 = pick_word(word1, word2)
501       break if word3 == MARKER
502       output << word3
503       word1, word2 = word2, word3
504     end
505     if output.length > 1
506       m.reply output.join(" ")
507     else
508       m.reply _("I can't :(")
509     end
510   end
511
512   def learn(*lines)
513     lines.each { |l| @learning_queue.push l }
514   end
515
516   def unreplied(m)
517     return if ignore? m
518
519     # in channel message, the kind we are interested in
520     message = m.plainmessage
521
522     if m.action?
523       message = "#{m.sourcenick} #{message}"
524     end
525
526     learn message
527     random_markov(m, message) unless m.replied?
528   end
529
530   def learn_triplet(word1, word2, word3)
531       k = "#{word1} #{word2}"
532       @chains_mutex.synchronize do
533         total = 0
534         hash = Hash.new(0)
535         if @chains.key? k
536           t2, h2 = @chains[k]
537           total += t2
538           hash.update h2
539         end
540         hash[word3] += 1
541         total += 1
542         @chains[k] = [total, hash]
543       end
544   end
545
546   def learn_line(message)
547     # debug "learning #{message.inspect}"
548     wordlist = clean_str(message).split(/\s+/).map { |w| w.intern }
549     return unless wordlist.length >= 2
550     word1, word2 = MARKER, MARKER
551     wordlist << MARKER
552     wordlist.each do |word3|
553       learn_triplet(word1, word2, word3)
554       word1, word2 = word2, word3
555     end
556   end
557
558   # TODO allow learning from URLs
559   def learn_from(m, params)
560     begin
561       path = params[:file]
562       file = File.open(path, "r")
563       pattern = params[:pattern].empty? ? nil : Regexp.new(params[:pattern].to_s)
564     rescue Errno::ENOENT
565       m.reply _("no such file")
566       return
567     end
568
569     if file.eof?
570       m.reply _("the file is empty!")
571       return
572     end
573
574     if params[:testing]
575       lines = []
576       range = case params[:lines]
577       when /^\d+\.\.\d+$/
578         Range.new(*params[:lines].split("..").map { |e| e.to_i })
579       when /^\d+$/
580         Range.new(1, params[:lines].to_i)
581       else
582         Range.new(1, [@bot.config['send.max_lines'], 3].max)
583       end
584
585       file.each do |line|
586         next unless file.lineno >= range.begin
587         lines << line.chomp
588         break if file.lineno == range.end
589       end
590
591       lines = lines.map do |l|
592         pattern ? l.scan(pattern).to_s : l
593       end.reject { |e| e.empty? }
594
595       if pattern
596         unless lines.empty?
597           m.reply _("example matches for that pattern at lines %{range} include: %{lines}") % {
598             :lines => lines.map { |e| Underline+e+Underline }.join(", "),
599             :range => range.to_s
600           }
601         else
602           m.reply _("the pattern doesn't match anything at lines %{range}") % {
603             :range => range.to_s
604           }
605         end
606       else
607         m.reply _("learning from the file without a pattern would learn, for example: ")
608         lines.each { |l| m.reply l }
609       end
610
611       return
612     end
613
614     if pattern
615       file.each { |l| learn(l.scan(pattern).to_s) }
616     else
617       file.each { |l| learn(l.chomp) }
618     end
619
620     m.okay
621   end
622 end
623
624 plugin = MarkovPlugin.new
625 plugin.map 'markov delay :delay', :action => "set_delay"
626 plugin.map 'markov delay', :action => "set_delay"
627 plugin.map 'markov ignore :action :option', :action => "ignore"
628 plugin.map 'markov ignore :action', :action => "ignore"
629 plugin.map 'markov ignore', :action => "ignore"
630 plugin.map 'markov enable', :action => "enable"
631 plugin.map 'markov disable', :action => "disable"
632 plugin.map 'markov status', :action => "status"
633 plugin.map 'chat about :seed1 [:seed2]', :action => "chat"
634 plugin.map 'chat', :action => "rand_chat"
635 plugin.map 'markov probability [:probability]', :action => "probability",
636            :requirements => {:probability => /^\d+%?$/}
637 plugin.map 'markov learn from :file [:testing [:lines lines]] [using pattern *pattern]', :action => "learn_from", :thread => true,
638            :requirements => {
639              :testing => /^testing$/,
640              :lines   => /^(?:\d+\.\.\d+|\d+)$/ }
641
642 plugin.default_auth('ignore', false)
643 plugin.default_auth('probability', false)
644 plugin.default_auth('learn', false)
645