3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 11:59:43 +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; @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; my $opt_error;
@ -44,6 +44,7 @@ my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p);
'num|n=i' => \$num, 'num|n=i' => \$num,
'all|a' => \$all, 'all|a' => \$all,
'unique|u' => \$unique, 'unique|u' => \$unique,
'pos=s' => \$part_of_speech,
'p' => \$opt_p, 'p' => \$opt_p,
'e' => \$opt_e, 'e' => \$opt_e,
); );
@ -161,6 +162,7 @@ if ($num <= 0 or $all or $unique) {
} }
my @results; my @results;
my %parts_of_speech;
for (my $i = $start; $i < $num; $i++) { for (my $i = $start; $i < $num; $i++) {
my $entry = $entries->[$i]; my $entry = $entries->[$i];
@ -194,6 +196,10 @@ for (my $i = $start; $i < $num; $i++) {
my $text; my $text;
foreach my $definition (@{$entry->{definitions}}) { 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}) "; $text .= "$definition->{partOfSpeech}) ";
my $entry = -1; my $entry = -1;
@ -220,8 +226,21 @@ for (my $i = $start; $i < $num; $i++) {
} }
if (not @results) { if (not @results) {
if (defined $part_of_speech) {
$entries_text = $part_of_speech;
$entries_text =~ s/(?<![sy])$/s/;
}
$entries_text =~ s/y$/ies/; $entries_text =~ s/y$/ies/;
print "There are no $entries_text for `$term`.\n"; 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; exit 1;
} }

View File

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