Inflector.NET
A port of Rails’ Inflector class for .NET. The Inflector allows you to determine the plural or singular of a given word, which can be useful for things like code generation. For example:
Inflector.Pluralize("address");
produces “addresses”. Whereas,
Inflector.Pluralize("species");
yields “species”;
The code is currently lacking some argument validation and doc comments but all of the tests have been ported and are all passing.
Download the source here: Inflector.NET Download
Let me know if you find any problems.
Enjoy.



Out of curiosity, does Inflector.Net offer any advantages over the Castle Project’s Inflector in ActiveRecord http://svn.castleproject.org:8080/svn/castle/trunk/ActiveRecord/Castle.ActiveRecord/Framework/Internal/Inflector.cs it’s seem very similar, and I wonder if perhaps there’s an opportunity to merge the two at some point?
Hi Alex,
They are one in the same. After I did the original port I also wrote the pluralization patch for ActiveRecord. :-) I’ll keep it available here in case anyone wants it on it’s own.
Cheers,
Andrew.
Ahh though they looked pretty similar (well identical, as it turns out heh!)
Good stuff
Excellent stuff Andrew.
I wrote a similar class myself without knowing this one already existed. (probably my class is not covering all the rules inflector has..)
BTW, is it expected behavior that ApplyRule will return null when no rule matches. Example: calling Pluralize(”Boys”) will return null instead of just returning.
I’ve added this code to ApplyRules (to match my expected behavior )
if (result == null) return word;
I would like to add this class to an existing commercial product, is there any license issue with that?
Your change probably makes sense and feel free to use the code.
“status” doesn’t singularize correctly. Inflector.Singularize(”status”) returns “statu”.
That’s because the plural of status is statuses.
Yea. In my code generator, I pass our table name of “work_item_status” to Inflector.Singularize I get a class named “WorkItemStatu”. We should rename our table to “work_item_statuses” or “status_of_work_items”.
I was kind of hoping singularizing procedures would ignore words that are already single, but that may be too much accomplish with a simple algorithm.
Chris
That should be a straight-forward change. Take a look at how the rules are specified.
Cheers.
Nice work, I do a lot of ActiveRecord code gen, this will come in handy. Thanks!