3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-22 18:52:40 +01:00

Plugin/FuncBuiltins: add maybe-to

This commit is contained in:
Pragmatic Software 2024-11-04 01:53:41 -08:00
parent 9ebc77f4da
commit ead34c81c9
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 39 additions and 3 deletions

View File

@ -96,6 +96,14 @@ sub initialize($self, %conf) {
subref => sub { $self->func_maybe_the(@_) }
}
);
$self->{pbot}->{functions}->register(
'maybe-to',
{
desc => 'prepend "to" in front of text depending on the part-of-speech of the first word in text',
usage => 'maybe-to <text>',
subref => sub { $self->func_maybe_to(@_) }
}
);
$self->{tagger} = Lingua::EN::Tagger->new;
}
@ -111,6 +119,7 @@ sub unload($self) {
$self->{pbot}->{functions}->unregister('uri_escape');
$self->{pbot}->{functions}->unregister('ana');
$self->{pbot}->{functions}->unregister('maybe-the');
$self->{pbot}->{functions}->unregister('maybe-to');
}
sub func_unquote($self, @rest) {
@ -191,7 +200,7 @@ sub func_maybe_the($self, @rest) {
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
# don't prepend "the" if a proper-noun nick follows
# don't prepend if a proper-noun nick follows
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return $text;
}
@ -210,4 +219,31 @@ sub func_maybe_the($self, @rest) {
return $text;
}
sub func_maybe_to($self, @rest) {
my $text = "@rest";
$text =~ s/^to (?:the )?//;
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
# don't prepend if a proper-noun nick follows
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return "to $text";
}
# special-case some indefinite nouns that Lingua::EN::Tagger treats as plain nouns
if ($word =~ m/(some|any|every|no)(thing|one|body|how|way|where|when|time|place)/i) {
return "to $text";
}
my $tagged = $self->{tagger}->add_tags($word);
if ($tagged !~ m/^\s*<(?:det|prps?|cd|in|nnp|to|rb|wdt|rbr|jjr)>/) {
$text = "to the $text";
} else {
$text = "to $text";
}
return $text;
}
1;

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 => 4833,
BUILD_DATE => "2024-11-03",
BUILD_REVISION => 4834,
BUILD_DATE => "2024-11-04",
};
sub initialize {}