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