File Coverage

blib/lib/Eliza/Chatbot.pm
Criterion Covered Total %
statement 26 52 50.0
branch 2 18 11.1
condition n/a
subroutine 8 9 88.8
pod 2 2 100.0
total 38 81 46.9


line stmt bran cond sub pod time code
1             package Eliza::Chatbot;
2              
3 9     9   34995 use strict;
  9         15  
  9         254  
4 9     9   38 use warnings;
  9         12  
  9         229  
5              
6 9     9   4769 use Moo;
  9         107103  
  9         46  
7 9     9   16181 use MooX::LazierAttributes;
  9         23088  
  9         37  
8 9     9   5177 use Eliza::Chatbot::Option;
  9         30  
  9         303  
9 9     9   4949 use Eliza::Chatbot::Brain;
  9         24  
  9         4089  
10              
11             our $VERSION = '0.07';
12              
13             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   28 my $self = shift;
22 4         45 my $options = Eliza::Chatbot::Option->new();
23 4         2584 foreach my $field (@user_options) {
24 20 100       90 if (my $val = $self->$field) {
25 3         67 $options->$field($val);
26             }
27             }
28 4         63 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 5889 my ($self, $user_input) = @_;
94 8         154 return $self->brain->transform($user_input, '');
95             }
96              
97             1;
98              
99             __END__