Eliza-Chatbot
view release on metacpan or search on metacpan
lib/Eliza/Chatbot/Brain.pm view on Meta::CPAN
package Eliza::Chatbot::Brain;
use Moo;
use MooX::LazierAttributes;
use Ref::Util qw(is_scalarref is_blessed_arrayref);
attributes(
decomp_matches => [rw, [ ], {lzy}],
[qw/options last/] => [rw, nan, {lzy}],
);
sub preprocess {
my ($self, $string) = @_;
my @orig_words = split / /, $string;
my @converted_words;
foreach my $word ( @orig_words ) {
$word =~ s{[?!,]|but}{.}g;
push @converted_words, $word;
}
my $formated = join ' ', @converted_words;
@converted_words = split /\./, $formated;
return @converted_words;
}
sub postprocess {
my ($self, $string) = @_;
if ( is_blessed_arrayref($string) ) {
for (my $i = 1; $i < scalar @{$string}; $i++){
$string->[$i] =~ s/([,;?!]|\.*)$//;
}
} elsif ( is_scalarref(\$string) ) {
$string =~ tr/ / /s; # Eliminate any duplicate space characters.
$string =~ s/[ ][?]$/?/; # Eliminate any spaces before the question mark.
}
return $string;
}
sub _test_quit {
my ($self, $string) = @_;
foreach my $quitword (@{$self->options->data->quit}) {
return 1 if $string =~ m{$quitword}xms;
}
}
sub _debug_memory {
my $self = shift;
my @memory = @{$self->options->memory};
my $string = sprintf("%s item(s) in memory stack:\n", scalar @memory);
foreach my $msg (@memory) {
$string .= sprintf("\t\t->%s\n", $msg);
}
return $string;
}
sub transform {
my ($self, $string, $use_memory) = @_;
my ($this_decomp, $reasmbkey);
my $options = $self->options;
$options->debug_text(sprintf("\t[Pulling string \"%s\" from memory.]\n", $string))
if $use_memory;
if ($self->_test_quit($string)){
( run in 0.877 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )