]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/markov.rb
spell plugin: it's spell.path, not spell.program
[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_users',
21     :default => [],
22     :desc => "Hostmasks of users to be ignored")
23
24   def initialize
25     super
26     @registry.set_default([])
27     if @registry.has_key?('enabled')
28       @bot.config['markov.enabled'] = @registry['enabled']
29       @registry.delete('enabled')
30     end
31     if @registry.has_key?('probability')
32       @bot.config['markov.probability'] = @registry['probability']
33       @registry.delete('probability')
34     end
35     @learning_queue = Queue.new
36     @learning_thread = Thread.new do
37       while s = @learning_queue.pop
38         learn s
39         sleep 0.5
40       end
41     end
42     @learning_thread.priority = -1
43   end
44
45   def cleanup
46     debug 'closing learning thread'
47     @learning_queue.push nil
48     @learning_thread.join
49     debug 'learning thread closed'
50   end
51
52   def generate_string(word1, word2)
53     # limit to max of 50 words
54     output = word1 + " " + word2
55
56     # try to avoid :nonword in the first iteration
57     wordlist = @registry["#{word1} #{word2}"]
58     wordlist.delete(:nonword)
59     if not wordlist.empty?
60       word3 = wordlist[rand(wordlist.length)]
61       output = output + " " + word3
62       word1, word2 = word2, word3
63     end
64
65     49.times do
66       wordlist = @registry["#{word1} #{word2}"]
67       break if wordlist.empty?
68       word3 = wordlist[rand(wordlist.length)]
69       break if word3 == :nonword
70       output = output + " " + word3
71       word1, word2 = word2, word3
72     end
73     return output
74   end
75
76   def help(plugin, topic="")
77     "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 [<chance>]' => set the % chance of rbot responding to input, or display the current probability, 'chat' => try and say something intelligent, 'chat about <foo> <bar>' => riff on a word pair (if possible)"
78   end
79
80   def clean_str(s)
81     str = s.dup
82     str.gsub!(/^\S+[:,;]/, "")
83     str.gsub!(/\s{2,}/, ' ') # fix for two or more spaces
84     return str.strip
85   end
86
87   def probability?
88     return @bot.config['markov.probability']
89   end
90
91   def status(m,params)
92     if @bot.config['markov.enabled']
93       m.reply "markov is currently enabled, #{probability?}% chance of chipping in"
94     else
95       m.reply "markov is currently disabled"
96     end
97   end
98
99   def ignore?(user=nil)
100     return false unless user
101     @bot.config['markov.ignore_users'].each do |mask|
102       return true if user.matches?(mask)
103     end
104     return false
105   end
106
107   def ignore(m, params)
108     action = params[:action]
109     user = params[:option]
110     case action
111     when 'remove':
112       if @bot.config['markov.ignore_users'].include? user
113         s = @bot.config['markov.ignore_users']
114         s.delete user
115         @bot.config['ignore_users'] = s
116         m.reply "#{user} removed"
117       else
118         m.reply "not found in list"
119       end
120     when 'add':
121       if user
122         if @bot.config['markov.ignore_users'].include?(user)
123           m.reply "#{user} already in list"
124         else
125           @bot.config['markov.ignore_users'] = @bot.config['markov.ignore_users'].push user
126           m.reply "#{user} added to markov ignore list"
127         end
128       else
129         m.reply "give the name of a person to ignore"
130       end
131     when 'list':
132       m.reply "I'm ignoring #{@bot.config['markov.ignore_users'].join(", ")}"
133     else
134       m.reply "have markov ignore the input from a hostmask.  usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
135     end
136   end
137
138   def enable(m, params)
139     @bot.config['markov.enabled'] = true
140     m.okay
141   end
142
143   def probability(m, params)
144     if params[:probability]
145       @bot.config['markov.probability'] = params[:probability].to_i
146       m.okay
147     else
148       m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
149     end
150   end
151
152   def disable(m, params)
153     @bot.config['markov.enabled'] = false
154     m.okay
155   end
156
157   def should_talk
158     return false unless @bot.config['markov.enabled']
159     prob = probability?
160     return true if prob > rand(100)
161     return false
162   end
163
164   def delay
165     1 + rand(5)
166   end
167
168   def random_markov(m, message)
169     return unless should_talk
170
171     word1, word2 = message.split(/\s+/)
172     return unless word1 and word2
173     line = generate_string(word1, word2)
174     return unless line
175     return if line == message
176     @bot.timer.add_once(delay) {
177       m.reply line
178     }
179   end
180
181   def chat(m, params)
182     line = generate_string(params[:seed1], params[:seed2])
183     if line != "#{params[:seed1]} #{params[:seed2]}"
184       m.reply line 
185     else
186       m.reply "I can't :("
187     end
188   end
189
190   def rand_chat(m, params)
191     # pick a random pair from the db and go from there
192     word1, word2 = :nonword, :nonword
193     output = Array.new
194     50.times do
195       wordlist = @registry["#{word1} #{word2}"]
196       break if wordlist.empty?
197       word3 = wordlist[rand(wordlist.length)]
198       break if word3 == :nonword
199       output << word3
200       word1, word2 = word2, word3
201     end
202     if output.length > 1
203       m.reply output.join(" ")
204     else
205       m.reply "I can't :("
206     end
207   end
208   
209   def message(m)
210     return unless m.public?
211     return if m.address?
212     return if ignore? m.source
213
214     # in channel message, the kind we are interested in
215     message = clean_str m.plainmessage
216
217     if m.action?
218       message = "#{m.sourcenick} #{message}"
219     end
220     
221     @learning_queue.push message
222     random_markov(m, message) unless m.replied?
223   end
224
225   def learn(message)
226     # debug "learning #{message}"
227     wordlist = message.split(/\s+/)
228     return unless wordlist.length >= 2
229     word1, word2 = :nonword, :nonword
230     wordlist.each do |word3|
231       k = "#{word1} #{word2}"
232       @registry[k] = @registry[k].push(word3)
233       word1, word2 = word2, word3
234     end
235     k = "#{word1} #{word2}"
236     @registry[k] = @registry[k].push(:nonword)
237   end
238 end
239
240 plugin = MarkovPlugin.new
241 plugin.map 'markov ignore :action :option', :action => "ignore"
242 plugin.map 'markov ignore :action', :action => "ignore"
243 plugin.map 'markov ignore', :action => "ignore"
244 plugin.map 'markov enable', :action => "enable"
245 plugin.map 'markov disable', :action => "disable"
246 plugin.map 'markov status', :action => "status"
247 plugin.map 'chat about :seed1 :seed2', :action => "chat"
248 plugin.map 'chat', :action => "rand_chat"
249 plugin.map 'markov probability [:probability]', :action => "probability",
250            :requirements => {:probability => /^\d+%?$/}