1

Does Java have a straightforward method for pluralizing a word? If not, I am wondering why it is not available when Rails has one.

Any specific reasons?

Slowcoder
  • 2,060
  • 3
  • 16
  • 21
  • 3
    because it's language specific. – jtahlborn Apr 24 '13 at 18:47
  • 1
    This is down to spoken language not programming language. You can make of programming language like Java to do it but it doesn't do itself. – IndoKnight Apr 24 '13 at 18:48
  • 4
    http://stackoverflow.com/questions/5907296/plural-form-of-a-word – James Scholes Apr 24 '13 at 18:49
  • http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-pluralize – Slowcoder Apr 24 '13 at 18:55
  • Rails has the pluralizing by a s simple method. http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-pluralize. So I was expecting something like this for English in Java. But I agree that supporting pluralization for all languages is not possible. – Slowcoder Apr 24 '13 at 19:01

2 Answers2

7

jtahlborn has a good point with regards to language specificity. That said, this is what I wrote for use in our app. It's not comprehensive, but it's easy enough to add exceptions as you hit them.

This is C#, but should be close enough or you to adapt:

public string GetPlural(string singular)
{
    string CONSONANTS = "bcdfghjklmnpqrstvwxz";

    switch (singular)
    {
        case "Person":
            return "People";
        case "Trash":
            return "Trash";
        case "Life":
            return "Lives";
        case "Man":
            return "Men";
        case "Woman":
            return "Women";
        case "Child":
            return "Children";
        case "Foot":
            return "Feet";
        case "Tooth":
            return "Teeth";
        case "Dozen":
            return "Dozen";
        case "Hundred":
            return "Hundred";
        case "Thousand":
            return "Thousand";
        case "Million":
            return "Million";
        case "Datum":
            return "Data";
        case "Criterion":
            return "Criteria";
        case "Analysis":
            return "Analyses";
        case "Fungus":
            return "Fungi";
        case "Index":
            return "Indices";
        case "Matrix":
            return "Matrices";
        case "Settings":
            return "Settings";
        case "UserSettings":
            return "UserSettings";
        default:
            // Handle ending with "o" (if preceeded by a consonant, end with -es, otherwise -s: Potatoes and Radios)
            if (singular.EndsWith("o") && CONSONANTS.Contains(singular[singular.Length - 2].ToString()))
            {
                return singular + "es";
            }
            // Handle ending with "y" (if preceeded by a consonant, end with -ies, otherwise -s: Companies and Trays)
            if (singular.EndsWith("y") && CONSONANTS.Contains(singular[singular.Length - 2].ToString()))
            {
                return singular.Substring(0, singular.Length - 1) + "ies";
            }
            // Ends with a whistling sound: boxes, buzzes, churches, passes
            if (singular.EndsWith("s") || singular.EndsWith("sh") || singular.EndsWith("ch") || singular.EndsWith("x") || singular.EndsWith("z"))
            {
                return singular + "es";
            }
            return singular + "s";

    }
}

An additional usage note... As you can probably see, it expects whatever word you're passing in to have the first letter capitalized (doesn't matter if it's not in the exceptions list). There are a myriad of a ways of handling this differently. It's simply what met our particular needs.

Pete
  • 6,585
  • 5
  • 43
  • 69
  • I just noticed the "UserSettings" at the bottom there. I ought to mention, one of the things we use this function for is generating code for our app. Hence the very specific "UserSettings" entry there... – Pete Apr 24 '13 at 19:02
  • Thanks you, this is helpful. I will implement this approach in java for my requirement. – Slowcoder Apr 25 '13 at 13:59
0

Take a look at JBoss's Inflector class. Even if you're not using JBoss you can grok the source code for a comprehensive approach to accomplishing this.

Madbreaks
  • 19,094
  • 7
  • 58
  • 72