tags:

views:

1290

answers:

7

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)?

+7  A: 

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.

Steven Robbins
From your suggestion I searched for "Inflector" and found this http://andrewpeters.net/inflectornet/ that sould basically be the same of the Castle one
Ronnie
Actually its not basically the same, its identical.
David Pfeffer
+2  A: 

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.

Greg Hewgill
You should make it throw if you pass in a verb or adverb!
Timwi
+2  A: 

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"));
    }
Software Monkey
This only covers a small sections of grammar though, it doesn't account for words like quizzes, parties, halves, mice, indices, etc. It's a good first stab, but there are a lot of other rules that should probably be processed first.
Jeremy Seghi
@Jeremy: Why not?: println("You have passed " + singularPlural(count,"quiz","quizzes") + " so far")
Software Monkey
@Software Monkey: I might be interpreting the question differently. I'm thinking the algorithm should be determining the plural form without any hint from the developer, while your method puts the onus of knowing what the plural form is on the developer.
Jeremy Seghi
@Jeremy: Hence the "I cheated..." lead in - doesn't seem to warrant a downvote.
Software Monkey
Agreed. I also think the information provided was useful, which is why any downvote didn't come from me. I don't downvote in general, along the lines of the "one man's junk...".
Jeremy Seghi
+1  A: 

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!

Chris S
+1  A: 

I whipped one together based on the Rails pluralizer. You can see it here

output = Formatting.Pluralization(100, "sausage"); 
Matt Grande
+15  A: 

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

Daniel
+1 For a solution in the framework.
jscharf
A: 

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?)

Eric H