Chatbot.pm

filter: Statementstmt 53.5% 30 / 56 Branchbran 11.1% 2 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 49.4% 42 / 85 SCAR 35.0
CC
11
Cov
43%
command_interface
90.0
_build_brain
3.0
BEGIN
1.0
CRAP 33.1
↑ Summary Chatbot/Brain.pm »
1 package Eliza::Chatbot;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use MooX::LazierAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Eliza::Chatbot::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Eliza::Chatbot::Brain;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = '0.09';
12  
13 9 our @user_options = qw(name script_file debug prompts_on memory_on);
14  
15 9 attributes (
16 [@user_options] => [rw],
17 brain => [rw, nan, { lzy, bld }],
18 );
19  
20 sub _build_brain {
21 4 ▶ my $self = shift;
Subroutine _build_brain: called CC=3 SCAR=11.0
22 4 my $options = Eliza::Chatbot::Option->new();
23 4 foreach my $field (@user_options) {
24 20 ▶ if (my $val = $self->$field) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->$field) 3 17 20
25 3 $options->$field($val);
26 }
27 }
28 4 return Eliza::Chatbot::Brain->new(options => $options);
29 }
30  
31 sub command_interface {
32 0 ▶ my $self = shift;
Subroutine command_interface: not called CC=9 SCAR=45.0
Pod documented
33 0 my ($reply, $previous_user_input, $user_input) = "";
34
35 0 my $options = $self->brain->options;
36 0 $options->botprompt($options->name . ":\t");
37 0 $options->userprompt("you:\t");
38  
39 # Seed the rand number generator.
40 0 srand( time() ^ ($$ + ($$ << 15)) );
41  
42 # print the Eliza prompt
43 0 ▶ print $options->botprompt if $options->prompts_on;
Branch: 1 branch
BranchTrueFalseTotal
if $options->prompts_on 0 0 0
44  
45 # print an initial greeting
46 0 print $options->welcome_message . "\n";
47  
48 0 while (1) {
49 0 ▶ print $options->userprompt if $options->prompts_on;
Branch: 1 branch
BranchTrueFalseTotal
if $options->prompts_on 0 0 0
50
51 0 $previous_user_input = $user_input;
52 0 chomp( $user_input = <STDIN> );
53  
54 # If the user enters the work "debug",
55 # the toggle on/off Eliza's debug output.
56 0 ▶ if ($user_input eq "debug") {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_input eq "debug") 0 0 0
57 0 $options->debug( ! $options->debug );
58 0 $user_input = $previous_user_input;
59 }
60  
61 # If the user enters the word "memory"
62 # then use the _debug_memory method to dump out
63 # the current contents of Eliza's memory
64 0 ▶ if ($user_input =~ m{memory|debug memory}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_input =~ /memory|debug memory/msx) 0 0 0
65 0 print $self->brain->_debug_memory();
66 0 redo;
67 }
68  
69 # If the user enters the word "debug that"
70 # the dump out the debugging of the most recent
71 # call to transform
72 0 ▶ if ($user_input eq "debug that") {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_input eq "debug that") 0 0 0
73 0 print $options->debug_text;
74 0 redo;
75 }
76  
77 # Invoke the transform method to generate a reply
78 0 $reply = $self->brain->transform($user_input, '');
79  
80 # Print out the debugging text if debugging is set to on.
81 # This variable should have been set by the transform method
82 0 ▶ print $options->debug_text if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
83  
84 # print the actual reply
85 0 ▶ print $options->botprompt if $options->prompts_on;
Branch: 1 branch
BranchTrueFalseTotal
if $options->prompts_on 0 0 0
86 0 print sprintf("%s\n", $reply);
87  
88 0 ▶ last if $self->brain->last;
Branch: 1 branch
BranchTrueFalseTotal
if $self->brain->last 0 0 0
89 }
90 }
91  
92 sub instance {
93 8 ▶ my ($self, $user_input) = @_;
Subroutine instance: called CC=1 SCAR=0.0
Pod documented
94 8 return $self->brain->transform($user_input, '');
95 }
96  
97 9 1;
98  
99 __END__
↑ Summary Chatbot/Brain.pm »