3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-02 18:19:33 +01:00
pbot/modules/getcfact.pl

37 lines
881 B
Perl
Raw Normal View History

2014-07-22 03:46:16 +02:00
#!/usr/bin/env perl
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";
}