edu.northwestern.at.morphadorner.corpuslinguistics.inflector.pluralizer

## Class RuleBasedPluralizer

- java.lang.Object
- - edu.northwestern.at.morphadorner.corpuslinguistics.inflector.pluralizer.RuleBasedPluralizer

- All Implemented Interfaces:
    Pluralizer
  Direct Known Subclasses:
    EnglishPluralizer

      public class RuleBasedPluralizer
      extends java.lang.Object
      implements Pluralizer

  A `Pluralizer` implemented using an ordered list of `WordRule`s.

  You may specify a fallback `Pluralizer` that is invoked when none of the rules match. This allows you to override some rules of another `Pluralizer`.

  This class preserves leading and trailing whitespace, so individual rules don't need to explicitly handle whitespace.

  Case is also preserved -- that is, the output of all uppercase input is automatically uppercased, and the output of titlecase input is automatically titlecased. This means rules can act in a case-insensitive manner.

  Original code written by Tom White under the Apache v2 license. Modified by Philip R. Burns for integration into MorphAdorner.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Pluralizer` | `fallbackPluralizer`
    `protected java.util.Locale` | `locale`
    `protected static Pluralizer` | `NOOP_PLURALIZER`
    `protected java.util.List<WordRule>` | `rules`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RuleBasedPluralizer()` Construct a pluralizer with an empty list of rules.
    `RuleBasedPluralizer(java.util.List<WordRule> rules, java.util.Locale locale)` Construct a pluralizer with a list of rules.
    `RuleBasedPluralizer(java.util.List<WordRule> rules, java.util.Locale locale, Pluralizer fallbackPluralizer)` Construct a pluralizer with a list of rules and a backup pluralizer.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Pluralizer` | `getFallbackPluralizer()` Get fall back pluralizer.
    `java.util.Locale` | `getLocale()` Get the locale.
    `java.util.List<WordRule>` | `getRules()` Get the pluralizer rules.
    `java.lang.String` | `pluralize(java.lang.String nounOrPronoun)` Pluralize a noun or pronoun.
    `java.lang.String` | `pluralize(java.lang.String nounOrPronoun, int number)` Pluralize a noun or pronoun.
    `protected java.lang.String` | `pluralizeInternal(java.lang.String nounOrPronoun)` Apply list of rules to a noun or pronoun.
    `protected java.lang.String` | `postProcess(java.lang.String trimmedWord, java.lang.String pluralizedWord)` Fix case of pluralized word.
    `void` | `setFallbackPluralizer(Pluralizer fallbackPluralizer)` Set the fall back pluralizer.
    `void` | `setLocale(java.util.Locale locale)` Set the pluralizer locale.
    `void` | `setRules(java.util.List<WordRule> rules)` Set the pluralizer rules.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### NOOP_PLURALIZER

          protected static final Pluralizer NOOP_PLURALIZER

    - #### rules

          protected java.util.List<WordRule> rules

    - #### locale

          protected java.util.Locale locale

    - #### fallbackPluralizer

          protected Pluralizer fallbackPluralizer

  - ### Constructor Detail

    - #### RuleBasedPluralizer

          public RuleBasedPluralizer()

      Construct a pluralizer with an empty list of rules.

    - #### RuleBasedPluralizer

          public RuleBasedPluralizer(java.util.List<WordRule> rules,
                             java.util.Locale locale)

      Construct a pluralizer with a list of rules.
      Parameters:
        `rules` - The rules to apply, in order.
        `locale` - The locale specifying the language of the pluralizer.

        A noop pluralizer is used as the fall back pluralizer when none of the specified rules applies.

    - #### RuleBasedPluralizer

          public RuleBasedPluralizer(java.util.List<WordRule> rules,
                             java.util.Locale locale,
                             Pluralizer fallbackPluralizer)

      Construct a pluralizer with a list of rules and a backup pluralizer.
      Parameters:
        `rules` - The rules to apply, in order.
        `locale` - The locale specifying the language of the pluralizer.
        `fallbackPluralizer` - The pluralizer to use when no rules match.

        The fall back pluralizer is invoked when none of the specified rules applies.

  - ### Method Detail

    - #### getFallbackPluralizer

          public Pluralizer getFallbackPluralizer()

      Get fall back pluralizer.
      Returns:
        The fall back pluralizer.

    - #### setFallbackPluralizer

          public void setFallbackPluralizer(Pluralizer fallbackPluralizer)

      Set the fall back pluralizer.
      Parameters:
        `fallbackPluralizer` - The fall back pluralizer.

    - #### getLocale

          public java.util.Locale getLocale()

      Get the locale.
      Returns:
        The pluralizer locale.

    - #### setLocale

          public void setLocale(java.util.Locale locale)

      Set the pluralizer locale.
      Parameters:
        `locale` - The pluralizer locale.

    - #### getRules

          public java.util.List<WordRule> getRules()

      Get the pluralizer rules.
      Returns:
        The pluralizer rules.

    - #### setRules

          public void setRules(java.util.List<WordRule> rules)

      Set the pluralizer rules.
      Parameters:
        `rules` - The pluralizer rules.

    - #### pluralize

          public java.lang.String pluralize(java.lang.String nounOrPronoun)

      Pluralize a noun or pronoun.
      Specified by:
        `pluralize` in interface `Pluralizer`
      Parameters:
        `nounOrPronoun` - The singular form of the noun or pronoun.
      Returns:
        The plural form of the noun or pronoun.

    - #### pluralize

          public java.lang.String pluralize(java.lang.String nounOrPronoun,
                                   int number)

      Pluralize a noun or pronoun.
      Specified by:
        `pluralize` in interface `Pluralizer`
      Parameters:
        `nounOrPronoun` - The singular form of the noun or pronoun.
        `number` - The number for the noun or pronoun.
      Returns:
        The form of the noun or pronoun for the specified number.

    - #### pluralizeInternal

          protected java.lang.String pluralizeInternal(java.lang.String nounOrPronoun)

      Apply list of rules to a noun or pronoun.
      Parameters:
        `nounOrPronoun` - Singular noun or pronoun.
      Returns:
        Plural form of the noun or pronoun, or `null` when no rule matches.

    - #### postProcess

          protected java.lang.String postProcess(java.lang.String trimmedWord,
                                     java.lang.String pluralizedWord)

      Fix case of pluralized word.
      Parameters:
        `trimmedWord` - The input word, with leading and trailing whitespace removed.
        `pluralizedWord` - The pluralized word.
      Returns:
        The `pluralizedWord` after processing.

        If `trimmedWord` is all uppercase, then `pluralizedWord` is uppercased. If `trimmedWord` is titlecase, then `pluralizedWord` is titlecased.