| 1 | package Lingua::PT::Inflect; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Lingua::PT::Hyphenate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | require Exporter; | |||||||||
| 9 | |||||||||||
| 10 | 3 | our @ISA = qw(Exporter); | |||||||||
| 11 | |||||||||||
| 12 | 3 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 13 | sing2plural | ||||||||||
| 14 | ) ] ); | ||||||||||
| 15 | |||||||||||
| 16 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 17 | |||||||||||
| 18 | 3 | our @EXPORT = qw( | |||||||||
| 19 | sing2plural | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 3 | our $VERSION = '0.08'; | |||||||||
| 23 | |||||||||||
| 24 | =head1 NAME | ||||||||||
| 25 | |||||||||||
| 26 | =encoding latin1 | ||||||||||
| 27 | |||||||||||
| 28 | Lingua::PT::Inflect - Converts Portuguese words from singular to plural | ||||||||||
| 29 | |||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||
| 31 | |||||||||||
| 32 | use Lingua::PT::Inflect; | ||||||||||
| 33 | |||||||||||
| 34 | $plural = sing2plural('programador') # now holds 'programadores' | ||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | Converts Portuguese words from singular to plural. There may be some | ||||||||||
| 39 | special cases that will fail (words ending in -ão or -s might fail, as | ||||||||||
| 40 | many special cases are yet to be prevented; anyone volunteering to | ||||||||||
| 41 | look at a huge list of words?) | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 3 | my (%exceptions,@rules,%rules); | |||||||||
| 46 | |||||||||||
| 47 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 3 | %exceptions = ( | |||||||||
| 49 | 'lápis' => 'lápis', | ||||||||||
| 50 | 'pires' => 'pires', | ||||||||||
| 51 | |||||||||||
| 52 | 'mão' => 'mãos', | ||||||||||
| 53 | 'afegão' => 'afegãos', | ||||||||||
| 54 | |||||||||||
| 55 | 'pão' => 'pães', | ||||||||||
| 56 | 'capitão' => 'capitães', | ||||||||||
| 57 | 'cão' => 'cães', | ||||||||||
| 58 | 'alemão' => 'alemães', | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 3 | @rules = map qr/$_/, qw(ás ês el ol al oi ul m ão (?<=[aeiou]) (?<=[rnsz])); | |||||||||
| 62 | |||||||||||
| 63 | 3 | %rules = ( | |||||||||
| 64 | qr/ás/ => 'ases', | ||||||||||
| 65 | qr/ês/ => 'eses', | ||||||||||
| 66 | qr/el/ => 'éis', | ||||||||||
| 67 | qr/ol/ => 'óis', | ||||||||||
| 68 | qr/al/ => 'ais', | ||||||||||
| 69 | qr/oi/ => 'ois', | ||||||||||
| 70 | qr/ul/ => 'uis', | ||||||||||
| 71 | qr/m/ => 'ns', | ||||||||||
| 72 | qr/ão/ => 'ões', | ||||||||||
| 73 | qr/(?<=[aeiou])/ => 's', | ||||||||||
| 74 | qr/(?<=[rnsz])/ => 'es', | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # | ||||||||||
| 80 | |||||||||||
| 81 | =head1 METHODS | ||||||||||
| 82 | |||||||||||
| 83 | =head2 new | ||||||||||
| 84 | |||||||||||
| 85 | Creates a new C<Lingua::PT::Inflect> object. | ||||||||||
| 86 | |||||||||||
| 87 | If you're doing this lots of time, it would probably be better for you | ||||||||||
| 88 | to use the C<sing2plural> function directly (that is, creating a new | ||||||||||
| 89 | object for each word in a long text doesn't seem so bright if you're | ||||||||||
| 90 | not going to use it later on). | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | 27 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 27 | my ($self, $word) = @_; | |||||||||
| 96 | 27 | bless \$word, $self; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =head2 sing2plural | ||||||||||
| 100 | |||||||||||
| 101 | Converts a word in the singular gender to plural. | ||||||||||
| 102 | |||||||||||
| 103 | $plural = sing2plural($singular); | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 54 | ▶ | sub sing2plural { | ||||||||
|
Subroutine
sing2plural: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 108 | 54 | ▶ | defined $_[0] || return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 54 | my $word; | |||||||||
| 111 | 54 | ▶ | if (ref($_[0]) eq 'Lingua::PT::Inflect') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 27 | my $self = shift; | |||||||||
| 113 | 27 | $word = $$self; | |||||||||
| 114 | } | ||||||||||
| 115 | else { | ||||||||||
| 116 | 27 | $word = shift; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 54 | $_ = $word; | |||||||||
| 120 | |||||||||||
| 121 | 54 | ▶ | defined $exceptions{$_} && return $exceptions{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | 46 | for my $rule (@rules) { | |||||||||
| 124 | 406 | ▶ | if (s/$rule$/$rules{$rule}/) {return $_} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 14 | ▶ | if (/il$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 8 | my @syl = hyphenate($_); | |||||||||
| 129 | |||||||||||
| 130 | 8 | ▶ | s!il$!$syl[-2] =~ /[ãâáêéíóõôúÃÁÉÍÓÕÔÊÂÚ]/ ? 'eis' : 'is' !e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 14 | return $_; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 3 | 1; | |||||||||
| 137 | __END__ | ||||||||||