diff --git a/lib/PBot/Plugin/FuncBuiltins.pm b/lib/PBot/Plugin/FuncBuiltins.pm index 0633597e..b3d2f925 100644 --- a/lib/PBot/Plugin/FuncBuiltins.pm +++ b/lib/PBot/Plugin/FuncBuiltins.pm @@ -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 ', + 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;