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