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.

Comments

Leave a response

  1. Alex Henderson Thu, 05 Jul 2007 04:25:56 CDT

    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?

  2. Andrew Thu, 05 Jul 2007 04:57:47 CDT

    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.

  3. Alex Henderson Thu, 05 Jul 2007 05:08:47 CDT

    Ahh though they looked pretty similar (well identical, as it turns out heh!)

    Good stuff

  4. Sebastian Talamoni Thu, 12 Jul 2007 10:49:36 CDT

    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?

  5. Andrew Thu, 12 Jul 2007 19:21:50 CDT

    Your change probably makes sense and feel free to use the code.

  6. Chris Mon, 30 Jul 2007 19:03:16 CDT

    “status” doesn’t singularize correctly. Inflector.Singularize(”status”) returns “statu”.

  7. Andrew Mon, 30 Jul 2007 20:02:49 CDT

    That’s because the plural of status is statuses.

  8. Chris Tue, 31 Jul 2007 00:36:09 CDT

    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

  9. Andrew Sat, 04 Aug 2007 22:07:56 CDT

    That should be a straight-forward change. Take a look at how the rules are specified.

    Cheers.

  10. Chris Mon, 07 Jan 2008 00:09:50 CST

    Nice work, I do a lot of ActiveRecord code gen, this will come in handy. Thanks!

Comments