Make English language plurals easily with Perl

| 1 Comment

You can probably figure out easily how to make a plural of a word in the English language, and when to put a or an as the indefinite article. However, it's not that easy when you need to do that dinamically on words provided by an user or coming from some other source. Also, you have no elegant way to automatically print something like this:

Your ranch contains no cows.
Your ranch contains 1 cow.
Your ranch contains 4 cows.

with a single print statement. Lingua::EN::Inflect provides all these features, so it allows to write proper English plurals even when you don't know the words you have to inflect. Just say:

use Lingua::EN::Inflect qw/:ALL/;

for my $n(0, 1, 4) {
    print 'Your ranch contains ' . NO( 'cow', $n ) . ".\n";
}

The output of this code is exactly the text shown above. Even though this is a very useful feature, Lingua::EN::Inflect offers many more utilities to work with plurals in the English language. Keep on reading to discover the gory details.

Let's analyze the formation of the plurals. The module uses some rules to generate them properly. For instance:

print PL('mirror', 5) . "\n";
print PL('watch', 5) . "\n";

correctly displays:

mirrors
watches

thus adding 's' or 'es' depending on the structure of the singular word. It's not all about nouns, as the moule also supports the plural for verbs and adjectives. i.e.:

for my $n(1, 14) {
    print PL_ADJ('This', $n) . ' ' . PL_N('cow', $n) . ' ' . PL_V ('walks', $n) . "\n";
}

prints:

This cow walks
These cows walk

So, you just pass to the functions the demonstrative adjective, the noun and the verb in their singular form (the verb has got be conjugated in the desired tense and person) to get the correct inflections. Numerical adjectives (a => some) are also handled, and so are pronouns:

for my $n(1, 14) {
    print PL_ADJ('a', $n) . ' ' . PL_N('piece', $n) . ' of ' . PL_N ('me', $n) . "\n";
}

magically outputs this:

a piece of me
some pieces of us

Is this module beginning to seem very useful to you? Well, it is. Maybe you're the "nostalgic" folk who likes the old-style plurals some words offer (I do). Since these ancient plurals are perfectly correct even in current English, you can get them:

print PL_N('cow', 5) . "\n";
print PL_N('octopus', 5) . "\n";
print PL_N('person', 5) . "\n";

classical();
print PL_N('cow', 5) . "\n";
print PL_N('octopus', 5) . "\n";
print PL_N('person', 5) . "\n";

Have a look at the poetic output:

cows
octopuses
people
kine
octopodes
persons

As you see, Lingua::EN::Inflect solves almost every problem you might encounter while creating plurals for English words you don't know when writing you Perl code. Also, it helps you with plurals of pronouns and adjectives, so that you're not forced to write a lot of if statements to get things done properly. The situations you can manage using this module are really countless:

# Suffix for ordinal numbers
# automatically convers 1 => 1st, 2 => 2nd, ...
for my $n(1, 2, 11) {
    print ORD($n) . " cow\n";
}

# Convert a number in letters
# Prints "twelve thousand, three hundred and fifty-one"
print NUMWORDS(12351) . "\n";

So, this is another of the many ways in which Perl can save you a lot of coding time. Lingua::EN::Inflect provides also some other features (take a look at the module documentation), and it is also easily extensible with your rules and exceptions (the author knows the module might not be perfect and probably does not cover everything).

Happy inflecting!

1 Comment

I wonder: does it handle the really oldish dual as in oxen/regexen ?

I guess no, but since I discovered the existence of this through a larry wall's speech, I thought I'd ask anyway :)

Leave a comment

About this Entry

This page contains a single entry by Michele Beltrame published on June 28, 2006 12:21 AM.

FIFA World Cup 2006 was the previous entry in this blog.

Rise and fall of the Hungarian notation is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.14-en