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)?
Most ORMs have a stab at it, although they generally aren't perfect. I know Catle 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.
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.
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 utility method:
static public String singularPlural(int val, String sng, String plu) {
return (val+" "+(val==1 ? sng : plu));
}
invoked like so
System.out.println("There were "+singularPlural(count,"something","somethings"));
I also had an overload that just added an "s" for the simple cases:
static public String singularPlural(int val, String sng) {
return singularPlural(val,sng,(sng+"s"));
}
Subsonic 3 has an Inflector
class which impressed me by turning Person
into People
. I peeked at the source and found it naturally cheats a little with a hardcoded list but that's really the only way of doing it in English and how humans do it - we remember the singular and plural of each word and don't just apply a rule. As there's not masculine/feminine(/neutral) to add to the mix it's a lot simpler.
Here's a snippet:
AddSingularRule("^(ox)en", "$1");
AddSingularRule("(vert|ind)ices$", "$1ex");
AddSingularRule("(matr)ices$", "$1ix");
AddSingularRule("(quiz)zes$", "$1");
AddIrregularRule("person", "people");
AddIrregularRule("man", "men");
AddIrregularRule("child", "children");
AddIrregularRule("sex", "sexes");
AddIrregularRule("tax", "taxes");
AddIrregularRule("move", "moves");
AddUnknownCountRule("equipment");
It accounts for some words not having plural equivalents, like the equipment example. As you can probably tell it does a simple Regex
replace using $1.
Update:
It appears Subsonic's Inflector
is infact the Castle ActiveRecord Inflector
class!
I whipped one together based on the Rails pluralizer. You can see it here
output = Formatting.Pluralization(100, "sausage");
You also have the System.Data.Entity.Design.PluralizationServices.PluralizationService.
I'm wanting to do the same thing. My first thought was a dictionary.com REST API or something. Anyone tried something like that or know if a service like this exists? I checked dictionary.reference.com and didn't see anything (after 15 seconds of detailed inspection).
Meanwhile, I think I'll check out System.Data.Entity.Design.PluralizationServices. Thanks for the tip Daniel!
Update: Works brilliantly for pluralizing class names in my custom ORM. (Go figure, right?)