• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singular string from plural noun

 
Clifton Craig
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Clifton Craig
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic