diff --git a/.gitignore b/.gitignore index b1d4982..51db30d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -brain +brain* *.swp config.yml diff --git a/README.md b/README.md index 3037bc2..95cbb27 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/export-brain.pl b/export-brain.pl new file mode 100644 index 0000000..d7285d3 --- /dev/null +++ b/export-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"; +} diff --git a/import-brain.pl b/import-brain.pl index 468be9a..ceee247 100644 --- a/import-brain.pl +++ b/import-brain.pl @@ -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 ; +my $line = ; +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 () { + 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 ; +} + close BRAIN;