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)?
Using Microsoft's Northwind example database:
System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new System.Globalization.CultureInfo("en-US"));
Singularize does not Singularize "Order_Details"
It returns "Order_Details" with the s
at the end. What is the work around?
I whipped one together based on the Rails pluralizer. You can see my blog post here, or on github here
output = Formatting.Pluralization(100, "sausage");
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"));
This page shows how to use PluralizationService
of System.Data.Entity
(.NET Framework 4.0)
http://zquanghoangz.blogspot.it/2012/02/beginner-with-pluralizationservices.html
I've created a tiny library for this in .net (C#), called Pluralizer (unsurprisingly).
It's meant to work with full sentences, something like String.Format does.
It basically works like this:
var target = new Pluralizer();
var str = "There {is} {_} {person}.";
var single = target.Pluralize(str, 1);
Assert.AreEqual("There is 1 person.", single);
// Or use the singleton if you're feeling dirty:
var several = Pluralizer.Instance.Pluralize(str, 47);
Assert.AreEqual("There are 47 people.", several);
It can also do way more than that. Read more about it on my blog. It's also available in NuGet.