mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-17 09:29:30 +01:00
applets/wiktionary: add -r flag for relations
This commit is contained in:
parent
a1ec2bfada
commit
68bd1e4d86
35
applets/wiktionary.pl
vendored
35
applets/wiktionary.pl
vendored
@ -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> [-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;
|
my $opt_error;
|
||||||
@ -45,6 +45,7 @@ my ($term, $lang, $section, $num, $all, $unique, $opt_e, $opt_p, $part_of_speech
|
|||||||
'all|a' => \$all,
|
'all|a' => \$all,
|
||||||
'unique|u' => \$unique,
|
'unique|u' => \$unique,
|
||||||
'pos=s' => \$part_of_speech,
|
'pos=s' => \$part_of_speech,
|
||||||
|
'r:s' => \$relation,
|
||||||
'p' => \$opt_p,
|
'p' => \$opt_p,
|
||||||
'e' => \$opt_e,
|
'e' => \$opt_e,
|
||||||
);
|
);
|
||||||
@ -163,6 +164,7 @@ if ($num <= 0 or $all or $unique) {
|
|||||||
|
|
||||||
my @results;
|
my @results;
|
||||||
my %parts_of_speech;
|
my %parts_of_speech;
|
||||||
|
my %relationships;
|
||||||
|
|
||||||
for (my $i = $start; $i < $num; $i++) {
|
for (my $i = $start; $i < $num; $i++) {
|
||||||
my $entry = $entries->[$i];
|
my $entry = $entries->[$i];
|
||||||
@ -194,16 +196,42 @@ for (my $i = $start; $i < $num; $i++) {
|
|||||||
|
|
||||||
elsif ($section eq 'definitions') {
|
elsif ($section eq 'definitions') {
|
||||||
my $text;
|
my $text;
|
||||||
|
my $dsep = '';
|
||||||
|
|
||||||
foreach my $definition (@{$entry->{definitions}}) {
|
foreach my $definition (@{$entry->{definitions}}) {
|
||||||
$parts_of_speech{$definition->{partOfSpeech}} = 1;
|
$parts_of_speech{$definition->{partOfSpeech}} = 1;
|
||||||
|
|
||||||
next if defined $part_of_speech && $definition->{partOfSpeech} ne $part_of_speech;
|
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 $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}}) {
|
foreach my $def (flatten @{$definition->{text}}) {
|
||||||
$def =~ s/^#//;
|
$def =~ s/^#//;
|
||||||
$text .= "$def\n";
|
$text .= "$def\n";
|
||||||
@ -220,6 +248,7 @@ for (my $i = $start; $i < $num; $i++) {
|
|||||||
$text .= "\n";
|
$text .= "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
push @results, $text if length $text;
|
push @results, $text if length $text;
|
||||||
}
|
}
|
||||||
|
@ -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 => 4793,
|
BUILD_REVISION => 4795,
|
||||||
BUILD_DATE => "2024-10-01",
|
BUILD_DATE => "2024-10-02",
|
||||||
};
|
};
|
||||||
|
|
||||||
sub initialize {}
|
sub initialize {}
|
||||||
|
Loading…
Reference in New Issue
Block a user