Skip to content

Commit

Permalink
fix: Pluralize.toPlural logic and rules, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm authored and manusa committed Apr 8, 2021
1 parent 61c74da commit 0a0b07d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,122 +16,110 @@
package io.fabric8.kubernetes.api;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pluralize implements UnaryOperator<String>
{

private static Pluralize INSTANCE = null;
private static final Object LOCK = new Object();
private static final List<String> UNCOUNTABLE = Arrays.asList("equipment", "fish", "information", "money", "rice", "series", "sheep", "species");
private static final List<UnaryOperator<String>> PLURALS = Arrays.<UnaryOperator<String>>asList(
//Irregulars
new StringReplace("(p)eople$", "$1erson"),
new StringReplace("(m)en$", "$1an"),
new StringReplace("(c)hildren$", "$1hild"),
new StringReplace("(s)exes$", "$1ex"),
new StringReplace("(m)oves$", "$1ove"),
new StringReplace("(s)tadiums$", "$1tadium"),

//Rules
new StringReplace("(quiz)$", "$1zes"),
new StringReplace("(matr)ix$", "$1ices"),
new StringReplace("(vert|ind)ex$", "$1ices"),
new StringReplace("^(ox)$", "$1en"),
new StringReplace("(alias|status)$", "$1"),
new StringReplace("(alias|status)$", "$1es"),
new StringReplace("(octop|vir)us$", "$1us"),
new StringReplace("(cris|ax|test)is$", "$1es"),
new StringReplace("(shoe)$", "$1s"),
new StringReplace("(o)$", "$1es"),
new StringReplace("(bus)$", "$1es"),
new StringReplace("([m|l])ouse$", "$1ice"),
new StringReplace("(x|ch|ss|sh)$", "$1es"),
new StringReplace("(m)ovie$", "$1ovies"),
new StringReplace("(s)eries$", "$1eries"),
new StringReplace("([^aeiouy]|qu)y$", "$1ies"),
new StringReplace("([lr])f$", "$1ves"),
new StringReplace("(tive)$", "$1s"),
new StringReplace("(hive)$", "$1s"),
new StringReplace("([^f])fe$", "$1ves"),
new StringReplace("(^analy)sis$", "$1sis"),
new StringReplace("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis"),
new StringReplace("([ti])um$", "$1a"),
new StringReplace("(n)ews$", "$1ews"),
new StringReplace("(s|si|u)s$", "$1s")
);

public static String toPlural(String word) {
if (INSTANCE != null) {
return INSTANCE.apply(word);
}
public class Pluralize implements UnaryOperator<String> {

synchronized (LOCK) {
if (INSTANCE == null) {
INSTANCE = new Pluralize();
}
}
return INSTANCE.apply(word);
private static final Pluralize INSTANCE = new Pluralize();
private static final Set<String> UNCOUNTABLE = new HashSet<>(Arrays.asList("equipment", "fish",
"information", "money", "rice", "series", "sheep", "species", "news"));
private static final Map<String, String> EXCEPTIONS = new HashMap<>();

static {
EXCEPTIONS.put("person", "people");
EXCEPTIONS.put("man", "men");
EXCEPTIONS.put("child", "children");
EXCEPTIONS.put("ox", "oxen");
EXCEPTIONS.put("die", "dice");
}

private static final List<UnaryOperator<String>> PLURALS = Arrays.asList(
//Rules
new StringReplace("([^aeiouy]|qu)y$", "$1ies"),
new StringReplace("(x|ch|ss|sh)$", "$1es"),
new StringReplace("(s)?ex$", "$1exes"),
new StringReplace("(bus)$", "$1es"),
new StringReplace("(quiz)$", "$1zes"),
new StringReplace("(matr)ix$", "$1ices"),
new StringReplace("(vert|ind)ex$", "$1ices"),
new StringReplace("(alias|status)$", "$1es"),
new StringReplace("(octop|vir)us$", "$1us"),
new StringReplace("(cris|ax|test)is$", "$1es"),
new StringReplace("(o)$", "$1es"),
new StringReplace("([m|l])ouse$", "$1ice"),
new StringReplace("([lr])f$", "$1ves"),
new StringReplace("([^f])fe$", "$1ves"),
new StringReplace("(^analy)sis$", "$1sis"),
new StringReplace("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis"),
new StringReplace("([ti])um$", "$1a"),
new StringReplace("(s|si|u)s$", "$1s")
);

public static String toPlural(String word) {
return INSTANCE.apply(word);
}

public String apply(String word) {
if (word == null || word.isEmpty() || UNCOUNTABLE.contains(word)) {
return word;
}

public String apply(String word) {
if (word == null) {
return null;
} else if (word.isEmpty()) {
return word;
} else if (UNCOUNTABLE.contains(word)) {
return word;
} else if (isAlreadyPlural(word)) {
return word;
}

for (UnaryOperator<String> function : PLURALS) {
String result = function.apply(word);
if (result != null) {
return result;
}
}
return word + "s";
// deal with exceptions
String plural = EXCEPTIONS.get(word);
if (plural != null) {
return plural;
}

// apply rules
for (UnaryOperator<String> function : PLURALS) {
String result = function.apply(word);
if (result != null) {
return result;
}
}

/**
* Rudimentary implementation of checking whether word is plural or not.
* It can be further improved to handle complex cases.
*
* @param word
* @return Boolean value indicating whether it's already plural or not
*/
private boolean isAlreadyPlural(String word) {
if(!word.endsWith("ss")) {
if(word.endsWith("s")) {
return true;
}
}
return false;
// we haven't found a match, if the word is already plural, return it or add a final 's'
return isAlreadyPlural(word) ? word : word + "s";
}

/**
* Rudimentary implementation of checking whether word is plural or not. It can be further
* improved to handle complex cases.
*
* @param word the word to test
* @return {@code true} if the specified word is already plural, {@code false} otherwise
*/
private boolean isAlreadyPlural(String word) {
if (!word.endsWith("ss")) {
return word.endsWith("s");
}
return false;
}

private static class StringReplace implements UnaryOperator<String> {

private final String target;
private final String replacement;
private final Pattern pattern;


public StringReplace(String target, String replacement) {
this.target = target;
this.replacement = replacement;
this.pattern = Pattern.compile(target, Pattern.CASE_INSENSITIVE);
this.replacement = replacement;
this.pattern = Pattern.compile(target, Pattern.CASE_INSENSITIVE);
}

public String apply(String word) {
Matcher matcher = this.pattern.matcher(word);
if (!matcher.find()) {
return null;
}
return matcher.replaceAll(replacement);
Matcher matcher = this.pattern.matcher(word);
if (!matcher.find()) {
return null;
}
return matcher.replaceAll(replacement);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.fabric8.kubernetes.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

public class PluralizeTest {
@Test
void pluralizeShouldWork() {
assertNull(Pluralize.toPlural(null));
assertEquals("", Pluralize.toPlural(""));

assertEquals("equipment", Pluralize.toPlural("equipment"));
assertEquals("news", Pluralize.toPlural("news"));

assertEquals("people", Pluralize.toPlural("person"));
assertEquals("children", Pluralize.toPlural("child"));
assertEquals("shoes", Pluralize.toPlural("shoe"));
assertEquals("loves", Pluralize.toPlural("love"));
assertEquals("movies", Pluralize.toPlural("movie"));
assertEquals("lives", Pluralize.toPlural("life"));
assertEquals("chives", Pluralize.toPlural("chive"));
assertEquals("diminutives", Pluralize.toPlural("diminutive"));
assertEquals("dice", Pluralize.toPlural("die"));
assertEquals("scarves", Pluralize.toPlural("scarf"));
assertEquals("humans", Pluralize.toPlural("human"));

assertEquals("definitions", Pluralize.toPlural("definition"));
assertEquals("statuses", Pluralize.toPlural("status"));
assertEquals("endpoints", Pluralize.toPlural("endpoints"));
}

}

0 comments on commit 0a0b07d

Please sign in to comment.