Inflector for .NET
Posted
by srkirkland
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by srkirkland
Published on Tue, 15 Mar 2011 16:17:05 GMT
Indexed on
2011/03/16
0:11 UTC
Read the original article
Hit count: 608
I was writing conventions for FluentNHibernate the other day and I ran into the need to pluralize a given string and immediately thought of the ruby on rails Inflector. It turns out there is a .NET library out there also capable of doing word inflection, originally written (I believe) by Andrew Peters, though the link I had no longer works. The entire Inflector class is only a little over 200 lines long and can be easily included into any project, and contains the Pluralize() method along with a few other helpful methods (like Singularize(), Camelize(), Capitalize(), etc).
The Inflector class is available in its entirety from my github repository https://github.com/srkirkland/Inflector. In addition to the Inflector.cs class I added tests for every single method available so you can gain an understanding of what each method does. Also, if you are wondering about a specific test case feel free to fork my project and add your own test cases to ensure Inflector does what you expect.
Here is an example of some test cases for pluralize:
TestData.Add("quiz", "quizzes");
TestData.Add("perspective", "perspectives");
TestData.Add("ox", "oxen");
TestData.Add("buffalo", "buffaloes");
TestData.Add("tomato", "tomatoes");
TestData.Add("dwarf", "dwarves");
TestData.Add("elf", "elves");
TestData.Add("mouse", "mice");
TestData.Add("octopus", "octopi");
TestData.Add("vertex", "vertices");
TestData.Add("matrix", "matrices");
TestData.Add("rice", "rice");
TestData.Add("shoe", "shoes");
Pretty smart stuff.
© ASP.net Weblogs or respective owner