Java String Singularize SingularToPlural(String noun)

Here you can find the source of SingularToPlural(String noun)

Description

Singular To Plural

License

Apache License

Declaration

public static String SingularToPlural(String noun) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String SingularToPlural(String noun) {
        String[] ExceptionWords_DirectAddS = { "canto", "solo", "piano", "lasso", "halo", "memento", "albino",
                "sirocco", "chief", "fife", "mischief", "hoof", "roof", "grief", "kerchief", "safe" };

        String[] ExceptionWords_IrregularInput = { "man", "foot", "mouse", "woman", "tooth", "louse", "child", "ox",
                "goose" };

        String[] ExceptionWords_IrregularOutput = { "men", "feet", "mice", "women", "teeth", "lice", "children",
                "oxen", "geese" };

        String[] ExceptionWords_NoPlural = { "gold", "silver", "wheat", "corn", "molasses", "copper", "sugar",
                "cotton", "USA" };

        noun = noun.trim();//  ww w. j  a va2s.c om
        for (String str : ExceptionWords_DirectAddS) {
            if (noun.compareToIgnoreCase(str) == 0) {
                return noun + "s";
            }
        }
        int i = 0;
        for (String str : ExceptionWords_IrregularInput) {
            if (noun.compareToIgnoreCase(str) == 0) {
                return ExceptionWords_IrregularOutput[i];
            }
            i++;
        }
        for (String str : ExceptionWords_NoPlural) {
            if (noun.compareToIgnoreCase(str) == 0) {
                return noun;
            }
        }
        if ((noun.endsWith("s")) || (noun.endsWith("z")) || (noun.endsWith("x")) || (noun.endsWith("sh"))
                || (noun.endsWith("ch"))) {
            noun = noun + "es";
        } else if (noun.endsWith("y")) {
            if ((noun.endsWith("ay")) || (noun.endsWith("ey")) || (noun.endsWith("iy")) || (noun.endsWith("oy"))
                    || (noun.endsWith("uy"))) {
                noun = noun + "s";
            } else {
                noun = noun.substring(0, noun.length() - 1) + "ies";
            }
        } else if (noun.endsWith("o")) {
            if ((noun.endsWith("ao")) || (noun.endsWith("eo")) || (noun.endsWith("io")) || (noun.endsWith("oo"))
                    || (noun.endsWith("uo"))) {
                noun = noun + "s";
            } else {
                noun = noun + "es";
            }
        } else if ((noun.endsWith("f")) && (noun.length() >= 1)) {
            noun = noun.substring(0, noun.length() - 1) + "ves";
        } else if ((noun.endsWith("fe")) && (noun.length() >= 2)) {
            noun = noun.substring(0, noun.length() - 2) + "ves";
        } else if (!noun.endsWith("\"")) {
            noun = noun + "s";
        }
        return noun;
    }
}

Related

  1. singularise(String name)
  2. singularize(final String buffer)
  3. singularize(String value)
  4. singularOrPlural(int testValue, String singular, String plural)
  5. singularPlural(int n, String singular, String plural)