apply patch and close #37 and #33
[rbot] / 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     return @registry['ignore_users'].include?(user)
54   end
55
56   def ignore(m, params)
57     if @registry['ignore_users'].nil?
58       @registry['ignore_users'] = []
59     end
60     action = params[:action]
61     user = params[:option]
62     case action
63     when 'remove':
64       if @registry['ignore_users'].include? user
65         s = @registry['ignore_users']
66         s.delete user
67         @registry['ignore_users'] = s
68         m.reply "#{user} removed"
69       else
70         m.reply "not found in list"
71       end
72     when 'add':
73       if user
74         if @registry['ignore_users'].include?(user)
75           m.reply "#{user} already in list"
76         else
77           @registry['ignore_users'] = @registry['ignore_users'].push user 
78           m.reply "#{user} added to markov ignore list"
79         end
80       else
81         m.reply "give the name of a person to ignore"
82       end
83     when 'list':
84       m.reply "I'm ignoring #{@registry['ignore_users'].join(", ")}"
85     else
86       m.reply "have markov ignore the input from a hostmask.  usage: markov ignore add <mask>; markov ignore remove <mask>; markov ignore list"
87     end
88   end
89
90   def enable(m, params)
91     @registry['enabled'] = true
92     m.okay
93   end
94
95   def probability(m, params)
96     @registry['probability'] = params[:probability].to_i
97     m.okay
98   end
99
100   def disable(m, params)
101     @registry['enabled'] = false
102     m.okay
103   end
104
105   def should_talk
106     return false unless @registry['enabled']
107     prob = probability?
108     return true if prob > rand(100)
109     return false
110   end
111
112   def random_markov(m, message)
113     return unless should_talk
114     line = generate_string(message)
115     return unless line
116     m.reply line unless line == message
117   end
118
119   def chat(m, params)
120     seed = "#{params[:seed1]} #{params[:seed2]}"
121     line = generate_string seed
122     if line != seed
123       m.reply line 
124     else
125       m.reply "I can't :("
126     end
127   end
128
129   def rand_chat(m, params)
130     # pick a random pair from the db and go from there
131     word1, word2 = :nonword, :nonword
132     output = Array.new
133     50.times do
134       wordlist = @registry["#{word1}/#{word2}"]
135       break if wordlist.empty?
136       word3 = wordlist[rand(wordlist.length)]
137       break if word3 == :nonword
138       output << word3
139       word1, word2 = word2, word3
140     end
141     if output.length > 1
142       m.reply output.join(" ")
143     else
144       m.reply "I can't :("
145     end
146   end
147   
148   def listen(m)
149     return unless m.kind_of?(PrivMessage) && m.public?
150     return if m.address?
151     return if ignore? m.source
152
153     # in channel message, the kind we are interested in
154     message = clean_str m.message
155     
156     # we respond first. otherwise if we add this line to the db first, and
157     # it's fairly unique, there's a good chance we'll just parrot it back
158     # here.
159     random_markov(m, message)
160     
161     wordlist = message.split(/\s+/)
162     return unless wordlist.length > 2
163     @lastline = message
164     word1, word2 = :nonword, :nonword
165     wordlist.each do |word3|
166       @registry["#{word1}/#{word2}"] = @registry["#{word1}/#{word2}"].push(word3)
167       word1, word2 = word2, word3
168     end
169     @registry["#{word1}/#{word2}"] = [:nonword]
170   end
171 end
172 plugin = MarkovPlugin.new
173 plugin.map 'markov ignore :action :option', :action => "ignore"
174 plugin.map 'markov ignore :action', :action => "ignore"
175 plugin.map 'markov ignore', :action => "ignore"
176 plugin.map 'markov enable', :action => "enable"
177 plugin.map 'markov disable', :action => "disable"
178 plugin.map 'markov status', :action => "status"
179 plugin.map 'chat about :seed1 :seed2', :action => "chat"
180 plugin.map 'chat', :action => "rand_chat"
181 plugin.map 'markov probability :probability', :action => "probability",
182            :requirements => {:probability => /^\d+$/}