Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way to handle singular/plural word based on some collection size [closed]

Tags:

java

jsp

jsp-tags

There are many instances in my work projects where I need to display the size of some collection in a sentence. For example, if the collection's size is 5, it will say "5 users". If it is size of 1 or 0, it will say "1 user" or "0 user". Right now, I'm doing it with if-else statements to determine whether to print the "s" or not, which is tedious.

I'm wondering if there's an open source JSP custom tag library that allows me to accomplish this. I know I can write one myself... basically, it will have 2 parameters like this: <lib:display word="user" collection="userList" />. Depending on the collection size, it will determine whether to append an "s" or not. But then, this implementation is not going to be too robust because I also need to handle "ies" and some words don't use any of those. So, instead of creating a half-baked tool, I'm hoping there's a more robust library I could utilize right away. I'm not too worried about prefixing the word with is/are in this case.

I use Java, by the way.

Thanks much.

like image 574
limc Avatar asked Jul 06 '10 19:07

limc


People also ask

How do you show a word as a singular and plural together?

It is sometimes desirable to refer to a noun in both its singular and plural form. The convention for doing so, for regular nouns that take the s-ending in plural, is to add the s and enclose it in parentheses.

What is the fifth rule for plural nouns?

Plural rule #5: some '-s' and '-z' endings. For some nouns that end in '-s' or '-z', you have to double the '-s' or '-z' and add '-es'.

What is the rule for plural words ending in s?

For the majority of words ending in S, you just add an -es to the end. “Walrus” becomes “walruses,” “bus” becomes “buses,” “class” becomes “classes.” Not too bad.


2 Answers

Take a look at inflector, a java project which lets you do Noun.pluralOf("user"), or Noun.pluralOf("user", userList.size()), and which handles a bunch of variations and unusual cases (person->people, loaf->loaves, etc.), as well as letting you define custom mapping rules when necessary.

like image 103
Jacob Mattison Avatar answered Sep 21 '22 20:09

Jacob Mattison


Hmm, I don't quite see why you need a library for this. I would think the function to do it is trivial:

public String singlePlural(int count, String singular, String plural)
{
  return count==1 ? singular : plural;
}

Calls would look like:

singlePlural(count, "user", "users");
singlePlural(count, "baby", "babies");
singlePlural(count, "person", "people");
singlePlural(count, "cherub", "cherubim");
... etc ...

Maybe this library does a whole bunch of other things that make it useful. I suppose you could say that it supplies a dictionary of what all the plural forms are, but in any given program you don't care about the plurals of all the words in the language, just the ones you are using in this program. I guess if the word that could be singular or plural is not known at compile time, if it's something entered by the user, then I'd want a third party dictionary rather than trying to build one myself.

Edit

Suddenly it occurs to me that what you were looking for was a function for making plurals generically, embodying a set of rules like "normally just add 's', but if the word ends in 'y' change the 'y' to 'ies', if it ends in 's' change it to 'ses', ..." etc. I think in English that would be impossible for any practical purpose: there are too many special cases, like "person/people" and "child/children" etc. I think the best you could do would be to have a generic "add an 's'" rule, maybe a few other common cases, and then a long list of exceptions. Perhaps in other languages one could come up with a fairly simple rule.

So as I say, if the word is not known at compile time but comes from some user input, then yes, a third-party dictionary is highly desirable.

like image 41
Jay Avatar answered Sep 21 '22 20:09

Jay