experimental export function
This commit is contained in:
parent
61fd02c600
commit
cd034f2e09
|
@ -1,3 +1,3 @@
|
|||
brain
|
||||
brain*
|
||||
*.swp
|
||||
config.yml
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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";
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue