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:
parent
a1ec2bfada
commit
68bd1e4d86
55
applets/wiktionary.pl
vendored
55
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,30 +196,57 @@ 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 = '';
|
||||||
|
|
||||||
foreach my $def (flatten @{$definition->{text}}) {
|
if (defined $relation) {
|
||||||
$def =~ s/^#//;
|
my $relations = $definition->{relatedWords};
|
||||||
$text .= "$def\n";
|
|
||||||
|
|
||||||
if (@{$definition->{examples}}) {
|
foreach my $rel (@$relations) {
|
||||||
foreach my $example (@{$definition->{examples}}) {
|
$relationships{$rel->{relationshipType}} = 1;
|
||||||
if ($example->{index} == $entry) {
|
if (!length $relation || $relation eq '*' || $rel->{relationshipType} eq $relation) {
|
||||||
$text .= " ($example->{text})\n";
|
$text .= "$rsep$rel->{relationshipType}) ";
|
||||||
}
|
$text .= join (",\n", $rel->{words}->@*);
|
||||||
|
$rsep = ";\n\n";
|
||||||
|
$entry++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$entry++;
|
if ($entry == -1) {
|
||||||
$text .= "\n";
|
$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";
|
||||||
|
|
||||||
|
if (@{$definition->{examples}}) {
|
||||||
|
foreach my $example (@{$definition->{examples}}) {
|
||||||
|
if ($example->{index} == $entry) {
|
||||||
|
$text .= " ($example->{text})\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$entry++;
|
||||||
|
$text .= "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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