2020-02-14 07:37:09 +01:00
|
|
|
# File: FuncBuiltins.pm
|
|
|
|
#
|
|
|
|
# Purpose: Registers the basic built-in Functions
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2020-02-14 07:37:09 +01:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::FuncBuiltins;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2020-02-14 07:37:09 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2020-02-14 07:37:09 +01:00
|
|
|
|
2021-08-23 22:36:11 +02:00
|
|
|
use PBot::Core::Utils::Indefinite;
|
|
|
|
|
2022-07-08 18:11:56 +02:00
|
|
|
use Lingua::EN::Tagger;
|
2021-08-23 22:36:11 +02:00
|
|
|
use URI::Escape qw/uri_escape_utf8/;
|
|
|
|
|
2020-02-14 07:37:09 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'title',
|
|
|
|
{
|
|
|
|
desc => 'Title-cases text',
|
|
|
|
usage => 'title <text>',
|
|
|
|
subref => sub { $self->func_title(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'ucfirst',
|
|
|
|
{
|
|
|
|
desc => 'Uppercases first character',
|
|
|
|
usage => 'ucfirst <text>',
|
|
|
|
subref => sub { $self->func_ucfirst(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'uc',
|
|
|
|
{
|
|
|
|
desc => 'Uppercases all characters',
|
|
|
|
usage => 'uc <text>',
|
|
|
|
subref => sub { $self->func_uc(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'lc',
|
|
|
|
{
|
|
|
|
desc => 'Lowercases all characters',
|
|
|
|
usage => 'lc <text>',
|
|
|
|
subref => sub { $self->func_lc(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'unquote',
|
|
|
|
{
|
|
|
|
desc => 'removes unescaped surrounding quotes and strips escapes from escaped quotes',
|
|
|
|
usage => 'unquote <text>',
|
|
|
|
subref => sub { $self->func_unquote(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'uri_escape',
|
|
|
|
{
|
|
|
|
desc => 'percent-encode unsafe URI characters',
|
|
|
|
usage => 'uri_escape <text>',
|
|
|
|
subref => sub { $self->func_uri_escape(@_) }
|
|
|
|
}
|
|
|
|
);
|
2021-08-23 22:36:11 +02:00
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'ana',
|
|
|
|
{
|
|
|
|
desc => 'fix-up a/an article at front of text',
|
|
|
|
usage => 'ana <text>',
|
|
|
|
subref => sub { $self->func_ana(@_) }
|
|
|
|
}
|
|
|
|
);
|
2022-07-08 18:11:56 +02:00
|
|
|
$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;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
$self->{pbot}->{functions}->unregister('title');
|
|
|
|
$self->{pbot}->{functions}->unregister('ucfirst');
|
|
|
|
$self->{pbot}->{functions}->unregister('uc');
|
|
|
|
$self->{pbot}->{functions}->unregister('lc');
|
|
|
|
$self->{pbot}->{functions}->unregister('unquote');
|
|
|
|
$self->{pbot}->{functions}->unregister('uri_escape');
|
2021-08-23 22:36:11 +02:00
|
|
|
$self->{pbot}->{functions}->unregister('ana');
|
2022-07-08 18:11:56 +02:00
|
|
|
$self->{pbot}->{functions}->unregister('maybe-the');
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub func_unquote {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
$text =~ s/^"(.*?)(?<!\\)"$/$1/ || $text =~ s/^'(.*?)(?<!\\)'$/$1/;
|
|
|
|
$text =~ s/(?<!\\)\\'/'/g;
|
|
|
|
$text =~ s/(?<!\\)\\"/"/g;
|
|
|
|
return $text;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub func_title {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
$text = ucfirst lc $text;
|
|
|
|
$text =~ s/ (\w)/' ' . uc $1/ge;
|
|
|
|
return $text;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub func_ucfirst {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
2022-07-10 04:09:27 +02:00
|
|
|
|
|
|
|
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
|
|
|
|
|
|
|
|
# don't ucfirst on nicks
|
|
|
|
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return ucfirst $text;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub func_uc {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
return uc $text;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub func_lc {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
return lc $text;
|
2020-02-14 07:37:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:22:00 +01:00
|
|
|
sub func_uri_escape {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
return uri_escape_utf8($text);
|
2020-02-14 08:22:00 +01:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:36:11 +02:00
|
|
|
sub func_ana {
|
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
|
|
|
|
if ($text =~ s/\b(an?)(\s+)//i) {
|
|
|
|
my ($article, $spaces) = ($1, $2);
|
|
|
|
my $fixed_article = select_indefinite_article $text;
|
|
|
|
|
|
|
|
if ($article eq 'AN') {
|
|
|
|
$fixed_article = uc $fixed_article;
|
|
|
|
} elsif ($article eq 'An' or $article eq 'A') {
|
|
|
|
$fixed_article = ucfirst $fixed_article;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $fixed_article . $spaces . $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2022-07-08 18:11:56 +02:00
|
|
|
sub func_maybe_the {
|
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
|
2022-07-10 04:09:27 +02:00
|
|
|
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
|
2022-07-08 18:11:56 +02:00
|
|
|
|
|
|
|
# don't prepend "the" if a proper-noun nick follows
|
|
|
|
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2022-07-14 19:15:30 +02:00
|
|
|
# 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 $text;
|
|
|
|
}
|
|
|
|
|
2022-07-08 18:11:56 +02:00
|
|
|
my $tagged = $self->{tagger}->add_tags($word);
|
|
|
|
|
2022-07-14 19:15:30 +02:00
|
|
|
if ($tagged !~ m/^\s*<(?:det|prps?|cd|in|nnp|to|rb|wdt|rbr|jjr)>/) {
|
2022-07-08 18:11:56 +02:00
|
|
|
$text = "the $text";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2020-02-14 07:37:09 +01:00
|
|
|
1;
|