]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/markov.rb
fix help text, ticket #36
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / markov.rb
1 class MarkovPlugin < Plugin
2   def initialize
3     super
4     @registry.set_default([])
5     @lastline = false
6     @enabled = false
7   end
8
9   def get_line
10     # limit to max of 50 words
11     return unless @lastline
12     word1, word2 = @lastline.split(/\s+/)
13     output = word1 + " " + word2
14     50.times do
15       wordlist = @registry["#{word1}/#{word2}"]
16       word3 = wordlist[rand(wordlist.length)]
17       break if word3 == :nonword
18       output = output + " " + word3
19       word1, word2 = word2, word3
20     end
21     return output
22   end
23
24   def markov(m, params)
25     m.reply get_line
26   end
27   
28   def help(plugin, topic="")
29     "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."
30   end
31   
32   def clean_str(s)
33     str = s.dup
34     str.gsub!(/^.+:/, "")
35     str.gsub!(/^.+,/, "")
36     return str.strip
37   end
38
39   def enable(m, params)
40     @enabled = true
41     m.okay
42   end
43
44   def disable(m, params)
45     @enabled = false
46     m.okay
47   end
48
49   def should_talk
50     return false unless @enabled
51     # 50:50
52     return false if rand(2) == 1
53     return true
54   end
55
56   def random_markov(m)
57     return unless should_talk
58     line = get_line
59     puts "got line #{line}"
60     m.reply line unless line == @lastline
61   end
62
63   def listen(m)
64     return unless m.kind_of?(PrivMessage) && m.public?
65     return if m.address?
66     message = clean_str m.message
67     # in channel message, the kind we are interested in
68     wordlist = message.split(/\s+/)
69     return unless wordlist.length > 2
70     @lastline = message
71     word1, word2 = :nonword, :nonword
72     wordlist.each do |word3|
73       @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
74       word1, word2 = word2, word3
75     end
76     @registry["#{word1}/#{word2}"] = [:nonword]
77     random_markov(m)
78   end
79 end
80 plugin = MarkovPlugin.new
81 plugin.map 'markov enable', :action => "enable"
82 plugin.map 'markov disable', :action => "disable"
83 plugin.map 'markov'