]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/markov.rb
Bug in topic plugin introduced in [440]
[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     @registry['enabled'] = false unless @registry.has_key?('enabled')
6     @lastline = false
7   end
8
9   def generate_string(word1, word2)
10     # limit to max of 50 words
11     output = word1 + " " + word2
12
13     # try to avoid :nonword in the first iteration
14     wordlist = @registry["#{word1} #{word2}"]
15     wordlist.delete(:nonword)
16     if not wordlist.empty?
17       word3 = wordlist[rand(wordlist.length)]
18       output = output + " " + word3
19       word1, word2 = word2, word3
20     end
21
22     49.times do
23       wordlist = @registry["#{word1} #{word2}"]
24       break if wordlist.empty?
25       word3 = wordlist[rand(wordlist.length)]
26       break if word3 == :nonword
27       output = output + " " + word3
28       word1, word2 = word2, word3
29     end
30     return output
31   end
32
33   def help(plugin, topic="")
34     "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.  other options to markov: 'ignore' => ignore a hostmask (accept no input), 'status' => show current status, 'probability' => set the % chance of rbot responding to input, 'chat' => try and say something intelligent, 'chat about <foo> <bar>' => riff on a word pair (if possible)"
35   end
36
37   def clean_str(s)
38     str = s.dup
39     str.gsub!(/^\S+[:,;]/, "")
40     str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
41     return str.strip
42   end
43
44   def probability?
45     prob = @registry['probability']
46     prob = 25 if prob.kind_of? Array;
47     prob = 0 if prob < 0
48     prob = 100 if prob > 100
49     return prob
50   end
51
52   def status(m,params)
53     enabled = @registry['enabled']
54     if (enabled)
55       m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
56     else
57       m.reply "markov is currently disabled"
58     end
59   end
60
61   def ignore?(user=nil)
62     @registry['ignore_users'].each do |mask|
63       return true if Irc.netmaskmatch mask, user
64     end
65     return false
66   end
67
68   def ignore(m, params)
69     if @registry['ignore_users'].nil?
70       @registry['ignore_users'] = []
71     end
72     action = params[:action]
73     user = params[:option]
74     case action
75     when 'remove':
76       if @registry['ignore_users'].include? user
77         s = @registry['ignore_users']
78         s.delete user
79         @registry['ignore_users'] = s
80         m.reply "#{user} removed"
81       else
82         m.reply "not found in list"
83       end
84     when 'add':
85       if user
86         if @registry['ignore_users'].include?(user)
87           m.reply "#{user} already in list"
88         else
89           @registry['ignore_users'] = @registry['ignore_users'].push user 
90           m.reply "#{user} added to markov ignore list"
91         end
92       else
93         m.reply "give the name of a person to ignore"
94       end
95     when 'list':
96       m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
97     else
98       m.reply "have markov ignore the input from a hostmask.  usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
99     end
100   end
101
102   def enable(m, params)
103     @registry['enabled'] = true
104     m.okay
105   end
106
107   def probability(m, params)
108     @registry['probability'] = params[:probability].to_i
109     m.okay
110   end
111
112   def disable(m, params)
113     @registry['enabled'] = false
114     m.okay
115   end
116
117   def should_talk
118     return false unless @registry['enabled']
119     prob = probability?
120     return true if prob > rand(100)
121     return false
122   end
123
124   def delay
125     1 + rand(5)
126   end
127
128   def random_markov(m, message)
129     return unless should_talk
130
131     word1, word2 = message.split(/\s+/)
132     line = generate_string(word1, word2)
133     return unless line
134     return if line == message
135     @bot.timer.add_once(delay, m) {|m|
136       m.reply line
137     }
138   end
139
140   def chat(m, params)
141     line = generate_string(params[:seed1], params[:seed2])
142     if line != "#{params[:seed1]} #{params[:seed2]}"
143       m.reply line 
144     else
145       m.reply "I can't :("
146     end
147   end
148
149   def rand_chat(m, params)
150     # pick a random pair from the db and go from there
151     word1, word2 = :nonword, :nonword
152     output = Array.new
153     50.times do
154       wordlist = @registry["#{word1} #{word2}"]
155       break if wordlist.empty?
156       word3 = wordlist[rand(wordlist.length)]
157       break if word3 == :nonword
158       output << word3
159       word1, word2 = word2, word3
160     end
161     if output.length > 1
162       m.reply output.join(" ")
163     else
164       m.reply "I can't :("
165     end
166   end
167   
168   def listen(m)
169     return unless m.kind_of?(PrivMessage) && m.public?
170     return if m.address?
171     return if ignore? m.source
172
173     # in channel message, the kind we are interested in
174     message = clean_str m.message
175
176     if m.action?
177       message = "#{m.sourcenick} #{message}"
178     end
179     
180     wordlist = message.split(/\s+/)
181     return unless wordlist.length >= 2
182     @lastline = message
183     word1, word2 = :nonword, :nonword
184     wordlist.each do |word3|
185       @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(word3)
186       word1, word2 = word2, word3
187     end
188     @registry["#{word1} #{word2}"] = @registry["#{word1} #{word2}"].push(:nonword)
189
190     return if m.replied?
191     random_markov(m, message)
192   end
193 end
194 plugin = MarkovPlugin.new
195 plugin.map 'markov ignore :action :option', :action => "ignore"
196 plugin.map 'markov ignore :action', :action => "ignore"
197 plugin.map 'markov ignore', :action => "ignore"
198 plugin.map 'markov enable', :action => "enable"
199 plugin.map 'markov disable', :action => "disable"
200 plugin.map 'markov status', :action => "status"
201 plugin.map 'chat about :seed1 :seed2', :action => "chat"
202 plugin.map 'chat', :action => "rand_chat"
203 plugin.map 'markov probability :probability', :action => "probability",
204            :requirements => {:probability => /^\d+%?$/}