applets/wiktionary: improve output

This commit is contained in:
Pragmatic Software 2022-06-27 20:35:02 -07:00
parent 41b69bdfa5
commit 3ef58c2bfa
3 changed files with 25 additions and 9 deletions

27
applets/wiktionary.pl vendored
View File

@ -18,6 +18,8 @@ use Encode;
use Getopt::Long qw/GetOptionsFromArray/;
use JSON;
sub flatten { map { ref eq 'ARRAY' ? flatten(@$_) : $_ } @_ }
binmode(STDOUT, ":utf8");
@ARGV = map { decode('UTF-8', $_, 1) } @ARGV;
@ -93,16 +95,27 @@ if (not defined $entries) {
$cache->set($cache_id, $entries);
}
my @valid_sections = qw/definitions etymology pronunciations/;
if ($ENV{DEBUG}) {
use Data::Dumper;
print Dumper($entries), "\n";
}
my @valid_sections = qw/definitions etymology pronunciations participle/;
if (not grep { $_ eq $section } @valid_sections) {
print "Unknown section `$section`. Available sections are: " . join(', ', sort @valid_sections) . "\n";
exit 1;
}
my $entries_text = $section;
my $total_entries_count = @$entries;
my $entries_text = $section;
if ($total_entries_count == 0) {
$entries_text =~ s/y$/ies/;
print "No $entries_text for `$term`.\n";
exit 1;
}
if ($num > $total_entries_count) {
if ($total_entries_count == 1) {
@ -159,11 +172,11 @@ for (my $i = $start; $i < $num; $i++) {
foreach my $definition (@{$entry->{definitions}}) {
$text .= "$definition->{partOfSpeech}) ";
$text .= join("\n\n", @{$definition->{text}}) . "\n\n";
$text .= join("\n\n", flatten @{$definition->{text}}) . "\n\n";
if (@{$definition->{examples}}) {
$text .= "examples:\n\n";
$text .= join("\n\n", @{$definition->{examples}}) . "\n\n";
$text .= join("\n\n", map { $_->{text} } @{$definition->{examples}}) . "\n\n";
}
}
@ -174,8 +187,8 @@ for (my $i = $start; $i < $num; $i++) {
if (not @results) {
$entries_text =~ s/y$/ies/;
print "There are no $entries_text for $term.\n";
exit 0;
print "There are no $entries_text for `$term`.\n";
exit 1;
}
my $total_results_count = @results;
@ -186,8 +199,6 @@ if ($total_results_count == 1) {
$entries_text =~ s/y$/ies/;
}
print "$total_entries_count entries, " if $total_entries_count > 1;
print "$total_results_count $entries_text for $term:\n\n";
if ($unique) {

View File

@ -4,6 +4,11 @@
#
# This was written for wiktionary.pl since Wiktionary::Parser in CPAN
# seems to be broken and abandoned.
#
# Important: This uses a custom fork of wiktionaryparser which contains
# numerous fixes. To install it use:
#
# pip install git+https://github.com/pragma-/WiktionaryParser
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT

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 => 4546,
BUILD_REVISION => 4547,
BUILD_DATE => "2022-06-27",
};