3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-28 05:32:48 +01:00
pbot/lib/PBot/Plugin/FuncPlural.pm

107 lines
2.3 KiB
Perl
Raw Normal View History

2020-07-20 01:39:03 +02:00
# File: FuncPlural.pm
#
2021-06-19 06:23:34 +02:00
# Purpose: Registers the plural Function.
2020-07-20 01:39:03 +02:00
2021-07-11 00:00:22 +02:00
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
2020-07-20 01:39:03 +02:00
2021-07-14 04:45:56 +02:00
package PBot::Plugin::FuncPlural;
use parent 'PBot::Plugin::Base';
2020-07-20 01:39:03 +02:00
2021-06-19 06:23:34 +02:00
use PBot::Imports;
2020-07-20 01:39:03 +02:00
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{functions}->register(
'plural',
{
desc => 'pluralizes a word or phrase',
usage => 'plural <word>',
subref => sub { $self->func_plural(@_) }
}
);
}
sub unload {
my $self = shift;
$self->{pbot}->{functions}->unregister('plural');
}
sub pluralize_word {
my ($word) = @_;
my @ignore = qw/trout fish tobacco music snow armor police dice caribou moose people sheep/;
my %endings = (
'man' => 'men',
'ch' => 'ches',
'sh' => 'shes',
'eer' => 'eer',
'mouse' => 'mice',
'goose' => 'geese',
'hoof' => 'hooves',
'wolf' => 'wolves',
'foot' => 'feet',
'ss' => 'sses',
'is' => 'ises',
'ife' => 'ives',
'us' => 'uses',
'x' => 'xes',
'ium' => 'ia',
2021-06-19 06:23:34 +02:00
'um' => 'a',
'stomach' => 'stomachs',
'cactus' => 'cacti',
'cactus' => 'cacti',
'knoif' => 'knoives',
'sheaf' => 'sheaves',
'dwarf' => 'dwarves',
2020-07-20 01:39:03 +02:00
'loaf' => 'loaves',
'louse' => 'lice',
'die' => 'dice',
'calf' => 'calves',
'self' => 'selves',
'tooth' => 'teeth',
'alumnus' => 'alumni',
'leaf' => 'leaves',
'ay' => 'ays',
'ey' => 'eys',
'child' => 'children',
'o' => 'oes',
);
$word =~ s/^an? //;
$word =~ s/\bthat\b/those/g; $word =~ s/\bthis\b/these/g;
return $word if grep { /^\Q$word\E$/i } @ignore;
foreach my $ending (sort { length $b <=> length $a } keys %endings) {
return $word if $word =~ s/$ending$/$endings{$ending}/;
}
$word =~ s/s$/s/ || $word =~ s/y$/ies/ || $word =~ s/$/s/;
return $word;
}
sub pluralize {
my ($string) = @_;
if ($string =~ m/(.*?) (containing|packed with|with what appears to be) (.*)/) {
my $word = pluralize_word $1;
return "$word $2 $3";
} elsif ($string =~ m/(.*?) of (.*)/) {
my $word = pluralize_word $1;
return "$word of $2";
} else {
return pluralize_word $string;
}
}
sub func_plural {
my $self = shift @_;
my $text = "@_";
return pluralize($text);
}
1;