introduce pluralizer: partially restore the original "forced" semantics

This commit is contained in:
Gregory.Shrago
2016-11-03 14:25:25 +03:00
parent d5e89e2279
commit e053c3140a
3 changed files with 27 additions and 19 deletions
@@ -63,7 +63,7 @@ class Pluralizer {
* Pass in a word token to produce a function that can replicate the case on
* another word.
*/
private static String restoreCase(String word, String result) {
static String restoreCase(String word, String result) {
if (word == null || result == null || word == result) return result;
char[] chars = result.toCharArray();
boolean prevUp = false;
@@ -173,20 +173,20 @@ class Pluralizer {
*/
JBIterable.of(new String[][]{
// Pronouns.
{"I", "we"},
{"me", "us"},
{"he", "they"},
{"she", "they"},
{"them", "them"},
{"myself", "ourselves"},
{"yourself", "yourselves"},
{"itself", "themselves"},
{"herself", "themselves"},
{"himself", "themselves"},
{"themself", "themselves"},
{"is", "are"},
{"was", "were"},
{"has", "have"},
//{"I", "we"},
//{"me", "us"},
//{"he", "they"},
//{"she", "they"},
//{"them", "them"},
//{"myself", "ourselves"},
//{"yourself", "yourselves"},
//{"itself", "themselves"},
//{"herself", "themselves"},
//{"himself", "themselves"},
//{"themself", "themselves"},
//{"is", "are"},
//{"was", "were"},
//{"has", "have"},
{"this", "these"},
{"that", "those"},
// Words ending in with a consonant and `o`.
@@ -832,7 +832,9 @@ public class StringUtil extends StringUtilRt {
@NotNull
@Contract(pure = true)
public static String pluralize(@NotNull String word) {
return Pluralizer.PLURALIZER.plural(word);
String plural = Pluralizer.PLURALIZER.plural(word);
return equalsIgnoreCase(plural, word) && !endsWithIgnoreCase(plural, "es") ?
Pluralizer.restoreCase(word, word + "s") : plural;
}
@NotNull
@@ -1645,13 +1647,17 @@ public class StringUtil extends StringUtilRt {
* Returns unpluralized variant using English based heuristics like properties -> property, names -> name, children -> child.
* Returns <code>null</code> if failed to match appropriate heuristic.
*
* @param name english word in plural form
* @param word english word in plural form
* @return name in singular form or <code>null</code> if failed to find one.
*/
@Nullable
@Contract(pure = true)
public static String unpluralize(@NotNull String word) {
return Pluralizer.PLURALIZER.singular(word);
String singular = Pluralizer.PLURALIZER.singular(word);
if (equalsIgnoreCase(singular, word)) {
singular = nullize(trimEnd(singular, "s", true));
}
return equalsIgnoreCase(singular, word) ? null : singular;
}
@Contract(pure = true)
@@ -98,7 +98,9 @@ public class StringUtilTest {
@Test
public void testUnPluralize() {
assertEquals("s", StringUtil.unpluralize("s"));
assertEquals("plurals", StringUtil.unpluralize("pluralss"));
assertEquals("I", StringUtil.unpluralize("Is"));
assertEquals(null, StringUtil.unpluralize("s"));
assertEquals("z", StringUtil.unpluralize("zs"));
assertEquals("Index", StringUtil.unpluralize("Indices"));
assertEquals("fix", StringUtil.unpluralize("fixes"));