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

applets/wiktionary: refactor relations

This commit is contained in:
Pragmatic Software 2024-10-02 20:13:45 -07:00
parent 68bd1e4d86
commit bb1b43b089
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 57 additions and 48 deletions

69
applets/wiktionary.pl vendored
View File

@ -107,7 +107,7 @@ if ($ENV{DEBUG}) {
print Dumper($entries), "\n";
}
my @valid_sections = qw/definitions etymology pronunciations participle/;
my @valid_sections = qw/definitions etymology pronunciations/;
if (not grep { $_ eq $section } @valid_sections) {
print "Unknown section `$section`. Available sections are: " . join(', ', sort @valid_sections) . "\n";
@ -196,47 +196,37 @@ 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 .= "$dsep$definition->{partOfSpeech}) ";
$dsep = ";\n\n";
my $entry = -1;
my $rsep = '';
my $relations = '';
if (defined $relation) {
my $relations = $definition->{relatedWords};
foreach my $rel (@$relations) {
foreach my $rel ($definition->{relatedWords}->@*) {
$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 ($rel->{words}->@*) {
$relations .= "$rel->{relationshipType}) ";
$relations .= join (", ", $rel->{words}->@*);
$relations .= "\n\n";
}
}
}
next if not length $relations;
}
my $entry = -1;
$text .= "$definition->{partOfSpeech}) ";
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";
if (@{$definition->{examples}}) {
if (!defined $relation && @{$definition->{examples}}) {
foreach my $example (@{$definition->{examples}}) {
if ($example->{index} == $entry) {
$text .= " ($example->{text})\n";
@ -247,7 +237,8 @@ for (my $i = $start; $i < $num; $i++) {
$entry++;
$text .= "\n";
}
}
$text .= $relations;
}
push @results, $text if length $text;
@ -256,10 +247,8 @@ for (my $i = $start; $i < $num; $i++) {
if (not @results) {
if (defined $part_of_speech) {
$entries_text = $part_of_speech;
}
print "There is no $entries_text available for `$term`.\n";
if (not exists $parts_of_speech{$part_of_speech}) {
print "There is no $part_of_speech available for `$term`.\n";
my @pos = sort keys %parts_of_speech;
@ -267,6 +256,26 @@ if (not @results) {
print 'Try ', join (', ', @pos), ".\n";
}
exit 1;
}
}
if (defined $relation) {
$relation = 'relations' if not length $relation;
if (not exists $relationships{$relation}) {
print "There are no $relation available for `$term`.\n";
my @relations = sort keys %relationships;
if (@relations) {
print 'Try ', join (', ', @relations), ".\n";
}
exit 1;
}
}
print "There is no $entries_text available for `$term`.\n";
exit 1;
}

View File

@ -25,7 +25,7 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4795,
BUILD_REVISION => 4796,
BUILD_DATE => "2024-10-02",
};