0

Add lat brain case for handling famous latin phrases request.

This commit is contained in:
eleksir
2020-10-24 20:02:19 +03:00
parent b13ea5bca2
commit dd0f656e57
4 changed files with 1388 additions and 3 deletions

33
bin/lat_init.pl Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/perl
# Used to initialize famous latin phrases markov chain brain
use 5.018;
use strict;
use warnings;
use utf8;
use English qw( -no_match_vars );
use lib qw(./vendor_perl/lib/perl5);
use File::Path qw(mkpath);
use Config::INI::Reader;
use Hailo;
my $config = Config::INI::Reader->read_file ('data/config.ini');
my $braindir = $config->{brain}->{dir};
unless (-d $braindir) {
mkpath ($braindir) or die "Unable to create $braindir: $OS_ERROR\n";
}
my $lat = Hailo->new (
brain => "$braindir/lat.brain.sqlite",
order => 2
);
$lat->train('data/phrases.txt');
my ($tokens, $expressions) = ($lat->stats())[0,1];
printf "Total tokens: %s\nTotal expressins: %s\n", $tokens, $expressions;
$lat->save();
exit 0;
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:

View File

@@ -1,4 +1,5 @@
requires 'Plack';
requires 'Hailo';
requires 'Config::INI::Reader'; requires 'Config::INI::Reader';
requires 'File::Path';
requires 'Hailo';
requires 'Plack';
requires 'local::lib'; requires 'local::lib';

1336
data/phrases.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -14,12 +14,17 @@ unless (-d $braindir) {
die "Directory $braindir does not exists\n"; die "Directory $braindir does not exists\n";
} }
unless (-f "$braindir/lat.brain.sqlite") {
die "Famous latin phrases module is not initialized. Run bin/lat_init.pl\n";
}
my %brain; my %brain;
# just return answer # just return answer
sub answer (@) { sub answer (@) {
my $chat = shift; my $chat = shift;
my $phrase = shift; my $phrase = shift;
my $msg;
unless (defined $brain{$chat}) { unless (defined $brain{$chat}) {
my $brain = sprintf '%s/%s.brain.sqlite', $braindir, $chat; my $brain = sprintf '%s/%s.brain.sqlite', $braindir, $chat;
@@ -30,7 +35,11 @@ sub answer (@) {
); );
} }
my $msg = $brain{$chat}->reply($phrase); if ($chat eq 'lat') {
$msg = $brain{$chat}->reply();
} else {
$msg = $brain{$chat}->reply($phrase);
}
return ('200', 'text/plain', $msg); return ('200', 'text/plain', $msg);
} }
@@ -68,6 +77,10 @@ my $app = sub {
($status, $content, $msg) = answer ($env->{HTTP_CHAT}, $env->{HTTP_PHRASE}); ($status, $content, $msg) = answer ($env->{HTTP_CHAT}, $env->{HTTP_PHRASE});
} }
if ($env->{PATH_INFO} eq '/lat' && $env->{REQUEST_METHOD} eq 'POST') {
($status, $content, $msg) = answer ('lat', undef);
}
return [ return [
$status, $status,
[ 'Content-Type' => $content, 'Content-Length' => length $msg ], [ 'Content-Type' => $content, 'Content-Length' => length $msg ],
@@ -75,5 +88,7 @@ my $app = sub {
]; ];
}; };
1;
__END__ __END__
# vim: set ft=perl noet ai ts=4 sw=4 sts=4: # vim: set ft=perl noet ai ts=4 sw=4 sts=4: