File Coverage

blib/lib/Eliza/Chatbot.pm
Criterion Covered Total %
statement 29 55 52.7
branch 2 18 11.1
condition n/a
subroutine 9 10 90.0
pod 2 2 100.0
total 42 85 49.4


line stmt bran cond sub pod time code
1             package Eliza::Chatbot;
2              
3 9     9   278126 use strict;
  9         17  
  9         327  
4 9     9   79 use warnings;
  9         16  
  9         473  
5              
6 9     9   5279 use Moo;
  9         98988  
  9         43  
7 9     9   28493 use MooX::LazierAttributes;
  9         269466  
  9         755  
8 9     9   7261 use Eliza::Chatbot::Option;
  9         122  
  9         410  
9 9     9   12265 use Eliza::Chatbot::Brain;
  9         115  
  9         415  
10 9     9   4497 use Const::XS qw/const/;
  9         5119  
  9         6010  
11             our $VERSION = '0.12';
12              
13             const our @user_options => qw(name script_file debug prompts_on memory_on);
14              
15             attributes (
16             [@user_options] => [rw],
17             brain => [rw, nan, { lzy, bld }],
18             );
19              
20             sub _build_brain {
21 4     4   51 my $self = shift;
22 4         82 my $options = Eliza::Chatbot::Option->new();
23 4         5993 foreach my $field (@user_options) {
24 20 100       165 if (my $val = $self->$field) {
25 3         171 $options->$field($val);
26             }
27             }
28 4         89 return Eliza::Chatbot::Brain->new(options => $options);
29             }
30              
31             sub command_interface {
32 0     0 1 0 my $self = shift;
33 0         0 my ($reply, $previous_user_input, $user_input) = "";
34            
35 0         0 my $options = $self->brain->options;
36 0         0 $options->botprompt($options->name . ":\t");
37 0         0 $options->userprompt("you:\t");
38              
39             # Seed the rand number generator.
40 0         0 srand( time() ^ ($$ + ($$ << 15)) );
41              
42             # print the Eliza prompt
43 0 0       0 print $options->botprompt if $options->prompts_on;
44              
45             # print an initial greeting
46 0         0 print $options->welcome_message . "\n";
47              
48 0         0 while (1) {
49 0 0       0 print $options->userprompt if $options->prompts_on;
50            
51 0         0 $previous_user_input = $user_input;
52 0         0 chomp( $user_input = );
53              
54             # If the user enters the work "debug",
55             # the toggle on/off Eliza's debug output.
56 0 0       0 if ($user_input eq "debug") {
57 0         0 $options->debug( ! $options->debug );
58 0         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 0       0 if ($user_input =~ m{memory|debug memory}xms) {
65 0         0 print $self->brain->_debug_memory();
66 0         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 0       0 if ($user_input eq "debug that") {
73 0         0 print $options->debug_text;
74 0         0 redo;
75             }
76              
77             # Invoke the transform method to generate a reply
78 0         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 0       0 print $options->debug_text if $self->debug;
83              
84             # print the actual reply
85 0 0       0 print $options->botprompt if $options->prompts_on;
86 0         0 print sprintf("%s\n", $reply);
87              
88 0 0       0 last if $self->brain->last;
89             }
90             }
91              
92             sub instance {
93 8     8 1 646421 my ($self, $user_input) = @_;
94 8         245 return $self->brain->transform($user_input, '');
95             }
96              
97             1;
98              
99             __END__