Plugin/WordMorph: add search subcommand to find words by regex

This commit is contained in:
Pragmatic Software 2024-02-22 18:56:52 -08:00
parent eb5b1540d5
commit d4880854db
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 35 additions and 3 deletions

View File

@ -32,7 +32,7 @@ sub unload($self) {
}
use constant {
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> (<word2> | <integer steps>) | solve <solution> | hint [from direction] | check <word> | neighbors <word> | show | giveup',
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> (<word2> | <integer steps>) | solve <solution> | hint [from direction] | check <word> | neighbors <word> | search <regex> | show | giveup',
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.",
LEFT => 0,
@ -270,6 +270,38 @@ sub wordmorph($self, $context) {
return "New word morph: " . $self->show_morph_with_blanks($channel) . " (Fill in the blanks)";
}
when ('search') {
if (not @args) {
return "Usage: wordmorph search <regex>";
}
return DB_UNAVAILABLE if not $self->{db};
my @words;
eval {
foreach my $length (keys $self->{db}->%*) {
foreach my $word (keys $self->{db}->{$length}->%*) {
if ($word =~ m/$args[0]/) {
push @words, $word;
}
}
}
};
if (my $except = $@) {
$except =~ s/ at \/home.*$//;
return "Error: $except";
}
if (not @words) {
return "No matching words found.";
}
return scalar @words . (@words == 1 ? ' word' : ' words') . ': ' . join(' ', @words);
}
when ('solve') {
if (not @args) {
return "Usage: wordmorph solve <solution>";

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4702,
BUILD_DATE => "2024-01-26",
BUILD_REVISION => 4703,
BUILD_DATE => "2024-02-22",
};
sub initialize {}