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