handle non-ascii strings properly

This commit is contained in:
Al Beano 2016-07-04 21:32:37 +01:00
parent d0c30f9634
commit 53865b20a5
3 changed files with 7 additions and 1 deletions

View File

@ -11,6 +11,7 @@ use lib $Bin;
use MarkovBot::Config;
use MarkovBot::Redis;
use Encode qw(encode);
sub learn( $ ) {
# input - line to be learned
@ -19,7 +20,7 @@ sub learn( $ ) {
my $redis = redis();
my $redis_prefix = config("redis_prefix");
$_ = shift;
$_ = encode("UTF-8", shift);
# learn - add a line of text to the brain
$_ = $_." ___end___";

View File

@ -11,14 +11,17 @@ use lib $Bin;
use MarkovBot::Config;
use MarkovBot::Redis;
use Encode qw(decode encode);
sub markov( $ ) {
# markov - given two starting words, returns a markov chain result
my $redis = redis;
my $p = config "redis_prefix";
my $s = shift;
my @s = @{$s};
@s = map {encode("UTF-8", $_)} @s;
if (!$redis->llen("$p:chains:$s[0],$s[1]")) {
# Phrase is not known
@ -34,6 +37,7 @@ sub markov( $ ) {
}
pop @s;
@s = map {decode("UTF-8", $_)} @s;
return join " ", @s;
}

View File

@ -5,3 +5,4 @@ requires "Redis" => "0";
requires "YAML::Tiny" => "0";
requires "POE::Component::SSLify" => "0";
requires "Scalar::Util" => "0";
requires "Encode" => "0";