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