shithead-ng/MarkovBot/Learning.pm

43 lines
815 B
Perl
Raw Normal View History

2016-04-30 23:53:26 +02:00
package MarkovBot::Learning;
use base qw(Exporter);
2016-07-04 21:32:07 +02:00
use 5.010;
use strict;
use warnings;
2016-04-30 23:53:26 +02:00
our @EXPORT = qw(learn);
use FindBin qw($Bin);
use lib $Bin;
use MarkovBot::Config;
use MarkovBot::Redis;
2016-07-04 22:32:37 +02:00
use Encode qw(encode);
2016-04-30 23:53:26 +02:00
sub learn( $ ) {
# input - line to be learned
# returns - nothing
my $redis = redis();
my $redis_prefix = config("redis_prefix");
2016-07-04 22:32:37 +02:00
$_ = encode("UTF-8", shift);
2016-04-30 23:53:26 +02:00
# learn - add a line of text to the brain
$_ = $_." ___end___";
$_ = lc $_;
$_ =~ y/\,\.\!\?\(\)//d;
# Split it into individual words
my @words = split(' ', $_);
# Disregard short sentences
return if scalar(@words < 4);
for (my $i = 0; $i <= $#words-2; $i++) {
# $words[$i] $words[$i+1] => $words[$i+2]
$redis->rpush("$redis_prefix:chains:$words[$i],$words[$i+1]", $words[$i+2]);
}
}
1;