File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Singular string from plural noun Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singular string from plural noun" Watch "Singular string from plural noun" New topic
Author

Singular string from plural noun

Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
I am in the middle of writing some XML transform logic and now I need a way to convert a String from it's plural form to it's singular form. It will be used when a bean has a property like addresses that returns a list to create:


I don't want to iterate the entire dictionary and right now it doesn't have to be perfect but I want a tight way of getting as close as possible. The code resides in a StringHelper object. Here's my code so far:



Any advice?


Holla at me...<br /><a href="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
oh, and here's my currently failing unit test code. I think I can figure out why it's failing myself but feel free to point out the bug.

I really need help on the algorithm. Is there a better, tighter way?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

If this is true:then, the way your shouldEndWithE method is written, this:will always return false, because it's only going to be passed strings that end with "es".

Apart from that, there's probably a much longer list of irregularities you might want to include, starting with goose/geese and mouse/mice. And you missed the sky/skies group.

I suppose there's going to be something to make sure you only get plural nouns passed to this method? Otherwise you'll need some process to ignore valid singular nouns ending with "s".
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
This is a thorny problem, and all the solutions are hard. Performance will be an issue if your methods get called frequently. You may want to see if hash tables help performance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singular string from plural noun