-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFuncBuiltins.pm
288 lines (241 loc) · 7.65 KB
/
FuncBuiltins.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File: FuncBuiltins.pm
#
# Purpose: Registers the basic built-in Functions
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <[email protected]>
# SPDX-License-Identifier: MIT
package PBot::Plugin::FuncBuiltins;
use parent 'PBot::Plugin::Base';
use PBot::Imports;
use PBot::Core::Utils::Indefinite;
use Lingua::EN::Tagger;
use URI::Escape qw/uri_escape_utf8/;
sub initialize($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(
'shquote',
{
desc => 'quotes text for sh invocation',
usage => 'shquote <text>',
subref => sub { $self->func_shquote(@_) }
}
);
$self->{pbot}->{functions}->register(
'quotemeta',
{
desc => 'escapes/quotes metacharacters',
usage => 'quotemeta <text>',
subref => sub { $self->func_quotemeta(@_) }
}
);
$self->{pbot}->{functions}->register(
'uri_escape',
{
desc => 'percent-encode unsafe URI characters',
usage => 'uri_escape <text>',
subref => sub { $self->func_uri_escape(@_) }
}
);
$self->{pbot}->{functions}->register(
'ana',
{
desc => 'fix-up a/an article at front of text',
usage => 'ana <text>',
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->{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->{pbot}->{functions}->register(
'maybe-on',
{
desc => 'prepend "on" 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_on(@_) }
}
);
$self->{tagger} = Lingua::EN::Tagger->new;
}
sub unload($self) {
$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('shquote');
$self->{pbot}->{functions}->unregister('quotemeta');
$self->{pbot}->{functions}->unregister('uri_escape');
$self->{pbot}->{functions}->unregister('ana');
$self->{pbot}->{functions}->unregister('maybe-the');
$self->{pbot}->{functions}->unregister('maybe-to');
$self->{pbot}->{functions}->unregister('maybe-on');
}
sub func_unquote($self, @rest) {
my $text = "@rest";
$text =~ s/^"(.*?)(?<!\\)"$/$1/ || $text =~ s/^'(.*?)(?<!\\)'$/$1/;
$text =~ s/(?<!\\)\\'/'/g;
$text =~ s/(?<!\\)\\"/"/g;
return $text;
}
sub func_title($self, @rest) {
my $text = "@rest";
$text = ucfirst lc $text;
$text =~ s/ (\w)/' ' . uc $1/ge;
return $text;
}
sub func_ucfirst($self, @rest) {
my $text = "@rest";
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
# don't ucfirst on nicks
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return $text;
}
return ucfirst $text;
}
sub func_uc($self, @rest) {
my $text = "@rest";
return uc $text;
}
sub func_lc($self, @rest) {
my $text = "@rest";
return lc $text;
}
sub func_shquote($self, @rest) {
my $text = "@rest";
$text =~ s/'/'"'"'/g;
return "'$text'";
}
sub func_quotemeta($self, @rest) {
my $text = "@rest";
return quotemeta $text;
}
sub func_uri_escape($self, @rest) {
my $text = "@rest";
return uri_escape_utf8($text);
}
sub func_ana($self, @rest) {
my $text = "@rest";
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;
}
sub func_maybe_the($self, @rest) {
my $text = "@rest";
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
# don't prepend if a proper-noun nick follows
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return $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 $text;
}
my $tagged = $self->{tagger}->add_tags($word);
if ($tagged !~ m/^\s*<(?:det|prps?|cd|in|nnp|to|rb|wdt|rbr|jjr)>/) {
$text = "the $text";
}
return $text;
}
sub func_maybe_to($self, @rest) {
my $text = "@rest";
$text =~ s/^to (?:the )?//;
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
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 {
unless ($tagged =~ m/^\s*<(?:in)>/) {
$text = "to $text";
}
}
return $text;
}
sub func_maybe_on($self, @rest) {
my $text = "@rest";
$text =~ s/^on (?:the )?//;
my ($word) = $text =~ m/^\s*([^',.;: ]+)/;
if ($self->{pbot}->{nicklist}->is_present_any_channel($word)) {
return "on $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 "on $text";
}
my $tagged = $self->{tagger}->add_tags($word);
if ($tagged !~ m/^\s*<(?:det|prps?|cd|in|nnp|to|rb|wdt|rbr|jjr)>/) {
$text = "on the $text";
} else {
unless ($tagged =~ m/^\s*<(?:in)>/) {
$text = "on $text";
}
}
return $text;
}
1;