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