PDA

View Full Version : Changing plural / singular words code


adamylo
10-06-2009, 11:32 AM
Using jsp code what is the best way to change keywords from plural to singular.

Eg: changing a keyword from match to matches or bag to bags.

cs_student
10-06-2009, 11:46 PM
How in-depth do you want to go?

You can solve many cases of this problem by simply appending a 's' to the end of the string.

However, the plurals for words such as loaf, knife, foot, etc. would be much more complex.

You could search google for a list of irregular plurals and their singular form and put them in a file to load into a map during runtime. Then check if a singular word is a key in the map to determine if you can simply append an 's' or if you need to use the special plural form.

Regards,


cs_student

adamylo
10-06-2009, 11:53 PM
thanks for your help.

We have a catalog of mostly IT items with laptops, desktops.

With generating keywords for titles on pages ect we are getting netbooks laptops - the best buy.

We need to somehow drop the s of netbooks as an example. Although adding the s would have been fine we need to know how to take it away.

As the word netbooks is just taken from one of our categories automatically we need a sting or snippet of code that will work.

cs_student
10-07-2009, 12:17 AM
The problem at hand is a little ambiguous.

Will there be any cases in which irregular plurals are used? (ie knives)

Will the word always be plural? Are you processing one word at a time or are you trying to identify plural words in a sentence and turn them singular?

I believe the case is that you are going to just have a regular plural verb.

In that case just truncate the 's'.


Is this what you are trying to do?

adamylo
10-07-2009, 12:19 AM
Just getting rid of the "s" that simple.

cs_student
10-07-2009, 01:19 AM
Try the String.substring(int beginIndex, int endIndex) (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int,%20int)) method.

You can truncate the s by simply taking the substring from chars 0 to n-1.

Give it a shot. If you have any trouble post what you have tried and what you specifically had trouble with.


cs_student

adamylo
10-07-2009, 01:34 AM
Here is the code how do you think it should be written where we are trying to drop the "s" off the word in ${group.name}?.

<pages:default title="${group.name} - ${parent.name} - Buy Discounted ${group.name} ${parent.name} at Cheapest Prices!">
<jsp:attribute name="head">

Thanks for your help

cs_student
10-07-2009, 02:44 AM
String notebook = "Notebooks";
out.println( notebook.substring(0, notebook.length() - 1) );


Should print "Notebook"

You figure out how to incorporate it into your own code.


cs_student