Is there any algorithm in c# to singularize - pluralize a word?

Is there any algorithm in c# to singularize - pluralize a word (in english) or does exist a .net library to do this (may be also in different languages)?


Solution 1:

You also have the System.Data.Entity.Design.PluralizationServices.PluralizationService.

UPDATE: Old answer deserves update. There's now also Humanizer: https://github.com/MehdiK/Humanizer

Solution 2:

I can do it for Esperanto, with no special cases!

string plural(string noun) { return noun + "j"; }

For English, it would be useful to become familiar with the rules for Regular Plurals of Nouns, as well as Irregular Plurals of Nouns. There is a whole Wikipedia article on the English plural, which may have some helpful information too.

Solution 3:

Most ORMs have a stab at it, although they generally aren't perfect. I know Castle has it's Inflector Class you can probably poke around. Doing it "perfectly" isn't an easy task though (English "rules" aren't really rules :)), so it depends if you are happy with a "reasonable guess" approach.

Solution 4:

I cheated in Java - I wanted to be able to produce a correct string for "There were n something(s)", so I wrote the foll. little overloaded utility method:

static public String pluralize(int val, String sng) {
    return pluralize(val,sng,(sng+"s"));
    }

static public String pluralize(int val, String sng, String plu) {
    return (val+" "+(val==1 ? sng : plu)); 
    }

invoked like so

System.out.println("There were "+pluralize(count,"something"));
System.out.println("You have broken "+pluralize(count,"knife","knives"));