diff --git a/lib/PBot/Plugin/FuncBuiltins.pm b/lib/PBot/Plugin/FuncBuiltins.pm index 901d27d8..ffe85131 100644 --- a/lib/PBot/Plugin/FuncBuiltins.pm +++ b/lib/PBot/Plugin/FuncBuiltins.pm @@ -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 ', + 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; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 2a23940c..29a11696 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -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 {}