3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 03:02:47 +01:00

applets/wiktionary: add part of speech filter option

This commit is contained in:
Pragmatic Software 2024-06-29 11:33:11 -07:00
parent dfd56aed57
commit ff31d71024
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 23 additions and 4 deletions

23
applets/wiktionary.pl vendored
View File

@ -24,9 +24,9 @@ binmode(STDOUT, ":utf8");
@ARGV = map { decode('UTF-8', $_, 1) } @ARGV;
my $usage = "Usage: wiktionary <term> [-e] [-p] [-l <language>] [-n <entry number>]; -e for etymology; -p for pronunciation\n";
my $usage = "Usage: wiktionary <term> [-pos <part of speech>] [-e] [-p] [-l <language>] [-n <entry number>]; -e for etymology; -p for pronunciation\n";
my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p);
my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p, $part_of_speech);
{
my $opt_error;
@ -44,6 +44,7 @@ my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p);
'num|n=i' => \$num,
'all|a' => \$all,
'unique|u' => \$unique,
'pos=s' => \$part_of_speech,
'p' => \$opt_p,
'e' => \$opt_e,
);
@ -161,6 +162,7 @@ if ($num <= 0 or $all or $unique) {
}
my @results;
my %parts_of_speech;
for (my $i = $start; $i < $num; $i++) {
my $entry = $entries->[$i];
@ -194,6 +196,10 @@ for (my $i = $start; $i < $num; $i++) {
my $text;
foreach my $definition (@{$entry->{definitions}}) {
$parts_of_speech{$definition->{partOfSpeech}} = 1;
next if defined $part_of_speech && $definition->{partOfSpeech} ne $part_of_speech;
$text .= "$definition->{partOfSpeech}) ";
my $entry = -1;
@ -220,8 +226,21 @@ for (my $i = $start; $i < $num; $i++) {
}
if (not @results) {
if (defined $part_of_speech) {
$entries_text = $part_of_speech;
$entries_text =~ s/(?<![sy])$/s/;
}
$entries_text =~ s/y$/ies/;
print "There are no $entries_text for `$term`.\n";
my @pos = sort keys %parts_of_speech;
if (@pos) {
print 'Try ', join (', ', @pos), ".\n";
}
exit 1;
}

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4764,
BUILD_DATE => "2024-06-28",
BUILD_REVISION => 4765,
BUILD_DATE => "2024-06-29",
};
sub initialize {}