Lingua-ENG-Inflect

 view release on metacpan or  search on metacpan

lib/Lingua/ENG/Inflect.pm  view on Meta::CPAN

    'lowlife'     => 'lowlifes',
    'flatfoot'    => 'flatfoots',
    'tenderfoot'  => 'tenderfoots',
    'Romany'      => 'Romanies',
    'romany'      => 'romanies',
    'Jerry'       => 'Jerrys',
    'jerry'       => 'jerries',
    'Mary'        => 'Marys',
    'mary'        => 'maries',
    'talouse'     => 'talouses',
    'blouse'      => 'blouses',
    'Rom'         => 'Roma',
    'rom'         => 'roma',

    %PL_sb_irregular_s,
);

my $PL_sb_irregular = enclose join '|', keys %PL_sb_irregular;

# CLASSICAL "..is" -> "..ides"

my @PL_sb_C_is_ides = 
(
# GENERAL WORDS...

    "ephemeris", "iris", "clitoris",
    "chrysalis", "epididymis",

# INFLAMATIONS...

    ".*itis", 

);

my $PL_sb_C_is_ides = enclose join "|", map { substr($_,0,-2) } @PL_sb_C_is_ides;

# CLASSICAL "..a" -> "..ata"

my @PL_sb_C_a_ata = 
(
    "anathema", "bema", "carcinoma", "charisma", "diploma",
    "dogma", "drama", "edema", "enema", "enigma", "lemma",
    "lymphoma", "magma", "melisma", "miasma", "oedema",
    "sarcoma", "schema", "soma", "stigma", "stoma", "trauma",
    "gumma", "pragma",
);

my $PL_sb_C_a_ata = enclose join "|", map { substr($_,0,-1) } @PL_sb_C_a_ata;

# UNCONDITIONAL "..a" -> "..ae"

my $PL_sb_U_a_ae = enclose join "|", 
(
    "alumna", "alga", "vertebra", "persona"
);

# CLASSICAL "..a" -> "..ae"

my $PL_sb_C_a_ae = enclose join "|", 
(
    "amoeba", "antenna", "formula", "hyperbola",
    "medusa", "nebula", "parabola", "abscissa",
    "hydra", "nova", "lacuna", "aurora", ".*umbra",
    "flora", "fauna",
);

# CLASSICAL "..en" -> "..ina"

my $PL_sb_C_en_ina = enclose join "|", map { substr($_,0,-2) }
(
    "stamen", "foramen", "lumen", "carmen"
);

# UNCONDITIONAL "..um" -> "..a"

my $PL_sb_U_um_a = enclose join "|", map { substr($_,0,-2) }
(
    "bacterium",    "agendum",  "desideratum",  "erratum",
    "stratum",  "datum",    "ovum",     "extremum",
    "candelabrum",
);

# CLASSICAL "..um" -> "..a"

my $PL_sb_C_um_a = enclose join "|", map { substr($_,0,-2) }
(
    "maximum",  "minimum",    "momentum",   "optimum",
    "quantum",  "cranium",    "curriculum", "dictum",
    "phylum",   "aquarium",   "compendium", "emporium",
    "enconium", "gymnasium",  "honorarium", "interregnum",
    "lustrum",  "memorandum", "millennium", "rostrum", 
    "spectrum", "speculum",   "stadium",    "trapezium",
    "ultimatum",    "medium",   "vacuum",   "velum", 
    "consortium",
);

# UNCONDITIONAL "..us" -> "i"

my $PL_sb_U_us_i = enclose join "|", map { substr($_,0,-2) }
(
    "alumnus",  "alveolus", "bacillus", "bronchus",
    "locus",    "nucleus",  "stimulus", "meniscus",
    "sarcophagus",
);

# CLASSICAL "..us" -> "..i"

my $PL_sb_C_us_i = enclose join "|", map { substr($_,0,-2) }
(
    "focus",    "radius",   "genius",
    "incubus",  "succubus", "nimbus",
    "fungus",   "nucleolus",    "stylus",
    "torus",    "umbilicus",    "uterus",
    "hippopotamus", "cactus",
);

# CLASSICAL "..us" -> "..us"  (ASSIMILATED 4TH DECLENSION LATIN NOUNS)

my $PL_sb_C_us_us = enclose join "|",
(
    "status", "apparatus", "prospectus", "sinus",

lib/Lingua/ENG/Inflect.pm  view on Meta::CPAN

                # "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"

# LITERAL STYLE ONLY NAMES NUMBERS LESS THAN A CERTAIN THRESHOLD...

    $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"

=head1 DESCRIPTION

Plural inflection for ENG.

The exportable subroutines of Lingua::ENG::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::ENG::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 I<not> explicitly catered for.

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

lib/Lingua/ENG/Inflect.pm  view on Meta::CPAN

    sep             Inter-item separator     ","
    last_sep        Final separator          value of 'sep' option

=head1 INTERPOLATING INFLECTIONS IN STRINGS

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::ENG::Inflect provides an exportable string-interpolating
subroutine (C<inflect($)>), which recognizes calls to the various inflection
subroutines within a string and interpolates them appropriately.

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

=head1 MODERN VS CLASSICAL INFLECTIONS

Certain words, mainly of Latin or Ancient Greek origin, can form
plurals either using the standard English "-s" 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::ENG::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 C<classical()> controls this feature.
If C<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.

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()> has no effect on the inflection of words which
are now fully assimilated. Hence:

        PL("forum")             # ALWAYS -> "forums"
        PL("criterion")         # ALWAYS -> "criteria"

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 C< classical(names=>0) >;

=head1 USER-DEFINED INFLECTIONS

=head2 Adding plurals at run-time

Lingua::ENG::Inflect provides five exportable subroutines which allow
the programmer to override the module's behaviour for specific cases:

=over 8

=item C<def_noun($$)>

The C<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 C<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:

      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 "classical" 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 "classical" modes.



( run in 0.424 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )