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