[Note: This module is strictly in maintenance mode now. Take a look at the newer Lingua::EN::Inflexion module, which offers a cleaner and more convenient interface, has many more features (including plural->singular inflexions), and is also much better tested. If you have existing code that relies on Lingua::EN::Inflect, see the section of the documentation entitled "CONVERTING FROM LINGUA::EN::INFLECT". ]

The exportable subroutines of Lingua::EN::Inflect provide plural inflections, "a"/"an" selection for English words, and manipulation of numbers as words

Plural forms of all nouns, most verbs, and some adjectives are provided. Where appropriate, "classical" variants (for example: "brother" -> "brethren", "dogma" -> "dogmata", etc.) are also provided.

Pronunciation-based "a"/"an" selection is provided for all English words, and most initialisms.

It is also possible to inflect numerals (1,2,3) to ordinals (1st, 2nd, 3rd) and to English words ("one", "two", "three).

In generating these inflections, Lingua::EN::Inflect follows the Oxford English Dictionary and the guidelines in Fowler's Modern English Usage, preferring the former where the two disagree.

The module is built around standard British spelling, but is designed to cope with common American variants as well. Slang, jargon, and other English dialects are not explicitly catered for.

Where two or more inflected forms exist for a single word (typically a "classical" form and a "modern" form), Lingua::EN::Inflect prefers the more common form (typically the "modern" one), unless "classical" processing has been specified (see "MODERN VS CLASSICAL INFLECTIONS").

All of the PL_... plural inflection subroutines take the word to be inflected as their first argument and return the corresponding inflection. Note that all such subroutines expect the singular form of the word. The results of passing a plural form are undefined (and unlikely to be correct).

The PL_... subroutines also take an optional second argument, which indicates the grammatical "number" of the word (or of another word with which the word being inflected must agree). If the "number" argument is supplied and is not 1 (or "one" or "a", or some other adjective that implies the singular), the plural form of the word is returned. If the "number" argument does indicate singularity, the (uninflected) word itself is returned. If the number argument is omitted, the plural form is returned unconditionally.

The exportable subroutine PL_N() takes a singular English noun or pronoun and returns its plural. Pronouns in the nominative ("I" -> "we") and accusative ("me" -> "us") cases are handled, as are possessive pronouns ("mine" -> "ours").

The exportable subroutine PL() takes a singular English noun, pronoun, verb, or adjective and returns its plural form. Where a word has more than one inflection depending on its part of speech (for example, the noun "thought" inflects to "thoughts", the verb "thought" to "thought"), the (singular) noun sense is preferred to the (singular) verb sense.

Hence PL("knife") will return "knives" ("knife" having been treated as a singular noun), whereas PL("knifes") will return "knife" ("knifes" having been treated as a 3rd person singular verb).

The inherent ambiguity of such cases suggests that, where the part of speech is known, PL_N, PL_V, and PL_ADJ should be used in preference to PL.

Note that all these subroutines ignore any whitespace surrounding the word being inflected, but preserve that whitespace when the result is returned. For example, PL(" cat ") returns " cats ".

The PL_... subroutines return only the inflected word, not the count that was used to inflect it. Thus, in order to produce "I saw 3 ducks", it is necessary to use:

print "I saw $N ", PL_N($animal,$N), "\n";

Since the usual purpose of producing a plural is to make it agree with a preceding count, Lingua::EN::Inflect provides an exportable subroutine (NO($;$)) which, given a word and a(n optional) count, returns the count followed by the correctly inflected word. Hence the previous example can be rewritten:

print "I saw ", NO($animal,$N), "\n";

In addition, if the count is zero (or some other term which implies zero, such as "zero", "nil", etc.) the count is replaced by the word "no". Hence, if $N had the value zero, the previous example would print the somewhat more elegant:

I saw no animals

rather than:

I saw 0 animals

Note that the name of the subroutine is a pun: the subroutine returns either a number (a No.) or a "no", in front of the inflected word.

Lingua::EN::Inflect therefore provides an exportable subroutine (NUM($;$)) that may be used to set a persistent "default number" value. If such a value is set, it is subsequently used whenever an optional second "number" argument is omitted. The default value thus set can subsequently be removed by calling NUM() with no arguments. Hence we could rewrite the previous example:

However, in certain contexts (see "INTERPOLATING INFLECTIONS IN STRINGS") it is preferable that NUM() return an empty string. Hence NUM() provides an optional second argument. If that argument is supplied (that is, if it is defined) and evaluates to false, NUM returns an empty string instead of its first argument. For example:

Lingua::EN::Inflect also provides a solution to the problem of comparing words of differing plurality through the exportable subroutines PL_eq($$), PL_N_eq($$), PL_V_eq($$), and PL_ADJ_eq($$). Each of these subroutines takes two strings, and compares them using the corresponding plural-inflection subroutine (PL(), PL_N(), PL_V(), and PL_ADJ() respectively).

As indicated by the comments in the previous example, the actual value returned by the various PL_eq subroutines encodes which of the three equality rules succeeded: "eq" is returned if the strings were identical, "s:p" if the strings were singular and plural respectively, "p:s" for plural and singular, and "p:p" for two distinct plurals. Inequality is indicated by returning an empty string.

It should be noted that two distinct singular words which happen to take the same plural form are not considered equal, nor are cases where one (singular) word's plural is the other (plural) word's singular. Hence all of the following return false:

Note too that, although the comparison is "number-insensitive" it is not case-insensitive (that is, PL("time","Times") returns false. To obtain both number and case insensitivity, prefix both arguments with lc (that is, PL(lc "time", lc "Times") returns true).

A() and AN() both take an optional second argument. As with the PL_... subroutines, this second argument is a "number" specifier. If its value is 1 (or some other value implying singularity), A() and AN() insert "a" or "an" as appropriate. If the number specifier implies plurality, (A() and AN() insert the actual second argument instead. For example:

Note that, as implied by the previous examples, A() and AN() both assume that their job is merely to provide the correct qualifier for a word (that is: "a", "an", or the specified count). In other words, they assume that the word they are given has already been correctly inflected for plurality. Hence, if $N has the value 2, then:

print A("cat",$N);

prints "2 cat", instead of "2 cats". The correct approach is to use:

print A(PL("cat",$N),$N);

or, better still:

print NO("cat",$N);

Note too that, like the various PL_... subroutines, whenever A() and AN() are called with only one argument they are subject to the effects of any preceding call to NUM(). Hence, another possible solution is:

"Initialisms" (sometimes inaccurately called "acronyms") are terms which have been formed from the initial letters of words in a phrase (for example, "NATO", "NBL", "S.O.S.", "SCUBA", etc.)

Such terms present a particular challenge when selecting between "a" and "an", since they are sometimes pronounced as if they were a single word ("nay-tow", "sku-ba") and sometimes as a series of letter names ("en-eff-ell", "ess-oh-ess").

A() and AN() cope with this dichotomy using a series of inbuilt rules, which may be summarized as:

If the word starts with a single letter, followed by a period or dash (for example, "R.I.P.", "C.O.D.", "e-mail", "X-ray", "T-square"), then choose the appropriate article for the sound of the first letter ("an R.I.P.", "a C.O.D.", "an e-mail", "an X-ray", "a T-square").

If the first two letters of the word are capitals, consonants, and do not appear at the start of any known English word, (for example, "LCD", "XML", "YWCA"), then once again choose "a" or "an" depending on the sound of the first letter ("an LCD", "an XML", "a YWCA").

Words beginning in the letter 'H' present another type of difficulty when selecting a suitable indefinite article. In a few such words (for example, "hour", "honour", "heir") the 'H' is not voiced at all, and so such words inflect with "an". The remaining cases ("voiced H's") may be divided into two categories: "hard H's" (such as "hangman", "holograph", "hat", etc.) and "soft H's" (such as "hysterical", "horrendous", "holy", etc.)

Hard H's always take "a" as their indefinite article, and soft H's normally do so as well. But some English speakers prefer "an" for soft H's (although the practice is now generally considered an affectation, rather than a legitimate grammatical alternative).

At present, the A() and AN() subroutines ignore soft H's and use "a" for any voiced 'H'. The author would, however, welcome feedback on this decision (envisaging a possible future "soft H" mode).

That last case is a little awkward since it's almost certainly a phone number, and "five million, five hundred and fifty-one thousand, two hundred and two" probably isn't what's wanted.

To overcome this, NUMWORDS() takes an optional named argument, 'group', which changes how numbers are translated. The argument must be a positive integer less than four, which indicated how the digits of the number are to be grouped. If the argument is 1, then each digit is translated separately. If the argument is 2, pairs of digits (starting from the left) are grouped together. If the argument is 3, triples of numbers (again, from the left) are grouped. Hence:

NUMWORDS("555-1202", group=>1)

returns "five, five, five, one, two, zero, two", whilst:

NUMWORDS("555-1202", group=>2)

returns "fifty-five, fifty-one, twenty, two", and:

NUMWORDS("555-1202", group=>3)

returns "five fifty-five, one twenty, two".

Phone numbers are often written in words as "five..five..five..one..two..zero..two", which is also easy to achieve:

join '..', NUMWORDS("555-1202", group=>1)

NUMWORDS also handles decimal fractions. Hence:

NUMWORDS("1.2345")

returns "one point two three four five" in a scalar context and ("one","point","two","three","four","five")) in an array context. Exponent form ("1.234e56") is not yet handled.

Multiple decimal points are only translated in one of the "grouping" modes. Hence:

NUMWORDS(101.202.303)

returns "one hundred and one point two zero two three zero three", whereas:

NUMWORDS(101.202.303, group=>1)

returns "one zero one point two zero two point three zero three".

The digit '0' is unusual in that in may be translated to English as "zero", "oh", or "nought". To cater for this diversity, NUMWORDS may be passed a named argument, 'zero', which may be set to the desired translation of '0'. For example:

print join "..", NUMWORDS("555-1202", group=>3, zero=>'oh')

prints "five..five..five..one..two..oh..two". By default, zero is rendered as "zero".

Likewise, the digit '1' may be rendered as "one" or "a/an" (or very occasionally other variants), depending on the context. So there is a 'one' argument as well:

print NUMWORDS($_, one=>'a solitary', zero=>'no more'),
PL(" bottle of beer on the wall\n", $_)
for (3,2,1,0);
# prints:
# three bottles of beer on the wall
# two bottles of beer on the wall
# a solitary bottle of beer on the wall
# no more bottles of beer on the wall

Care is needed if the word "a/an" is to be used as a 'one' value. Unless the next word is known in advance, it's almost always necessary to use the A function as well:

Another major regional variation in number translation is the use of "and" in certain contexts. The named argument 'and' allows the programmer to specify how "and" should be handled. Hence:

print scalar NUMWORDS("765", 'and'=>'')

prints "seven hundred sixty-five", instead of "seven hundred and sixty-five". By default, the "and" is included.

The translation of the decimal point is also subject to variation (with "point", "dot", and "decimal" being the favorites). The named argument 'decimal' allows the programmer to how the decimal point should be rendered. Hence:

print scalar NUMWORDS("666.124.64.101", group=>3, decimal=>'dot')

prints "six sixty-six, dot, one twenty-four, dot, sixty-four, dot, one zero one" By default, the decimal point is rendered as "point".

When creating a list of words, commas are used between adjacent items, except if the items contain commas, in which case semicolons are used. But if there are less than two items, the commas/semicolons are omitted entirely. The final item also has a conjunction (usually "and" or "or") before it. And although it's technically incorrect (and sometimes misleading), some people prefer to omit the comma before that final conjunction, even when there are more than two items.

That's complicated enough to warrant its own subroutine: WORDLIST(). This subroutine expects a list of words, possibly with one or more hash references containing options. It returns a string that joins the list together in the normal English usage. For example:

Unfortunately the need to separate each subroutine call detracts significantly from the readability of the resulting code. To ameliorate this problem, Lingua::EN::Inflect provides an exportable string-interpolating subroutine (inflect($)), which recognizes calls to the various inflection subroutines within a string and interpolates them appropriately.

Note that inflect also correctly handles calls to the NUM() subroutine (whether interpolated or antecedent). The inflect() subroutine has a related extra feature, in that it automatically cancels any "default number" value before it returns its interpolated string. This means that calls to NUM() which are embedded in an inflect()-interpolated string do not "escape" and interfere with subsequent inflections.

Lingua::EN::Inflect caters to such words by providing an "alternate state" of inflection known as "classical mode". By default, words are inflected using their contemporary English plurals, but if classical mode is invoked, the more traditional plural forms are returned instead.

The exportable subroutine classical() controls this feature. If classical() is called with no arguments, it unconditionally invokes classical mode. If it is called with a single argument, it turns all classical inflects on or off (depending on whether the argument is true or false). If called with two or more arguments, those arguments specify which aspects of classical behaviour are to be used.

LEI assumes that a capitalized word is a person's name. So it forms the plural according to the rules for names (which is that you don't inflect, you just add -s or -es). You can choose to turn that behaviour off (it's on by the default, even when the module isn't in classical mode) by calling classical(names=>0).

The def_noun subroutine takes a pair of string arguments: the singular and plural forms of the noun being specified. The singular form specifies a pattern to be interpolated (as m/^(?:$first_arg)$/i). Any noun matching this pattern is then replaced by the string in the second argument. The second argument specifies a string which is interpolated after the match succeeds, and is then used as the plural form. For example:

Note that both arguments should usually be specified in single quotes, so that they are not interpolated when they are specified, but later (when words are compared to them). As indicated by the last example, care also needs to be taken with certain characters in the second argument, to ensure that they are not unintentionally interpolated during comparison.

The second argument string may also specify a second variant of the plural form, to be used when "classical" plurals have been requested. The beginning of the second variant is marked by a '|' character:

The def_verb subroutine takes three pairs of string arguments (that is, six arguments in total), specifying the singular and plural forms of the three "persons" of verb. As with def_noun, the singular forms are specifications of run-time-interpolated patterns, whilst the plural forms are specifications of (up to two) run-time-interpolated strings:

The def_adj subroutine takes a pair of string arguments, which specify the singular and plural forms of the adjective being defined. As with def_noun and def_adj, the singular forms are specifications of run-time-interpolated patterns, whilst the plural forms are specifications of (up to two) run-time-interpolated strings:

The def_a and def_an subroutines each take a single argument, which specifies a pattern. If a word passed to A() or AN() matches this pattern, it will be prefixed (unconditionally) with the corresponding indefinite article. For example:

def_a 'error';
def_a 'in.+';
def_an 'mistake';
def_an 'error';

As with the other def_... subroutines, such redefinitions are sequential in effect so that, after the above example, "error" will be inflected with "an".

When it is imported, Lingua::EN::Inflect executes (as Perl code) the contents of any file named .inflectrc which it finds in the in the directory where Lingua/EN/Inflect.pm is installed, or in the current home directory ($ENV{HOME}), or in both. Note that the code is executed within the Lingua::EN::Inflect namespace.

Hence the user or the local Perl guru can make appropriate calls to def_noun, def_verb, etc. in one of these .inflectrc files, to permanently and universally modify the behaviour of the module. For example

Note that calls to the def_... subroutines from within a program will take precedence over the contents of the home directory .inflectrc file, which in turn takes precedence over the system-wide .inflectrc file.

On loading, if the Perl code in a .inflectrc file is invalid (syntactically or otherwise), an appropriate fatal error is issued. A common problem is not ending the file with something that evaluates to true (as the five def_... subroutines do).

Using the five def_... subroutines directly in a program may also result in fatal diagnostics, if a (singular) pattern or an interpolated (plural) string is somehow invalid.

The singular form of a user-defined noun or verb (as defined by a call to def_noun, def_verb, def_adj, def_a or def_an) is not a valid Perl regular expression. The actual Perl error message is also given.

The plural form(s) of a user-defined noun or verb (as defined by a call to def_noun, def_verb or def_adj) is not a valid Perl interpolated string (usually because it interpolates some undefined variable).

Some other problem occurred in loading the named local or global .inflectrc file. The Perl error message (including the line number) is also given.

There are no diagnosable run-time error conditions for the actual inflection subroutines, except NUMWORDS and hence no run-time diagnostics. If the inflection subroutines are unable to form a plural via a user-definition or an inbuilt rule, they just "guess" the commonest English inflection: adding "-s" for nouns, removing "-s" for verbs, and no inflection for adjectives.

NUMWORDS() was passed a number larger than the number represented by 3006 consecutive nines. The words representing that number are 63,681 characters long, including commas and spaces. If you're interested in the actual value, see t/numwords.t.

are complex and often depend more on intent rather than content. Hence it is infeasible to specify such rules algorithmically.

Therefore, Lingua::EN::Text contents itself with the following compromise: If the governing number is zero, inflections always return the plural form unless the appropriate "classical" inflection is in effect, in which case the singular form is always returned.

Another context in which intent (and not content) sometimes determines plurality is where two distinct meanings of a word require different plurals. For example:

Three basses were stolen from the band's equipment trailer.
Three bass were stolen from the band's aquarium.
I put the mice next to the cheese.
I put the mouses next to the computers.
Several thoughts about leaving crossed my mind.
Several thought about leaving across my lawn.

Lingua::EN::Inflect handles such words in two ways:

If both meanings of the word are the same part of speech (for example, "bass" is a noun in both sentences above), then one meaning is chosen as the "usual" meaning, and only that meaning's plural is ever returned by any of the inflection subroutines.

If each meaning of the word is a different part of speech (for example, "thought" is both a noun and a verb), then the noun's plural is returned by PL() and PL_N() and the verb's plural is returned only by PL_V().

Such contexts are, fortunately, uncommon (particularly "same-part-of-speech" examples). An informal study of nearly 600 "difficult plurals" indicates that PL() can be relied upon to "get it right" about 98% of the time (although, of course, ichthyophilic guitarists or cyber-behaviouralists may experience higher rates of confusion).

If the choice of a particular "usual inflection" is considered inappropriate, it can always be reversed with a preliminary call to the corresponding def_... subroutine.

Despite the populist pandering of certain New World dictionaries, the plural is "octopuses" or (for the pedantic classicist) "octopodes". The suffix "-pus" is Greek, not Latin, so the plural is "-podes", not "pi".