Amethyst/Brain/Infobot/Module/Statement.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 257 CC 27 SCAR 41.1
CC
27
Cov
0%
forget
132.0
process
72.0
statement
42.0
CRAP 61.2
1 package Amethyst::Brain::Infobot::Module::Statement;
2  
3 use strict;
4 use vars qw(@ISA
5 $RE_VERB $RE_QUESTION $RE_IGNORE $RE_PREAMBLE
6 $RE_RMKPREFIX $RE_RMKSUFFIX
7 $RE_RMVPREFIX $RE_RMVSUFFIX
8 );
9 use Amethyst::Message;
10 use Amethyst::Store;
11 use Amethyst::Brain::Infobot;
12 use Amethyst::Brain::Infobot::Module;
13  
14 @ISA = qw(Amethyst::Brain::Infobot::Module);
15 $RE_VERB = "is|are|has";
16 $RE_QUESTION = "what|who|where|which|when|wot|wtf";
17 $RE_IGNORE = "if|this|that|there|so|some|someone" .
18 "|he|she|we|it|they|you|i" .
19 "|$RE_QUESTION";
20 $RE_PREAMBLE = "think|thinks|note|notes|said|say|says|that";
21 $RE_RMKPREFIX = "but|and|or|btw|actually|well"; # |a|the
22 $RE_RMKSUFFIX = "really|actually";
23 $RE_RMVSUFFIX = "too|also|as well";
24  
25 sub new {
26 my $class = shift;
27 my $self = $class->SUPER::new(
28 Name => 'Statement',
29 # Regex => qr/(?:karma|\+\+|--)/i,
30 Usage => 'Make a statement. Ask a question.',
31 Description => "Statement handler",
32 @_
33 );
34  
35 die "No FactoidWrite store in Infobot config"
36 unless $self->{Infobot}->{FactoidWrite};
37  
38 $self->{WriteStore} = new Amethyst::Store(
39 Source => $self->{Infobot}->{FactoidWrite},
40 );
41  
42 my @stores = ($self->{WriteStore});
43 foreach (@{ $self->{Infobot}->{FactoidRead} }) {
44 my $store = new Amethyst::Store(
45 Source => $_,
46 );
47 push(@stores, $store);
48 }
49  
50 $self->{Stores} = \@stores;
51  
52 return bless $self, $class;
53 }
54  
55 sub forget {
56 my ($self, $message, $key) = @_;
57  
58 my $filter = qr/./;
59 my $msg = undef;
60 my $data = undef;
61  
62 print STDERR "Forget: $key\n";
63  
64 if ($key =~ m,^\s*(.*?)\s+/(.*)/\s*$,) {
65 $key = $1;
66 $filter = $2;
67 print STDERR "Forget: key $key, filter $filter\n";
68 eval { "x" =~ m/$filter/ };
69 if ($@) {
70 my $reply = $self->reply_to($message,
71 "Invalid regexp $filter: $@");
72 $reply->send;
73 return 1;
74 }
75 }
76  
77 my $skey = $self->normalise($key);
78  
79 my $store = $self->{WriteStore};
80  
81 if ($data = $store->get($skey)) {
82 foreach (keys %$data) {
83 if (/$filter/) {
84 # delete $data->{$_};
85 $msg = "I forgot $skey.";
86 }
87 }
88 if (%$data) {
89 # $store->set($skey, $data);
90 }
91 else {
92 # $store->unset($skey);
93 }
94 }
95  
96 foreach my $store (@{ $self->{Stores} }) {
97 if ($data = $store->get($skey)) {
98 foreach (keys %$data) {
99 if (/$filter/) {
100 $msg ="I have been instructed not to forget " .
101 "$skey. It is in readonly store " .
102 "$store->{Source}."
103 unless $msg;
104 }
105 }
106 }
107 }
108  
109 unless ($msg) {
110 if ($filter) {
111 $msg = "I knew nothing about $skey matching $filter.";
112 }
113 else {
114 $msg = "I didn't know about $skey.";
115 }
116 }
117  
118 my $reply = $self->reply_to($message, $msg);
119 $reply->send;
120  
121 return 1;
122 }
123  
124 sub question {
125 my ($self, $message, $key) = @_;
126  
127 print STDERR "Question: $key\n";
128  
129 my $skey = $self->normalise($key);
130  
131 my @out;
132  
133 foreach my $store (@{ $self->{Stores} }) {
134  
135 my $data = $store->get($skey);
136 foreach my $sval (keys %$data) {
137 my $href = $data->{$sval};
138 my $key = $href->{key} || $skey;
139 my $val = $href->{val} || $sval;
140  
141 my $msg;
142 if ($val =~ s/^\s*<reply>\s*//) {
143 $msg = $val;
144 }
145 else {
146 $msg = "$key $href->{verb} $val";
147 }
148 $msg =~ s/\$who\b/$message->user/gex;
149 push(@out, ucfirst $msg);
150 }
151 }
152  
153 if (@out) {
154 # XXX FIXME better.
155 @out = @out[0..9] if @out > 10;
156 my $reply = $self->reply_to($message, join(' or ', @out));
157 $reply->send;
158 return 1;
159 }
160  
161 return undef;
162 }
163  
164 sub statement {
165 my ($self, $message, $key, $verb, $val) = @_;
166  
167 print STDERR "Statement: $key\n";
168  
169 my $store = $self->{WriteStore};
170  
171 my $skey = $self->normalise($key);
172 my $sval = $self->normalise($val);
173  
174 my $data = $store->get($skey);
175 if ($data) {
176 if ($data->{$sval}) {
177 print STDERR "I already knew $key => $val\n";
178 return undef;
179 }
180 print STDERR "I also knew $key as something else\n";
181 if ($sval !~ s/^\s*also\s+//) {
182 my @ovals = keys %$data;
183 my $oval = $ovals[int(rand(@ovals))];
184 my $href = $data->{$oval};
185  
186 my $okey = $href->{key} || $skey;
187 $oval = $href->{val} || $oval;
188  
189 my $msg = "$okey $href->{verb} $oval";
190 my $reply = $self->reply_to($message, "But " . $msg);
191 $reply->send;
192 return 1;
193 }
194 $val =~ s/^\s*also\s+//i;
195 }
196  
197 $data->{$sval} = {
198 verb => $verb,
199 user => $message->user,
200 time => time(),
201 };
202 $data->{$sval}->{key} = $key if $key ne $skey;
203 $data->{$sval}->{val} = $val if $val ne $sval;
204  
205 $store->set($skey, $data);
206  
207 return undef;
208 }
209  
210 sub normalise {
211 my ($self, $text) = @_;
212  
213 $text = lc $text;
214 $text =~ s/\s+/ /g;
215 $text =~ s/^\s*//;
216 $text =~ s/[[:punct:]\s]*$//;
217  
218 return $text;
219 }
220  
221 sub tidy_key {
222 my ($self, $key) = @_;
223  
224 print STDERR "K = $key\n";
225  
226 $key =~ s/\s+/ /g;
227  
228 $key =~ s/^.* that //i; # $N verbs that X is Y
229 $key =~ s/^.* notes? //i; # $N notes X is Y
230 1 while $key =~ s/^\s+(?:$RE_RMKPREFIX)\s+//i;
231 1 while $key =~ s/\s+(?:$RE_RMKSUFFIX)\s+$//i;
232  
233 $key =~ s/^.*,//;
234 $key =~ s/^\s*//;
235 $key =~ s/[[:punct:]\s]*$//;
236  
237 print STDERR "M = $key\n";
238  
239 return $key;
240 }
241  
242 sub process {
243 my ($self, $message) = @_;
244  
245 my $content = $message->content;
246 my $who = $message->user;
247  
248 $content =~ s/\s+/ /g;
249  
250 $content =~ s/\bi am /$1$who is /i;
251 $content =~ s/\bmy /$1$who\'s /ig;
252 $content =~ s/\bour /$1$who\'s /ig;
253 # $content =~ s/\byour /$1$name\'s /ig;
254  
255 $content =~ s/\bisn't /is not /ig;
256 $content =~ s/\baren't /are not /ig;
257  
258 # if ($content =~ /^forget\s+(?:(?:a|an|the)\s+)?(.*)/i) {
259 if ($content =~ /^forget\s+(.*)/i) {
260 return $self->forget($message, $1);
261 }
262  
263 # Modification ignored for now
264 if ($content =~ /^($RE_QUESTION)\s+($RE_VERB)\s+(.*)/io){
265 return $self->question($message, $1);
266 }
267 elsif ($content =~ /^(.*?)\s*\?\s*$/) {
268 return $self->question($message, $1);
269 }
270  
271 foreach (split(/(?:[?!:;]|\.\s)/, $content)) {
272 s/^\s*\b(?:$RE_RMKPREFIX)\b\s+//i;
273 if (/^\s*(.*?)\s+($RE_VERB)\s+(.*)/io) {
274 print STDERR "Considering: $_\n";
275 my ($key, $verb, $val) = ($1, lc $2, $3);
276  
277 $key = $self->tidy_key($key);
278  
279 next if length $key > 20;
280 next unless $key =~ /\S/;
281 next if $key =~ /\b(?:$RE_IGNORE)\b/io;
282  
283 $self->statement($message, $key, $verb, $val);
284 }
285 }
286  
287 # forget X
288 # X is Y
289 # I/you/we am/is/are Y
290 # who/what/which/where am/is/are Y
291  
292 return undef;
293 }
294  
295 1;