Plugin/FuncBuiltin: add `maybe-the` function

The `maybe-the` function examines the argument's part-of-speech
classification (noun, verb, etc) to determine whether to prepend
the word "the".
This commit is contained in:
Pragmatic Software 2022-07-08 09:11:56 -07:00
parent 833c20efbb
commit 3ab4ed0a81
1 changed files with 32 additions and 0 deletions

View File

@ -12,6 +12,7 @@ use PBot::Imports;
use PBot::Core::Utils::Indefinite;
use Lingua::EN::Tagger;
use URI::Escape qw/uri_escape_utf8/;
sub initialize {
@ -72,6 +73,16 @@ sub initialize {
subref => sub { $self->func_ana(@_) }
}
);
$self->{pbot}->{functions}->register(
'maybe-the',
{
desc => 'prepend "the" in front of text depending on the part-of-speech of the first word in text',
usage => 'maybe-the <text>',
subref => sub { $self->func_maybe_the(@_) }
}
);
$self->{tagger} = Lingua::EN::Tagger->new;
}
sub unload {
@ -83,6 +94,7 @@ sub unload {
$self->{pbot}->{functions}->unregister('unquote');
$self->{pbot}->{functions}->unregister('uri_escape');
$self->{pbot}->{functions}->unregister('ana');
$self->{pbot}->{functions}->unregister('maybe-the');
}
sub func_unquote {
@ -146,4 +158,24 @@ sub func_ana {
return $text;
}
sub func_maybe_the {
my $self = shift;
my $text = "@_";
my ($word) = $text =~ m/^\s*([^',.; ]+)/;
# don't prepend "the" if a proper-noun nick follows
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return $text;
}
my $tagged = $self->{tagger}->add_tags($word);
if ($tagged !~ m/^\s*<(?:det|prps?|cd|in|nnp|to|rb|wdt|vbg)>/) {
$text = "the $text";
}
return $text;
}
1;