public class RuleBasedPluralizer extends java.lang.Object implements Pluralizer
Pluralizer implemented using an ordered list of WordRules.
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.
| 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 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.
|
| 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.
|
protected static final Pluralizer NOOP_PLURALIZER
protected java.util.List<WordRule> rules
protected java.util.Locale locale
protected Pluralizer fallbackPluralizer
public RuleBasedPluralizer()
public RuleBasedPluralizer(java.util.List<WordRule> rules, java.util.Locale locale)
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.
public RuleBasedPluralizer(java.util.List<WordRule> rules, java.util.Locale locale, Pluralizer fallbackPluralizer)
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.
public Pluralizer getFallbackPluralizer()
public void setFallbackPluralizer(Pluralizer fallbackPluralizer)
fallbackPluralizer - The fall back pluralizer.public java.util.Locale getLocale()
public void setLocale(java.util.Locale locale)
locale - The pluralizer locale.public java.util.List<WordRule> getRules()
public void setRules(java.util.List<WordRule> rules)
rules - The pluralizer rules.public java.lang.String pluralize(java.lang.String nounOrPronoun)
pluralize in interface PluralizernounOrPronoun - The singular form of the noun or pronoun.public java.lang.String pluralize(java.lang.String nounOrPronoun,
int number)
pluralize in interface PluralizernounOrPronoun - The singular form of the noun or pronoun.number - The number for the noun or pronoun.protected java.lang.String pluralizeInternal(java.lang.String nounOrPronoun)
nounOrPronoun - Singular noun or pronoun.null
when no rule matches.protected java.lang.String postProcess(java.lang.String trimmedWord,
java.lang.String pluralizedWord)
trimmedWord - The input word, with leading and trailing
whitespace removed.pluralizedWord - The pluralized word.pluralizedWord after
processing.
If trimmedWord is all uppercase, then
pluralizedWord is uppercased.
If trimmedWord is titlecase, then
pluralizedWord is titlecased.