mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-10 20:12:35 +01:00
Plugin/WordMorph: add neighbors
subcommand; improve custom
error messages
This commit is contained in:
parent
0c22896f66
commit
219851ffaf
@ -35,7 +35,7 @@ sub unload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use constant {
|
use constant {
|
||||||
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> <word2> | solve <solution> | hint [from direction] | check <word> | show | giveup',
|
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> <word2> | solve <solution> | hint [from direction] | check <word> | neighbors <word> | show | giveup',
|
||||||
NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.",
|
NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.",
|
||||||
DB_UNAVAILABLE => "Word morph database not available.",
|
DB_UNAVAILABLE => "Word morph database not available.",
|
||||||
LEFT => 0,
|
LEFT => 0,
|
||||||
@ -56,6 +56,22 @@ sub wordmorph {
|
|||||||
my $channel = $context->{from};
|
my $channel = $context->{from};
|
||||||
|
|
||||||
given ($command) {
|
given ($command) {
|
||||||
|
when ('neighbors') {
|
||||||
|
if (!@args || @args > 1) {
|
||||||
|
return 'Usage: wordmorph neighbors <word>; list the neighbors of a given word';
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB_UNAVAILABLE if not $self->{db};
|
||||||
|
|
||||||
|
if (not exists $self->{db}->{length $args[0]}->{$args[0]}) {
|
||||||
|
return "I do not know this word `$args[0]`.";
|
||||||
|
}
|
||||||
|
|
||||||
|
my @neighbors = @{$self->{db}->{length $args[0]}->{$args[0]}};
|
||||||
|
my $count = @neighbors;
|
||||||
|
return "`$args[0]` has $count neighbor" . ($count != 1 ? 's' : '') . ": " . join(', ', sort @neighbors);
|
||||||
|
}
|
||||||
|
|
||||||
when ('check') {
|
when ('check') {
|
||||||
if (!@args || @args > 1) {
|
if (!@args || @args > 1) {
|
||||||
return 'Usage: wordmorph check <word>; check if a word exists in the Word Morph database';
|
return 'Usage: wordmorph check <word>; check if a word exists in the Word Morph database';
|
||||||
@ -400,11 +416,14 @@ sub transform {
|
|||||||
my @path;
|
my @path;
|
||||||
my (%leftstarts, %rightstarts);
|
my (%leftstarts, %rightstarts);
|
||||||
|
|
||||||
|
die "I do not know this word `$left`.\n" if not exists $list->{$left};
|
||||||
|
die "I do not know this word `$right`.\n" if not exists $list->{$right};
|
||||||
|
|
||||||
SEARCH:
|
SEARCH:
|
||||||
for (;;) {
|
for (;;) {
|
||||||
my @left_ids = $leftstart..$#left; # choose array of indices of new words
|
my @left_ids = $leftstart..$#left; # choose array of indices of new words
|
||||||
$leftstart = $#left;
|
$leftstart = $#left;
|
||||||
die "Cannot create word morph! Bad word '$left' :(\n" if $leftstarts{$leftstart}++ > 2; # finish search if the path could not be found
|
die "Cannot find a path from `$left` to `$right`.\n" if $leftstarts{$leftstart}++ > 2; # finish search if the path could not be found
|
||||||
for my $id (@left_ids) { # come through all new words
|
for my $id (@left_ids) { # come through all new words
|
||||||
my @prefix = @{$left[$id]};
|
my @prefix = @{$left[$id]};
|
||||||
my $searched = pop @prefix;
|
my $searched = pop @prefix;
|
||||||
@ -425,7 +444,7 @@ sub transform {
|
|||||||
|
|
||||||
my @right_ids = $rightstart..$#right; # all the same :) the tree is build from both ends to speed up the process
|
my @right_ids = $rightstart..$#right; # all the same :) the tree is build from both ends to speed up the process
|
||||||
$rightstart = $#right;
|
$rightstart = $#right;
|
||||||
die "Cannot create word morph! Bad word '$right'\n" if $rightstarts{$rightstart}++ > 2;
|
die "Cannot find a path from `$left` to `$right`.\n" if $rightstarts{$rightstart}++ > 2; # finish search if the path could not be found
|
||||||
for my $id (@right_ids) { # build right relational table
|
for my $id (@right_ids) { # build right relational table
|
||||||
my @prefix = @{$right[$id]};
|
my @prefix = @{$right[$id]};
|
||||||
my $searched = pop @prefix;
|
my $searched = pop @prefix;
|
||||||
|
@ -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 => 4582,
|
BUILD_REVISION => 4584,
|
||||||
BUILD_DATE => "2022-09-06",
|
BUILD_DATE => "2022-09-07",
|
||||||
};
|
};
|
||||||
|
|
||||||
sub initialize {}
|
sub initialize {}
|
||||||
|
Loading…
Reference in New Issue
Block a user