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