Inflector.NET
A port of 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 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!
[...] and mouse => mice. Fortunately us in the .NET world can take advantage of the functionality with Infector.NET, a port of the Rails Infector by Andrew Peters. Using this I could simply set the value of my label [...]
Thanks for pointing this out on Fluent Nhibernate’s discussion board, now I can drop needing
.ForTypesThatDeriveFrom(autoMap => { autoMap.WithTable(”Categories”);
}
And handle everything with my convention statement
WithConvention(convention => convention.GetTableName = type => Inflector.Pluralize(type.Name))!
[...] You can download Inflector and it’s source over at Andrew Peter’s blog Inflector.NET. [...]
[...] While developing a codegen tool, I needed Ruby on Rails’ pluralization functionality. Fortunately, I found a port of Rail’s Inflector class at https://andrewpeters.net/inflectornet/. [...]
Do you have some problem if I copy the implementation in another project ?
@Fabio,
No.
Thank you very much Andrew. I just made a port from english to brazilian portuguese and it works like a charm.
You’re welcome
Ah sorry Andrew
this is the post about Inflector you reviewed and its implementation in Spanish
this is the last with some improvements