Convert singular to plural. select "a" or "an".
This document describes version 1.895 of Lingua::EN::Inflect
use Lingua::EN::Inflect qw ( PL PL_N PL_V PL_ADJ NO NUM PL_eq PL_N_eq PL_V_eq PL_ADJ_eq A AN PART_PRES ORD NUMWORDS WORDLIST inflect classical def_noun def_verb def_adj def_a def_an ); # UNCONDITIONALLY FORM THE PLURAL print "The plural of ", $word, " is ", PL($word), "\n"; # CONDITIONALLY FORM THE PLURAL print "I saw $cat_count ", PL("cat",$cat_count), "\n"; # FORM PLURALS FOR SPECIFIC PARTS OF SPEECH print PL_N("I",$N1), PL_V("saw",$N1), PL_ADJ("my",$N2), PL_N("saw",$N2), "\n"; # DEAL WITH "0/1/N" -> "no/1/N" TRANSLATION: print "There ", PL_V("was",$errors), NO(" error",$errors), "\n"; # USE DEFAULT COUNTS: print NUM($N1,""), PL("I"), PL_V(" saw"), NUM($N2), PL_N(" saw"); print "There ", NUM($errors,''), PL_V("was"), NO(" error"), "\n"; # COMPARE TWO WORDS "NUMBER-INSENSITIVELY": print "same\n" if PL_eq($word1, $word2); print "same noun\n" if PL_N_eq($word1, $word2); print "same verb\n" if PL_V_eq($word1, $word2); print "same adj.\n" if PL_ADJ_eq($word1, $word2); # ADD CORRECT "a" OR "an" FOR A GIVEN WORD: print "Did you want ", A($thing), " or ", AN($idea), "\n"; # CONVERT NUMERALS INTO ORDINALS (i.e. 1->1st, 2->2nd, 3->3rd, etc.) print "It was", ORD($position), " from the left\n"; # CONVERT NUMERALS TO WORDS (i.e. 1->"one", 101->"one hundred and one", etc.) # IN A SCALAR CONTEXT: GET BACK A SINGLE STRING... $words = NUMWORDS(1234); # "one thousand, two hundred and thirty-four" $words = NUMWORDS(ORD(1234)); # "one thousand, two hundred and thirty-fourth" # IN A LIST CONTEXT: GET BACK A LIST OF STRINGSi, ONE FOR EACH "CHUNK"... @words = NUMWORDS(1234); # ("one thousand","two hundred and thirty-four") # OPTIONAL PARAMETERS CHANGE TRANSLATION: $words = NUMWORDS(12345, group=>1); # "one, two, three, four, five" $words = NUMWORDS(12345, group=>2); # "twelve, thirty-four, five" $words = NUMWORDS(12345, group=>3); # "one twenty-three, forty-five" $words = NUMWORDS(1234, 'and'=>''); # "one thousand, two hundred thirty-four" $words = NUMWORDS(1234, 'and'=>', plus'); # "one thousand, two hundred, plus thirty-four" $words = NUMWORDS(555_1202, group=>1, zero=>'oh'); # "five, five, five, one, two, oh, two" $words = NUMWORDS(555_1202, group=>1, one=>'unity'); # "five, five, five, unity, two, oh, two" $words = NUMWORDS(123.456, group=>1, decimal=>'mark'); # "one two three mark four five six"
# \s-1LITERAL\s0 \s-1STYLE\s0 \s-1ONLY\s0 \s-1NAMES\s0 \s-1NUMBERS\s0 \s-1LESS\s0 \s-1THAN\s0 A \s-1CERTAIN\s0 \s-1THRESHOLD\s0...
$words = NUMWORDS( 9, threshold=>10); # "nine" $words = NUMWORDS( 10, threshold=>10); # "ten" $words = NUMWORDS( 11, threshold=>10); # "11" $words = NUMWORDS(1000, threshold=>10); # "1,000"
# JOIN WORDS INTO A LIST:
$list = WORDLIST("apple", "banana", "carrot"); # "apple, banana, and carrot"
$list = WORDLIST("apple", "banana"); # "apple and banana"
$list = WORDLIST("apple", "banana", "carrot", {final_sep=>""}); # "apple, banana and carrot"
# REQUIRE "CLASSICAL" PLURALS (EG: "focus"->"foci", "cherub"->"cherubim")
classical; # USE ALL CLASSICAL PLURALS
classical 1; # USE ALL CLASSICAL PLURALS classical 0; # USE ALL MODERN PLURALS (DEFAULT)
classical 'zero'; # "no error" INSTEAD OF "no errors" classical zero=>1; # "no error" INSTEAD OF "no errors" classical zero=>0; # "no errors" INSTEAD OF "no error"
classical 'herd'; # "2 buffalo" INSTEAD OF "2 buffalos" classical herd=>1; # "2 buffalo" INSTEAD OF "2 buffalos" classical herd=>0; # "2 buffalos" INSTEAD OF "2 buffalo"
classical 'persons'; # "2 chairpersons" INSTEAD OF "2 chairpeople" classical persons=>1; # "2 chairpersons" INSTEAD OF "2 chairpeople" classical persons=>0; # "2 chairpeople" INSTEAD OF "2 chairpersons"
classical 'ancient'; # "2 formulae" INSTEAD OF "2 formulas" classical ancient=>1; # "2 formulae" INSTEAD OF "2 formulas" classical ancient=>0; # "2 formulas" INSTEAD OF "2 formulae"
# INTERPOLATE "PL()", "PL_N()", "PL_V()", "PL_ADJ()", A()", "AN()" # "NUM()" AND "ORD()" WITHIN STRINGS:
print inflect("The plural of $word is PL($word)\n"); print inflect("I saw $cat_count PL("cat",$cat_count)\n"); print inflect("PL(I,$N1) PL_V(saw,$N1) PL(a,$N2) PL_N(saw,$N2)"); print inflect("NUM($N1,)PL(I) PL_V(saw) NUM($N2,)PL(a) PL_N(saw)"); print inflect("I saw NUM($cat_count) PL("cat")\nNUM()"); print inflect("There PL_V(was,$errors) NO(error,$errors)\n"); print inflect("There NUM($errors,) PL_V(was) NO(error)\n"; print inflect("Did you want A($thing) or AN($idea)\n"); print inflect("It was ORD($position) from the left\n");
# ADD USER-DEFINED INFLECTIONS (OVERRIDING INBUILT RULES):
def_noun "VAX" => "VAXen"; # SINGULAR => PLURAL
def_verb "will" => "shall", # 1ST PERSON SINGULAR => PLURAL "will" => "will", # 2ND PERSON SINGULAR => PLURAL "will" => "will", # 3RD PERSON SINGULAR => PLURAL
def_adj "hir" => "their", # SINGULAR => PLURAL
def_a "h" # "AY HALWAYS SEZ 'HAITCH'!"
def_an "horrendous.*" # "AN HORRENDOUS AFFECTATION"
The exportable subroutines of Lingua::EN::Inflect provide plural inflections, \*(L"a\*(R"/\*(L"an\*(R" selection for English words, and manipulation of numbers as words
Plural forms of all nouns, most verbs, and some adjectives are provided. Where appropriate, \*(L"classical\*(R" variants (for example: \*(L"brother\*(R" -> \*(L"brethren\*(R", \*(L"dogma\*(R" -> \*(L"dogmata\*(R", etc.) are also provided.
Pronunciation-based \*(L"a\*(R"/\*(L"an\*(R" 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 (\*(L"one\*(R", \*(L"two\*(R", "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 \*(L"classical\*(R" form and a \*(L"modern\*(R" form), Lingua::EN::Inflect prefers the more common form (typically the \*(L"modern\*(R" one), unless \*(L"classical\*(R" processing has been specified (see \*(L"\s-1MODERN\s0 \s-1VS\s0 \s-1CLASSICAL\s0 \s-1INFLECTIONS\s0\*(R").
All of the \*(C`PL_...\*(C' 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 \*(C`PL_...\*(C' subroutines also take an optional second argument, which indicates the grammatical \*(L"number\*(R" of the word (or of another word with which the word being inflected must agree). If the \*(L"number\*(R" 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 \*(L"number\*(R" argument does indicate singularity, the (uninflected) word itself is returned. If the number argument is omitted, the plural form is returned unconditionally.
The various subroutines are: The exportable subroutine \*(C`PL_N()\*(C' takes a singular English noun or pronoun and returns its plural. Pronouns in the nominative (\*(L"I\*(R" -> \*(L"we\*(R") and accusative (\*(L"me\*(R" -> \*(L"us\*(R") cases are handled, as are possessive pronouns (\*(L"mine\*(R" -> \*(L"ours\*(R"). The exportable subroutine \*(C`PL_V()\*(C' takes the singular form of a conjugated verb (that is, one which is already in the correct \*(L"person\*(R" and \*(L"mood\*(R") and returns the corresponding plural conjugation. The exportable subroutine \*(C`PL_ADJ()\*(C' takes the singular form of certain types of adjectives and returns the corresponding plural form. Adjectives that are correctly handled include: \*(L"numerical\*(R" adjectives (\*(L"a\*(R" -> \*(L"some\*(R"), demonstrative adjectives (\*(L"this\*(R" -> \*(L"these\*(R", \*(L"that\*(R" -> \*(L"those\*(R"), and possessives (\*(L"my\*(R" -> \*(L"our\*(R", \*(L"cat's\*(R" -> \*(L"cats'\*(R", \*(L"child's\*(R" -> \*(L"childrens'\*(R", etc.) The exportable subroutine \*(C`PL()\*(C' 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 \*(L"thought\*(R" inflects to \*(L"thoughts\*(R", the verb \*(L"thought\*(R" to \*(L"thought\*(R"), the (singular) noun sense is preferred to the (singular) verb sense. Hence \*(C`PL("knife")\*(C' will return \*(L"knives\*(R" (\*(L"knife\*(R" having been treated as a singular noun), whereas \*(C`PL("knifes")\*(C' will return \*(L"knife\*(R" (\*(L"knifes\*(R" having been treated as a 3rd person singular verb). The inherent ambiguity of such cases suggests that, where the part of speech is known, \*(C`PL_N\*(C', \*(C`PL_V\*(C', and \*(C`PL_ADJ\*(C' should be used in preference to \*(C`PL\*(C'.
Note that all these subroutines ignore any whitespace surrounding the word being inflected, but preserve that whitespace when the result is returned. For example, \*(C`PL(" cat ")\*(C' returns \*(L" cats \*(R".
The \*(C`PL_...\*(C' subroutines return only the inflected word, not the count that was used to inflect it. Thus, in order to produce \*(L"I saw 3 ducks\*(R", 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 (\*(C`NO($;$)\*(C') 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 \*(L"no\*(R". 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.
Wordy and comma'd plurals
The \*(C`NO()\*(C' subroutine takes an optional third argument: a hash of named options that configure its behaviour.
The 'words_below' option informs \*(C`NO()\*(C' what other numbers (i.e. apart from zero) it should convert to words. For example:S
for my $count (0..12) { print NO('cat', $count, {words_below => 10}), "\n"; }
would print:
no cats one cat two cats three cats four cats five cats six cats seven cats eight cats nine cats 10 cats 11 cats 12 cats
The 'comma' and 'comma_every' options determine whether or not the numbers produced by \*(C`NO()\*(C' have commas in them. That is:
2001 space odysseys
versus:
2,001 space odysseys
Normally, numbers are produced without commas, but if 'comma' or 'comma_every' is specified, then commas are added as requested.
The 'comma' option specifies which character to use as a comma. It defaults to ',', but may be set to anything convenient:
print NO('Euro', $amount, {comma=>'.'});
# prints: 1.000.000 Euros
The 'comma_every' option specifies how many characters between commas. It defaults to 3, but may be set to any positive number:
print NO('Euro', $amount, {comma_every=>4});
# prints: 100,0000 Euros
Note that you can set both options at once, if you wish:
print NO('Euro', $amount, {comma_every=>2, comma=>'_'});
# prints: 1_00_00_00 Euros
In some contexts, the need to supply an explicit count to the various \*(C`PL_...\*(C' subroutines makes for tiresome repetition. For example:
print PL_ADJ("This",$errors), PL_N(" error",$errors), PL_V(" was",$errors), " fatal.\n";
Lingua::EN::Inflect therefore provides an exportable subroutine (\*(C`NUM($;$)\*(C') which may be used to set a persistent \*(L"default number\*(R" value. If such a value is set, it is subsequently used whenever an optional second \*(L"number\*(R" argument is omitted. The default value thus set can subsequently be removed by calling \*(C`NUM()\*(C' with no arguments. Hence we could rewrite the previous example:
NUM($errors); print PL_ADJ("This"), PL_N(" error"), PL_V(" was"), "fatal.\n"; NUM();
Normally, \*(C`NUM()\*(C' returns its first argument, so that it may also be \*(L"inlined\*(R" in contexts like:
print NUM($errors), PL_N(" error"), PL_V(" was"), " detected.\n" print PL_ADJ("This"), PL_N(" error"), PL_V(" was"), "fatal.\n" if $severity > 1;
However, in certain contexts (see \*(L"\s-1INTERPOLATING\s0 \s-1INFLECTIONS\s0 \s-1IN\s0 \s-1STRINGS\s0\*(R") it is preferable that \*(C`NUM()\*(C' return an empty string. Hence \*(C`NUM()\*(C' provides an optional second argument. If that argument is supplied (that is, if it is defined) and evaluates to false, \*(C`NUM\*(C' returns an empty string instead of its first argument. For example:
print NUM($errors,0), NO("error"), PL_V(" was"), " detected.\n"; print PL_ADJ("This"), PL_N(" error"), PL_V(" was"), "fatal.\n" if $severity > 1;
Lingua::EN::Inflect also provides a solution to the problem of comparing words of differing plurality through the exportable subroutines \*(C`PL_eq($$)\*(C', \*(C`PL_N_eq($$)\*(C', \*(C`PL_V_eq($$)\*(C', and \*(C`PL_ADJ_eq($$)\*(C'. Each of these subroutines takes two strings, and compares them using the corresponding plural-inflection subroutine (\*(C`PL()\*(C', \*(C`PL_N()\*(C', \*(C`PL_V()\*(C', and \*(C`PL_ADJ()\*(C' respectively).
The comparison returns true if:
the strings are \*(C`eq\*(C'-equal, or
one string is \*(C`eq\*(C'-equal to a plural form of the other, or
the strings are two different plural forms of the one word.
Hence all of the following return true:
PL_eq("index","index") # RETURNS "eq" PL_eq("index","indexes") # RETURNS "s:p" PL_eq("index","indices") # RETURNS "s:p" PL_eq("indexes","index") # RETURNS "p:s" PL_eq("indices","index") # RETURNS "p:s" PL_eq("indices","indexes") # RETURNS "p:p" PL_eq("indexes","indices") # RETURNS "p:p" PL_eq("indices","indices") # RETURNS "eq"
As indicated by the comments in the previous example, the actual value returned by the various \*(C`PL_eq\*(C' subroutines encodes which of the three equality rules succeeded: \*(L"eq\*(R" is returned if the strings were identical, \*(L"s:p\*(R" if the strings were singular and plural respectively, \*(L"p:s\*(R" for plural and singular, and \*(L"p:p\*(R" 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:
PL_eq("base","basis") # ALTHOUGH BOTH -> "bases" PL_eq("syrinx","syringe") # ALTHOUGH BOTH -> "syringes" PL_eq("she","he") # ALTHOUGH BOTH -> "they"
PL_eq("opus","operas") # ALTHOUGH "opus" -> "opera" -> "operas" PL_eq("taxi","taxes") # ALTHOUGH "taxi" -> "taxis" -> "taxes"
Note too that, although the comparison is \*(L"number-insensitive\*(R" it is not case-insensitive (that is, \*(C`PL("time","Times")\*(C' returns false. To obtain both number and case insensitivity, prefix both arguments with \*(C`lc\*(C' (that is, \*(C`PL(lc "time", lc "Times")\*(C' returns true).
\*(C`Lingua::EN::Inflect\*(C' also provides the \*(C`PART_PRES\*(C' subroutine, which can take a 3rd person singular verb and correctly inflect it to its present participle:
PART_PRES("runs") # "running" PART_PRES("loves") # "loving" PART_PRES("eats") # "eating" PART_PRES("bats") # "batting" PART_PRES("spies") # "spying"
Lingua::EN::Inflect provides two exportable subroutines (\*(C`A($;$)\*(C' and \*(C`AN($;$)\*(C') which will correctly prepend the appropriate indefinite article to a word, depending on its pronunciation. For example:
A("cat") # -> "a cat" AN("cat") # -> "a cat" A("euphemism") # -> "a euphemism" A("Euler number") # -> "an Euler number" A("hour") # -> "an hour" A("houri") # -> "a houri"
The two subroutines are identical in function and may be used interchangeably. The only reason that two versions are provided is to enhance the readability of code such as:
print "That is ", AN($errortype), " error\n; print "That is ", A($fataltype), " fatal error\n;
Note that in both cases the actual article provided depends only on the pronunciation of the first argument, not on the name of the subroutine.
\*(C`A()\*(C' and \*(C`AN()\*(C' will ignore any indefinite article that already exists at the start of the string. Thus:
@half_arked = ( "a elephant", "a giraffe", "an ewe", "a orangutan", );
print A($_), "\n" for @half_arked;
# prints: # an elephant # a giraffe # a ewe # an orangutan
\*(C`A()\*(C' and \*(C`AN()\*(C' both take an optional second argument. As with the \*(C`PL_...\*(C' subroutines, this second argument is a \*(L"number\*(R" specifier. If its value is 1 (or some other value implying singularity), \*(C`A()\*(C' and \*(C`AN()\*(C' insert \*(L"a\*(R" or \*(L"an\*(R" as appropriate. If the number specifier implies plurality, (\*(C`A()\*(C' and \*(C`AN()\*(C' insert the actual second argument instead. For example:
A("cat",1) # -> "a cat" A("cat",2) # -> "2 cat" A("cat","one") # -> "one cat" A("cat","no") # -> "no cat"
Note that, as implied by the previous examples, \*(C`A()\*(C' and \*(C`AN()\*(C' both assume that their job is merely to provide the correct qualifier for a word (that is: \*(L"a\*(R", \*(L"an\*(R", 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 \*(L"2 cat\*(R", instead of \*(L"2 cats\*(R". 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 \*(C`PL_...\*(C' subroutines, whenever \*(C`A()\*(C' and \*(C`AN()\*(C' are called with only one argument they are subject to the effects of any preceding call to \*(C`NUM()\*(C'. Hence, another possible solution is:
NUM($N); print A(PL("cat"));
\*(L"Initialisms\*(R" (sometimes inaccurately called \*(L"acronyms\*(R") are terms which have been formed from the initial letters of words in a phrase (for example, \*(L"\s-1NATO\s0\*(R", \*(L"\s-1NBL\s0\*(R", \*(L"S.O.S.\*(R", \*(L"\s-1SCUBA\s0\*(R", etc.)
Such terms present a particular challenge when selecting between \*(L"a\*(R" and \*(L"an\*(R", since they are sometimes pronounced as if they were a single word (\*(L"nay-tow\*(R", \*(L"sku-ba\*(R") and sometimes as a series of letter names (\*(L"en-eff-ell\*(R", \*(L"ess-oh-ess\*(R").
\*(C`A()\*(C' and \*(C`AN()\*(C' 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, \*(L"R.I.P.\*(R", \*(L"C.O.D.\*(R", \*(L"e-mail\*(R", \*(L"X-ray\*(R", \*(L"T-square\*(R"), then choose the appropriate article for the sound of the first letter (\*(L"an R.I.P.\*(R", \*(L"a C.O.D.\*(R", \*(L"an e-mail\*(R", \*(L"an X-ray\*(R", \*(L"a T-square\*(R").
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, \*(L"\s-1LCD\s0\*(R", \*(L"\s-1XML\s0\*(R", \*(L"\s-1YWCA\s0\*(R"), then once again choose \*(L"a\*(R" or \*(L"an\*(R" depending on the sound of the first letter (\*(L"an \s-1LCD\s0\*(R", \*(L"an \s-1XML\s0\*(R", \*(L"a \s-1YWCA\s0\*(R").
Otherwise, assume the string is a capitalized word or a pronounceable initialism (for example, \*(L"\s-1LED\s0\*(R", \*(L"\s-1OPEC\s0\*(R", \*(L"\s-1FAQ\s0\*(R", \*(L"\s-1UNESCO\s0\*(R"), and therefore takes \*(L"a\*(R" or \*(L"an\*(R" according to the (apparent) pronunciation of the entire word (\*(L"a \s-1LED\s0\*(R", \*(L"an \s-1OPEC\s0\*(R", \*(L"a \s-1FAQ\s0\*(R", \*(L"a \s-1UNESCO\s0\*(R").
Note that rules 1 and 3 together imply that the presence or absence of punctuation may change the selection of indefinite article for a particular initialism (for example, \*(L"a \s-1FAQ\s0\*(R" but \*(L"an F.A.Q.\*(R"). Words beginning in the letter 'H' present another type of difficulty when selecting a suitable indefinite article. In a few such words (for example, \*(L"hour\*(R", \*(L"honour\*(R", \*(L"heir\*(R") the 'H' is not voiced at all, and so such words inflect with \*(L"an\*(R". The remaining cases (\*(L"voiced H's\*(R") may be divided into two categories: \*(L"hard H's\*(R" (such as \*(L"hangman\*(R", \*(L"holograph\*(R", \*(L"hat\*(R", etc.) and \*(L"soft H's\*(R" (such as \*(L"hysterical\*(R", \*(L"horrendous\*(R", \*(L"holy\*(R", etc.)
Hard H's always take \*(L"a\*(R" as their indefinite article, and soft H's normally do so as well. But some English speakers prefer \*(L"an\*(R" for soft H's (although the practice is now generally considered an affectation, rather than a legitimate grammatical alternative).
At present, the \*(C`A()\*(C' and \*(C`AN()\*(C' subroutines ignore soft H's and use \*(L"a\*(R" for any voiced 'H'. The author would, however, welcome feedback on this decision (envisaging a possible future \*(L"soft H\*(R" mode).
Occasionally it is useful to present an integer value as an ordinal rather than as a numeral. For example:
Enter password (1st attempt): ******** Enter password (2nd attempt): ********* Enter password (3rd attempt): ********* No 4th attempt. Access denied.
To this end, Lingua::EN::Inflect provides the \*(C`ORD()\*(C' subroutine. <\s-1ORD\s0()> takes a single argument and forms its ordinal equivalent. If the argument isn't a numerical integer, it just adds \*(L"-th\*(R".
The exportable subroutine \*(C`NUMWORDS\*(C' takes a number (cardinal or ordinal) and returns an English representation of that number. In a scalar context a string is returned. Hence:
use Lingua::EN::Inflect qw( NUMWORDS );
$words = NUMWORDS(1234567);
puts the string:
"one million, two hundred and thirty-four thousand, five hundred and sixty-seven"
into $words.
In a list context each comma-separated chunk is returned as a separate element. Hence:
@words = NUMWORDS(1234567);
puts the list:
("one million", "two hundred and thirty-four thousand", "five hundred and sixty-seven")
into @words.
Non-digits (apart from an optional leading plus or minus sign, any decimal points, and ordinal suffixes \*(-- see below) are silently ignored, so the following all produce identical results:
NUMWORDS(5551202); NUMWORDS(5_551_202); NUMWORDS("5,551,202"); NUMWORDS("555-1202");
That last case is a little awkward since it's almost certainly a phone number, and \*(L"five million, five hundred and fifty-one thousand, two hundred and two\*(R" probably isn't what's wanted.
To overcome this, \*(C`NUMWORDS()\*(C' 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)
\*(C`NUMWORDS\*(C' also handles decimal fractions. Hence:
NUMWORDS("1.2345")
returns "one point two three four five" in a scalar context and \*(C`("one","point","two","three","four","five")\*(C') in an array context. Exponent form ("1.234e56") is not yet handled.
Multiple decimal points are only translated in one of the \*(L"grouping\*(R" 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 \*(L"zero\*(R", \*(L"oh\*(R", or \*(L"nought\*(R". To cater for this diversity, \*(C`NUMWORDS\*(C' 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 \*(L"zero\*(R".
Likewise, the digit '1' may be rendered as \*(L"one\*(R" or \*(L"a/an\*(R" (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 \*(L"a/an\*(R" is to be used as a 'one' value. Unless the next word is known in advance, it's almost always necessary to use the \*(C`A\*(C' function as well:
print A( NUMWORDS(1, one=>'a') . " $_\n") for qw(cat aardvark ewe hour);
# prints: # a cat # an aardvark # a ewe # an hour
Another major regional variation in number translation is the use of \*(L"and\*(R" in certain contexts. The named argument 'and' allows the programmer to specify how \*(L"and\*(R" should be handled. Hence:
print scalar NUMWORDS("765", 'and'=>'')
prints \*(L"seven hundred sixty-five\*(R", instead of \*(L"seven hundred and sixty-five\*(R". By default, the \*(L"and\*(R" is included.
The translation of the decimal point is also subject to variation (with \*(L"point\*(R", \*(L"dot\*(R", and \*(L"decimal\*(R" 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 \*(L"six sixty-six, dot, one twenty-four, dot, sixty-four, dot, one zero one\*(R" By default, the decimal point is rendered as \*(L"point\*(R".
\*(C`NUMWORDS\*(C' also handles the ordinal forms of numbers. So:
print scalar NUMWORDS('1st'); print scalar NUMWORDS('3rd'); print scalar NUMWORDS('202nd'); print scalar NUMWORDS('1000000th');
print:
first third two hundred and twenty-second one millionth
Two common idioms in this regard are:
print scalar NUMWORDS(ORD($number));
and:
print scalar ORD(NUMWORDS($number));
These are identical in effect, except when $number contains a decimal:
$number = 99.09; print scalar NUMWORDS(ORD($number)); # ninety-ninth point zero nine print scalar ORD(NUMWORDS($number)); # ninety-nine point zero ninth
Use whichever you feel is most appropriate.
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 \*(L"and\*(R" or \*(L"or\*(R") 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: \*(C`WORDLIST()\*(C'. 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:
print "You chose ", WORDLIST(@selected_items), "\n"; # You chose barley soup, roast beef, and Yorkshire pudding
print "You chose ", WORDLIST(@selected_items, {final_sep=>""}), "\n"; # You chose barley soup, roast beef and Yorkshire pudding
print "Please chose ", WORDLIST(@side_orders, {conj=>"or"}), "\n"; # Please chose salad, vegetables, or ice-cream
The available options are:
Option named Specifies Default value
conj Final conjunction "and" sep Inter-item separator "," last_sep Final separator value of 'sep' option
By far the commonest use of the inflection subroutines is to produce message strings for various purposes. For example:
print NUM($errors), PL_N(" error"), PL_V(" was"), " detected.\n"; print PL_ADJ("This"), PL_N(" error"), PL_V(" was"), "fatal.\n" if $severity > 1;
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.
Using \*(C`inflect\*(C' the previous example could be rewritten:
print inflect "NUM($errors) PL_N(error) PL_V(was) detected.\n"; print inflect "PL_ADJ(This) PL_N(error) PL_V(was) fatal.\n" if $severity > 1;
Note that \*(C`inflect\*(C' also correctly handles calls to the \*(C`NUM()\*(C' subroutine (whether interpolated or antecedent). The \*(C`inflect()\*(C' subroutine has a related extra feature, in that it automatically cancels any \*(L"default number\*(R" value before it returns its interpolated string. This means that calls to \*(C`NUM()\*(C' which are embedded in an \*(C`inflect()\*(C'-interpolated string do not \*(L"escape\*(R" and interfere with subsequent inflections.
Certain words, mainly of Latin or Ancient Greek origin, can form plurals either using the standard English \*(L"-s\*(R" suffix, or with their original Latin or Greek inflections. For example:
PL("stigma") # -> "stigmas" or "stigmata" PL("torus") # -> "toruses" or "tori" PL("index") # -> "indexes" or "indices" PL("millennium") # -> "millenniums" or "millennia" PL("ganglion") # -> "ganglions" or "ganglia" PL("octopus") # -> "octopuses" or "octopodes"
Lingua::EN::Inflect caters to such words by providing an \*(L"alternate state\*(R" of inflection known as \*(L"classical mode\*(R". 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 \*(C`classical()\*(C' controls this feature. If \*(C`classical()\*(C' 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.
Thus:
classical; # SWITCH ON CLASSICAL MODE print PL("formula"); # -> "formulae"
classical 0; # SWITCH OFF CLASSICAL MODE print PL("formula"); # -> "formulas"
classical $cmode; # CLASSICAL MODE IFF $cmode print PL("formula"); # -> "formulae" (IF $cmode) # -> "formulas" (OTHERWISE)
classical herd=>1; # SWITCH ON CLASSICAL MODE FOR "HERD" NOUNS print PL("wilderbeest"); # -> "wilderbeest"
classical names=>1; # SWITCH ON CLASSICAL MODE FOR NAMES print PL("sally"); # -> "sallies" print PL("Sally"); # -> "Sallys"
Note however that \*(C`classical()\*(C' has no effect on the inflection of words which are now fully assimilated. Hence:
PL("forum") # ALWAYS -> "forums" PL("criterion") # ALWAYS -> "criteria"
\s-1LEI\s0 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 \*(C`classical(names=>0)\*(C'.
Lingua::EN::Inflect provides five exportable subroutines which allow the programmer to override the module's behaviour for specific cases: The \*(C`def_noun\*(C' 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 \*(C`m/^(?:$first_arg)$/i\*(C'). 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: def_noun 'cow' => 'kine'; def_noun '(.+i)o' => '$1i'; def_noun 'spam(mer)?' => '\\$\\%\\@#\\$\\@#!!'; 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 \*(L"classical\*(R" plurals have been requested. The beginning of the second variant is marked by a '|' character: def_noun 'cow' => 'cows|kine'; def_noun '(.+i)o' => '$1os|$1i'; def_noun 'spam(mer)?' => '\\$\\%\\@#\\$\\@#!!|varmints'; If no classical variant is given, the specified plural form is used in both normal and \*(L"classical\*(R" modes. If the second argument is \*(C`undef\*(C' instead of a string, then the current user definition for the first argument is removed, and the standard plural inflection(s) restored. Note that in all cases, later plural definitions for a particular singular form replace earlier definitions of the same form. For example: # FIRST, HIDE THE MODERN FORM.... def_noun 'aviatrix' => 'aviatrices';
# LATER, HIDE THE CLASSICAL FORM... def_noun 'aviatrix' => 'aviatrixes';
# FINALLY, RESTORE THE DEFAULT BEHAVIOUR... def_noun 'aviatrix' => undef; Special care is also required when defining general patterns and associated specific exceptions: put the more specific cases after the general pattern. For example: def_noun '(.+)us' => '$1i'; # EVERY "-us" TO "-i" def_noun 'bus' => 'buses'; # EXCEPT FOR "bus" This \*(L"try-most-recently-defined-first\*(R" approach to matching user-defined words is also used by \*(C`def_verb\*(C', \*(C`def_a\*(C' and \*(C`def_an\*(C'. The \*(C`def_verb\*(C' subroutine takes three pairs of string arguments (that is, six arguments in total), specifying the singular and plural forms of the three \*(L"persons\*(R" of verb. As with \*(C`def_noun\*(C', the singular forms are specifications of run-time-interpolated patterns, whilst the plural forms are specifications of (up to two) run-time-interpolated strings: def_verb 'am' => 'are', 'are' => 'are|art", 'is' => 'are';
def_verb 'have' => 'have', 'have' => 'have", 'ha(s|th)' => 'have'; Note that as with \*(C`def_noun\*(C', modern/classical variants of plurals may be separately specified, subsequent definitions replace previous ones, and \*(C`undef\*(C''ed plural forms revert to the standard behaviour. The \*(C`def_adj\*(C' subroutine takes a pair of string arguments, which specify the singular and plural forms of the adjective being defined. As with \*(C`def_noun\*(C' and \*(C`def_adj\*(C', the singular forms are specifications of run-time-interpolated patterns, whilst the plural forms are specifications of (up to two) run-time-interpolated strings: def_adj 'this' => 'these', def_adj 'red' => 'red|gules', As previously, modern/classical variants of plurals may be separately specified, subsequent definitions replace previous ones, and \*(C`undef\*(C''ed plural forms revert to the standard behaviour. The \*(C`def_a\*(C' and \*(C`def_an\*(C' subroutines each take a single argument, which specifies a pattern. If a word passed to \*(C`A()\*(C' or \*(C`AN()\*(C' 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 \*(C`def_...\*(C' subroutines, such redefinitions are sequential in effect so that, after the above example, \*(L"error\*(R" will be inflected with \*(L"an\*(R". 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 \*(C`def_noun\*(C', \*(C`def_verb\*(C', etc. in one of these .inflectrc files, to permanently and universally modify the behaviour of the module. For example
> cat /usr/local/lib/perl5/Text/Inflect/.inflectrc
def_noun "UNIX" => "UN*X|UNICES";
def_verb "teco" => "teco", # LITERALLY: "to edit with TECO" "teco" => "teco", "tecos" => "teco";
def_a "Euler.*"; # "Yewler" TURNS IN HIS GRAVE
Note that calls to the \*(C`def_...\*(C' 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 \*(C`def_...\*(C' subroutines do).
Using the five \*(C`def_...\*(C' subroutines directly in a program may also result in fatal diagnostics, if a (singular) pattern or an interpolated (plural) string is somehow invalid.
Specific diagnostics related to user-defined inflections are: The singular form of a user-defined noun or verb (as defined by a call to \*(C`def_noun\*(C', \*(C`def_verb\*(C', \*(C`def_adj\*(C', \*(C`def_a\*(C' or \*(C`def_an\*(C') 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 \*(C`def_noun\*(C', \*(C`def_verb\*(C' or \*(C`def_adj\*(C') 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 \*(C`NUMWORDS\*(C' 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 \*(L"guess\*(R" the commonest English inflection: adding \*(L"-s\*(R" for nouns, removing \*(L"-s\*(R" for verbs, and no inflection for adjectives.
\*(C`Lingua::EN::Inflect::NUMWORDS()\*(C' can \*(C`die\*(C' with the following messages: The optional argument to \*(C`NUMWORDS()\*(C' wasn't 1, 2 or 3. \*(C`NUMWORDS()\*(C' was passed a number larger than 999,999,999,999,999,999,999,999,999,999,999,999 (that is: nine hundred and ninety-nine decillion, nine hundred and ninety-nine nonillion, nine hundred and ninety-nine octillion, nine hundred and ninety-nine septillion, nine hundred and ninety-nine sextillion, nine hundred and ninety-nine quintillion, nine hundred and ninety-nine quadrillion, nine hundred and ninety-nine trillion, nine hundred and ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine :-) The problem is that \*(C`NUMWORDS\*(C' doesn't know any words for number components bigger than \*(L"decillion\*(R".
If a verb has identical 1st and 2nd person singular forms, but different 1st and 2nd person plural forms, then when its plural is constructed, the 2nd person plural form is always preferred.
The author is not currently aware of any such verbs in English, but is not quite arrogant enough to assume ipso facto that none exist.
The singular pronoun \*(L"it\*(R" presents a special problem because its plural form can vary, depending on its \*(L"case\*(R". For example:
It ate my homework -> They ate my homework It ate it -> They ate them I fed my homework to it -> I fed my homework to them
As a consequence of this ambiguity, \*(C`PL()\*(C' or \*(C`PL_N\*(C' have been implemented so that they always return the nominative plural (that is, \*(L"they\*(R").
However, when asked for the plural of an unambiguously accusative \*(L"it\*(R" (namely, \*(C`PL("to it")\*(C', \*(C`PL_N("from it")\*(C', \*(C`PL("with it")\*(C', etc.), both subroutines will correctly return the accusative plural (\*(L"to them\*(R", \*(L"from them\*(R", \*(L"with them\*(R", etc.)
The rules governing the choice between:
There were no errors.
and
There was no error.
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 \*(L"classical\*(R" inflection is in effect, in which case the singular form is always returned.
Thus, the sequence:
NUM(0); print inflect "There PL(was) NO(choice)";
produces \*(L"There were no choices\*(R", whereas:
classical 'zero'; # or: classical(zero=>1); NUM(0); print inflect "There PL(was) NO(choice)";
it will print \*(L"There was no choice\*(R".
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, \*(L"bass\*(R" is a noun in both sentences above), then one meaning is chosen as the \*(L"usual\*(R" 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, \*(L"thought\*(R" is both a noun and a verb), then the noun's plural is returned by \*(C`PL()\*(C' and \*(C`PL_N()\*(C' and the verb's plural is returned only by \*(C`PL_V()\*(C'.
Such contexts are, fortunately, uncommon (particularly \*(L"same-part-of-speech\*(R" examples). An informal study of nearly 600 \*(L"difficult plurals\*(R" indicates that \*(C`PL()\*(C' can be relied upon to \*(L"get it right\*(R" 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 \*(L"usual inflection\*(R" is considered inappropriate, it can always be reversed with a preliminary call to the corresponding \*(C`def_...\*(C' subroutine.
I'm not taking any further correspondence on: Despite the populist pandering of certain New World dictionaries, the plural is \*(L"octopuses\*(R" or (for the pedantic classicist) \*(L"octopodes\*(R". The suffix \*(L"-pus\*(R" is Greek, not Latin, so the plural is \*(L"-podes\*(R", not \*(L"pi\*(R". Had no plural in Latin (possibly because it was a mass noun). The only plural is the Anglicized \*(L"viruses\*(R".
Damian Conway ([email protected]) Matthew Persico (\s-1ORD\s0 inflection)
The endless inconsistencies of English.
(Please report words for which the correct plural or indefinite article is not formed, so that the reliability of Lingua::EN::Inflect can be improved.)
Copyright (c) 1997-2009, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.