3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-17 01:19:31 +01:00

applets/wiktionary: add -r flag for relations

This commit is contained in:
Pragmatic Software 2024-10-02 18:50:56 -07:00
parent a1ec2bfada
commit 68bd1e4d86
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 44 additions and 15 deletions

35
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> [-pos <part of speech>] [-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] [-r [relation]] [-l <language>] [-n <entry number>]; -e for etymology; -p for pronunciation\n";
my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p, $part_of_speech);
my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p, $part_of_speech, $relation);
{
my $opt_error;
@ -45,6 +45,7 @@ my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p, $part_of_speech
'all|a' => \$all,
'unique|u' => \$unique,
'pos=s' => \$part_of_speech,
'r:s' => \$relation,
'p' => \$opt_p,
'e' => \$opt_e,
);
@ -163,6 +164,7 @@ if ($num <= 0 or $all or $unique) {
my @results;
my %parts_of_speech;
my %relationships;
for (my $i = $start; $i < $num; $i++) {
my $entry = $entries->[$i];
@ -194,16 +196,42 @@ for (my $i = $start; $i < $num; $i++) {
elsif ($section eq 'definitions') {
my $text;
my $dsep = '';
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 .= "$dsep$definition->{partOfSpeech}) ";
$dsep = ";\n\n";
my $entry = -1;
my $rsep = '';
if (defined $relation) {
my $relations = $definition->{relatedWords};
foreach my $rel (@$relations) {
$relationships{$rel->{relationshipType}} = 1;
if (!length $relation || $relation eq '*' || $rel->{relationshipType} eq $relation) {
$text .= "$rsep$rel->{relationshipType}) ";
$text .= join (",\n", $rel->{words}->@*);
$rsep = ";\n\n";
$entry++;
}
}
if ($entry == -1) {
$relation = 'relations' if not length $relation;
print "There are no $relation available for `$term`.\n";
my @rel = sort keys %relationships;
if (@rel) {
print 'Try ', join(', ', @rel), ".\n";
}
exit 1;
}
} else {
foreach my $def (flatten @{$definition->{text}}) {
$def =~ s/^#//;
$text .= "$def\n";
@ -220,6 +248,7 @@ for (my $i = $start; $i < $num; $i++) {
$text .= "\n";
}
}
}
push @results, $text if length $text;
}

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 => 4793,
BUILD_DATE => "2024-10-01",
BUILD_REVISION => 4795,
BUILD_DATE => "2024-10-02",
};
sub initialize {}