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