3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 21:39:27 +01:00
pbot/applets/getcfact.pl
Pragmatic Software 3d97dc2c33 Rename "modules" to "applets"
"Applet" is a much better name for the external command-line
scripts and programs that can be loaded as PBot commands. They
will no longer be confused with Perl modules.

https://en.wikipedia.org/wiki/Applet
2021-11-19 18:05:50 -08:00

37 lines
1020 B
Perl
Executable File
Vendored

#!/usr/bin/env perl
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
use warnings;
use strict;
my $CFACTS = 'cfacts.txt';
my $CJEOPARDY_DATA = 'cjeopardy.dat';
my $text = join(' ', @ARGV);
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;
my @facts;
open $fh, "<", $CFACTS or die "Could not open $CFACTS: $!";
while (my $fact = <$fh>) {
next if length $text and $fact !~ /\Q$text\E/i;
next if grep { $fact =~ /\Q$_\E/i } @valid_answers;
push @facts, $fact;
}
close $fh;
if (not @facts) { print "No fact containing text $text found.\n"; }
else { print $facts[int rand(@facts)], "\n"; }