From c0759152ce70e3b5cdc637a4a6e82eca87d262c9 Mon Sep 17 00:00:00 2001 From: Al Beano Date: Wed, 6 Jul 2016 14:31:28 +0100 Subject: [PATCH] bugfixes and fine tuning for import/export --- export-brain.pl | 7 +++++-- import-brain.pl | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/export-brain.pl b/export-brain.pl index d7285d3..944e553 100644 --- a/export-brain.pl +++ b/export-brain.pl @@ -18,9 +18,12 @@ 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"; +say "shithead-ng file (version 0)\nthis file cannot be used by other markov chain programs, without modification.\ndo 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."; for (@keys) { + # mark the beginning of the record + print "\n\x1c"; + my $prefix = quotemeta $p; my $k = $_; $k =~ s/$prefix:chains://; @@ -28,5 +31,5 @@ for (@keys) { my @words = split ",", $k; push @words, @{ $redis->lrange($_, 0, -1) }; @words = map { $_ eq '___end___' ? "\x1d" : $_ } @words; - print join("\x1f", @words), "\n\x1c"; + print join "\x1f", @words; } diff --git a/import-brain.pl b/import-brain.pl index ceee247..ec5a5f7 100644 --- a/import-brain.pl +++ b/import-brain.pl @@ -16,18 +16,19 @@ close BRAIN; # open it again from the top open BRAIN, $brain_file or die $!; -if ($line =~ m/^#\ shithead-ng\ file\ \(version\ .+\)$/) { +if ($line =~ m/^shithead-ng\ file\ \(version\ .+\)$/) { # shithead-ng specific file while () { next unless $_ =~ m/^\x1c/; $_ =~ s/^\x1c//; - my @words = split "\x1f", @words; + chomp; + my @words = split "\x1f", $_; @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); + $redis->lpush("$p:chains:".shift(@words).",".shift(@words), @words); } } else { # generic irc log file