2014-07-22 03:46:16 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-07-22 03:46:16 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $CFACTS = 'cfacts.txt';
|
2014-08-31 22:23:27 +02:00
|
|
|
my $CJEOPARDY_DATA = 'cjeopardy.dat';
|
2014-07-22 03:46:16 +02:00
|
|
|
|
|
|
|
my $text = join(' ', @ARGV);
|
|
|
|
|
2014-08-31 22:23:27 +02:00
|
|
|
sub encode { my $str = shift; $str =~ s/\\(.)/{sprintf "\\%03d", ord($1)}/ge; return $str; }
|
|
|
|
sub decode { my $str = shift; $str =~ s/\\(\d{3})/{"\\" . chr($1)}/ge; return $str }
|
|
|
|
|
|
|
|
my $jeopardy_answers;
|
|
|
|
open my $fh, "<", $CJEOPARDY_DATA;
|
|
|
|
if (defined $fh) {
|
|
|
|
$jeopardy_answers = <$fh>;
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @valid_answers = map { lc decode $_ } split /\|/, encode $jeopardy_answers;
|
|
|
|
|
2014-07-22 03:46:16 +02:00
|
|
|
my @facts;
|
2014-08-31 22:23:27 +02:00
|
|
|
open $fh, "<", $CFACTS or die "Could not open $CFACTS: $!";
|
2014-07-22 03:46:16 +02:00
|
|
|
while (my $fact = <$fh>) {
|
|
|
|
next if length $text and $fact !~ /\Q$text\E/i;
|
2014-08-31 22:23:27 +02:00
|
|
|
next if grep { $fact =~ /\Q$_\E/i } @valid_answers;
|
2014-07-22 03:46:16 +02:00
|
|
|
push @facts, $fact;
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
if (not @facts) {
|
|
|
|
print "No fact containing text $text found.\n";
|
|
|
|
} else {
|
|
|
|
print $facts[int rand(@facts)], "\n";
|
|
|
|
}
|