experimental export function

This commit is contained in:
Al Beano 2016-07-06 11:56:49 +01:00
parent 61fd02c600
commit cd034f2e09
4 changed files with 67 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
brain
brain*
*.swp
config.yml

View File

@ -62,3 +62,11 @@ Now, just run `perl MarkovBot.pl`.
< user> .ping
< shithead-ng> Pong!
```
## Exporting the brainfile
```bash
perl export-brain.pl > brain
```
This produces a shithead-ng brainfile which cannot (currently) be read by other programs. This is because the redis database does not contain enough information to reconstruct the brainfile it was created from. This file can, however, be read by shithead-ng's import-brain.pl.

32
export-brain.pl Normal file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env perl
#
# Export brain script
#
# This script will block redis for a little bit
# It outputs a specific shithead-ng brain file, which cannot be used
# by other (unmodified) markov chain bots.
#
use 5.010;
use strict;
use warnings;
use FindBin qw($Bin);
use lib $Bin;
use MarkovBot::Redis;
use MarkovBot::Config;
my $redis = redis();
my $p = config("redis_prefix");
my @keys = $redis->keys("$p:chains:*");
print "# shithead-ng file (version 0)\n# this file cannot be used by other markov chain programs, without modification.\n# do not remove the first line, as it is used by the import script to determine whether the file is a standard irc log or a shithead-ng specific file.\n\x1c";
for (@keys) {
my $prefix = quotemeta $p;
my $k = $_;
$k =~ s/$prefix:chains://;
my @words = split ",", $k;
push @words, @{ $redis->lrange($_, 0, -1) };
@words = map { $_ eq '___end___' ? "\x1d" : $_ } @words;
print join("\x1f", @words), "\n\x1c";
}

View File

@ -3,12 +3,35 @@ use 5.010;
use strict;
use warnings;
use FindBin qw($Bin);
use lib $Bin;
use MarkovBot::Learning;
use MarkovBot::Redis;
use MarkovBot::Config;
my $brain_file = $ARGV[0];
# read off first line
open BRAIN, $brain_file or die $!;
learn $_ while <BRAIN>;
my $line = <BRAIN>;
close BRAIN;
# open it again from the top
open BRAIN, $brain_file or die $!;
if ($line =~ m/^#\ shithead-ng\ file\ \(version\ .+\)$/) {
# shithead-ng specific file
while (<BRAIN>) {
next unless $_ =~ m/^\x1c/;
$_ =~ s/^\x1c//;
my @words = split "\x1f", @words;
@words = map { $_ eq "\x1d" ? "___end___" : $_ } @words;
# Write to redis
my $p = config("redis_prefix");
my $redis = redis();
$redis->lpush("$p:chains:".shift @words.",".shift @words, @words);
}
} else {
# generic irc log file
learn $_ while <BRAIN>;
}
close BRAIN;