Chatbot/Brain.pm

filter: Statementstmt 87.3% 90 / 103 Branchbran 67.8% 19 / 28 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 81.8% 126 / 154 SCAR 33.5
CC
25
Cov
82%
transform
20.9
postprocess
5.3
_test_quit
3.0
CRAP 28.6
« Chatbot.pm ↑ Summary Chatbot/ScriptParser.pm »
1 package Eliza::Chatbot::Brain;
2  
3 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use MooX::LazierAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Ref::Util qw(is_scalarref is_blessed_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 attributes(
9 decomp_matches => [rw, [ ], {lzy}],
10 [qw/options last/] => [rw, nan, {lzy}],
11 );
12  
13 sub preprocess {
14 13 ▶ my ($self, $string) = @_;
Subroutine preprocess: called CC=2 SCAR=6.9
Pod documented
15 13 my @orig_words = split / /, $string;
16  
17 13 my @converted_words;
18 13 foreach my $word ( @orig_words ) {
19 32 $word =~ s{[?!,]|but}{.}g;
20 32 push @converted_words, $word;
21 }
22  
23 13 my $formated = join ' ', @converted_words;
24 13 @converted_words = split /\./, $formated;
25 13 return @converted_words;
26 }
27  
28 sub postprocess {
29 19 ▶ my ($self, $string) = @_;
Subroutine postprocess: called CC=4 SCAR=16.6
Pod documented
30 19 ▶ if ( is_blessed_arrayref($string) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_blessed_arrayref($string)) { } 0 19 19
elsif (is_scalarref(\$string)) { } 13 6 19
31 0 ▶ for (my $i = 1; $i < scalar @{$string}; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$string;}) 0 0 0
32 0 $string->[$i] =~ s/([,;?!]|\.*)$//;
33 }
34 } elsif ( is_scalarref(\$string) ) {
35 13 $string =~ tr/ / /s; # Eliminate any duplicate space characters.
36 13 $string =~ s/[ ][?]$/?/; # Eliminate any spaces before the question mark.
37 }
38 19 return $string;
39 }
40  
41 sub _test_quit {
42 19 ▶ my ($self, $string) = @_;
Subroutine _test_quit: called CC=3 SCAR=11.0
43 19 foreach my $quitword (@{$self->options->data->quit}) {
44 29 ▶ return 1 if $string =~ m{$quitword}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /$quitword/msx 11 18 29
45 }
46 }
47  
48 sub _debug_memory {
49 6 ▶ my $self = shift;
Subroutine _debug_memory: called CC=2 SCAR=6.9
50 6 my @memory = @{$self->options->memory};
51 6 my $string = sprintf("%s item(s) in memory stack:\n", scalar @memory);
52 6 foreach my $msg (@memory) {
53 21 $string .= sprintf("\t\t->%s\n", $msg);
54 }
55 6 return $string;
56 }
57  
58 sub transform {
59 13 ▶ my ($self, $string, $use_memory) = @_;
Subroutine transform: called CC=18 SCAR=30.4
Pod documented
60  
61 13 my ($this_decomp, $reasmbkey);
62 13 my $options = $self->options;
63 13 ▶ $options->debug_text(sprintf("\t[Pulling string \"%s\" from memory.]\n", $string))
Branch: 1 branch
BranchTrueFalseTotal
if $use_memory 0 13 13
64 if $use_memory;
65  
66 13 ▶ if ($self->_test_quit($string)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_test_quit($string)) 5 8 13
67 5 $self->last(1);
68 5 return $options->data->final->[ $options->myrand(scalar @{$options->data->final}) ];
69 }
70  
71 # Default to a really low rank.
72 8 my $rank = -2;
73 8 my $reasmb = "";
74 8 my $goto = "";
75  
76 # First run the string through preprocess.
77 8 my @string_parts = $self->preprocess( $string );
78  
79 8 $self->decomp_matches([]);
80 # Examine each part of the input string in turn.
81 8 foreach my $string_part (@string_parts) {
82  
83 # Run through the whole list of keywords.
84 8 KEYWORD: foreach my $keyword (keys %{$options->data->decomp}) {
85  
86 # Check to see if the input string contains a keyword
87 # which outranks any we have found previously
88 # (On first loop, rank is set to -2.)
89 24 ▶ if ( ($string_part =~ /\b$keyword\b/i or $keyword eq $goto)
Branch: 1 branch
BranchTrueFalseTotal
if ($string_part =~ /\b$keyword\b/i || $keyword eq $goto and $rank < $options->data->key->{$keyword}) 6 18 24
Condition66%4/6
$string_part =~ /\b$keyword\b/i || $keyword eq $goto and $rank < $options->data->key->{$keyword}
!ll&&!rl&&r
1806
$string_part =~ /\b$keyword\b/i or $keyword eq $goto
l!l&&r!l&&!r
6018
MC/DC33%1/3
$string_part =~ /\b$keyword\b/i || $keyword eq $goto and $rank < $options->data->key->{$keyword}
$string_part =~ /\b$keyword\b/i $keyword eq $goto $rank < $options->data->key->{$keyword}
Truth table40%2/5
A or B and C
A= $string_part =~ /\b$keyword\b/iB= $keyword eq $gotoC= $rank < $options->data->key->{$keyword}
ABCresult
00X0
1X00
0100
1X11
0111
90 and
91 $rank < $options->data->key->{$keyword}
92 )
93 {
94 # If we find one, then set $rank to equal
95 # the rank of that keyword.
96 6 $rank = $options->data->key->{$keyword};
97 6 ▶ $options->debug_text(
Condition50%1/2
$options->debug_text // ""
l!l
60
MC/DC0%0/1
$options->debug_text // ""
$options->debug_text
Truth table50%1/2
A
A= $options->debug_text
Aresult
11
00
98 sprintf("%s \trank:%d keyword:%s",
99 ($options->debug_text // ''), $rank, $keyword)
100 );
101  
102 # Now let's check all the decomposition rules for that keyword.
103 6 foreach my $decomp (@{$options->data->decomp->{$keyword}}) {
104  
105 # Change '*' to '\b(.*)\b' in this decomposition rule,
106 # so we can use it for regular expressions. Later,
107 # we will want to isolate individual matches to each wildcard.
108 6 $this_decomp = $decomp;
109 6 $this_decomp =~ s/\s*\*\s*/\\b\(\.\*\)\\b/g;
110 # If this docomposition rule contains a word which begins with '@',
111 # then the script also contained some synonyms for that word.
112 # Find them all using %synon and generate a regular expression
113 # containing all of them.
114 6 ▶ if ($this_decomp =~ /\@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_decomp =~ /\@/) 0 6 6
115 0 $this_decomp =~ s/.*\@(\w*).*/$1/i;
116 0 my $synonyms = join ('|', @{$options->data->synon->{$this_decomp}} );
117 0 $this_decomp =~ s/(.*)\@$this_decomp(.*)/$1($this_decomp\|$synonyms)$2/g;
118 }
119  
120 $options->debug_text(
121 6 sprintf("%s\n\t\t: %s", $options->debug_text, $decomp)
122 );
123
124 # Using the regular expression we just generated,
125 # match against the input string. Use empty "()"'s to
126 # eliminate warnings about uninitialized variables.
127 6 ▶ if ($string_part =~ /$this_decomp()()()()()()()()()()()/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_part =~ /$this_decomp()()()()()()()()()()()/i) 6 0 6
128  
129 # If this decomp rule matched the string,
130 # then create an array, so that we can refer to matches
131 # to individual wildcards. Use '0' as a placeholder
132 # (we don't want to refer to any "zeroth" wildcard).
133 6 my @decomp_matches = ("0", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
134
135  
136 6 push @{$self->decomp_matches}, { matches => \@decomp_matches };
137
138 6 $options->debug_text(
139 sprintf( "%s : %s \n",
140 $options->debug_text, join( ' ', @decomp_matches))
141 );
142
143 # Using the keyword and the decomposition rule,
144 # reconstruct a key for the list of reassamble rules.
145 6 $reasmbkey = join $;, $keyword, $decomp;
146 # Get the list of possible reassembly rules for this key.
147 6 my @these_reasmbs = @{$options->data->reasmb->{$reasmbkey}};
148  
149 # Pick out a reassembly rule at random :).
150 6 $reasmb = $these_reasmbs[ $options->myrand( scalar @these_reasmbs ) ];
151 6 $options->debug_text(
152 sprintf("%s \t\t--> %s\n",
153 $options->debug_text, $reasmb )
154 );
155  
156 # If the reassembly rule we picked contains the word "goto",
157 # then we start over with a new keyword. Set $keyword to equal
158 # that word, and start the whole loop over.
159 6 ▶ if ($reasmb =~ m/^goto\s(\w*).*/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reasmb =~ /^goto\s(\w*).*/i) 0 6 6
160 0 $options->debug_text(sprintf("%s \$1 = $1\n",
161 $options->debug_text));
162 0 $goto = $keyword = $1;
163 0 $rank = -2;
164 0 redo KEYWORD;
165 }
166  
167 # Otherwise, using the matches to wildcards which we stored above,
168 # insert words from the input string back into the reassembly rule.
169 # [THANKS to Gidon Wise for submitting a bugfix here]
170 6 my $decomp_matches = $self->decomp_matches;
171 6 foreach my $match (@{$decomp_matches}) {
172 6 $match->{matches} = $self->postprocess( $match->{matches} );
173 6 ▶ for (my $i = 1; $i < 10; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 54 6 60
174 54 $reasmb =~ s/\($i\)/$match->{matches}->[$i]/g;
175 }
176 }
177  
178 # Move on to the next keyword. If no other keywords match,
179 # then we'll end up actually using the $reasmb string
180 # we just generated above.
181 6 next KEYWORD;
182  
183 } # End if ($string_part =~ /$this_decomp/i)
184  
185 0 $options->debug_text($options->debug_text . "\n");
186 } # End DECOMP: foreach $decomp (@{ $self->{decomplist}->{$keyword} })
187  
188 } # End if ( ($string_part =~ /\b$keyword\b/i or $keyword eq $goto)
189  
190 } # End KEYWORD: foreach $keyword (keys %{ $self->{decomplist})
191
192 } # End STRING_PARTS: foreach $string_part (@string_parts) {
193  
194 8 ▶ $reasmb = $self->transform("xnone", "") if $reasmb eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $reasmb eq "" 2 6 8
195
196 8 $reasmb = $self->postprocess($reasmb);
197
198 8 ▶ if ($options->memory_on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->memory_on) 8 0 8
199 # Shift out the least-recent item from the bottom
200 # of the memory stack if the stack exceeds the max size.
201 8 ▶ shift @{$options->memory} if scalar @{$options->memory} >= $options->max_memory_size;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$options->memory;} >= $options->max_memory_size 0 8 8
202 # push in the current reasem string
203 8 push @{$options->memory}, $reasmb;
204  
205 $options->debug_text(sprintf("%s \t%d item(s) in memory.\n",
206 8 $options->debug_text, scalar @{$options->memory} ));
207 }
208  
209 # Save the return string so that forgetful calling programs
210 # can ask the bot what the last reply was.
211 8 $options->transform_text($reasmb);
212 8 return $reasmb;
213 }
214  
215 9 1;
216  
217 __END__
« Chatbot.pm ↑ Summary Chatbot/ScriptParser.pm »