views:

624

answers:

6

This is about best practices in general, not specific for a single language, database or whatever

We all have to deal with generated output where you can be reporting "one products" or "two product". Doesn't read very well... Some just solve this by using "one product(s)" or "number of products: (1)" and others might have other solutions.

Things could be even more complex in different spoken languages! In French, when you have zero products, you would use the singular form, not the plural form! (Zero product) Other languages (Chinese, Japanese) might even lack these grammatical differences or have more than two different words to indicate something about the number of products. (A plural and a greater plural, for example.)

But to keep this simple, let's focus on the languages that have both singular and plural words.

When setting up a new project, which also has to generate reports, how do you deal with singular and plural words? Do you add two name fields in your database for singular and plural form? Do you add additional rules in the code to transform words from singular to plural? Do you use other tricks?

When working on a project that needs to track singular and plural forms, how do you deal with this?

A: 
Number of products:  1 
Number of products:  4
Number of products:  FILE_NOT_FOUND

Trying to use natural language for reporting quantitative data is just too much of a hassle.

MusiGenesis
Yeah, I know. :-) Just humor me and think of this as an important requirement in the reporting engine of the product you're working on.
Workshop Alex
And since when being too much of a hassle has stopped mankind?
Vinko Vrsalovic
Awww... Now I can't stop wondering about the plural form of "mankind"... "mankinds" perhaps? ;-)
Workshop Alex
I think yes, but that brings up the question what you're supposed to do when confronted with the need to use plural for the real singulare tantum, like *dust* or *wealth* ;-) On the other hand, if you're going to sell dust or scissors you will likely have it accompanied with units like carat of dust (sounds promising:)) or pair of scissors.
Michael Krelin - hacker
Interestingly, a scissor is supposed to be just a single blade of a pair of scissors. Technically, you could call two scissors a quarter of scissors... :-) In a time when scissors were hand-made, it would still be practical to use the singular word. Modern industrialization just made the singular form obsolete. :-)
Workshop Alex
@Workshop Alex: you guys are making my point for me. This is a gigantic can of worms just for English, and if you throw in other languages, it will be gigantic *cans of worms* (how 'bout that one for you?).
MusiGenesis
I think the plural of "mankind" is "menkind".
MusiGenesis
Don't scissor scissors until scissors scissor you!
Michael Krelin - hacker
@hacker: scissor? I barely *know* her!
MusiGenesis
MusiGenesis ;-)))) The more reason not to scissor!
Michael Krelin - hacker
A: 

With english applications it's usually easiest and most efficient to store the singular and create the plural by using a group of if statements.

if( count > 1 ){ suffix = 's'; }

Mikey
-1 buses, alumni, etc.
MusiGenesis
+1  A: 

Normally I would send my text through some kind of formatter, which reformats the values you want to display to a human readable text. This also could modify your "product" text. Java has the MessageFormat class for this, which supports such modifications. See the examples at [1].

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html

ZeissS
+7  A: 

I'd recommend taking a look at gettext in general and ngettext in particular. Maybe even if you're not going to translate your application. Just head to this part of the documentation. It has implementation for more or less all languages and even if your language of choice lacks this support, nothing stops you from borrowing the ideas.

Michael Krelin - hacker
But how about product names stored in a database?
Workshop Alex
The same thing - storing singular/plural pairs. And, in case of translation (which is not your case or is it?) you'll just have to make sure they are either put into .po if you use gettext or use similar algorithm and store it elsewhere (of course it makes no sense to put thousands of products into .po file).
Michael Krelin - hacker
Just thought about another problem with database-stored product names - it is quite easy to handle plural/singular form, but when it comes to translation there would be a problem with cases. And trying to stick to nominative may turn out to be almost as awkward as 'Number of products: N'.
Michael Krelin - hacker
+1  A: 

Read and implement this; report back when you're done (in a few years). Personally, I'm satisfied with the (s) approach ;) (though it goes without saying this doesn't work for all languages).

Noon Silk
Well, that link clearly shows the complexity of singular/plural words. Also shows that the number of forms even varies from just one to up to 6 different forms! I like this site already! :-)
Workshop Alex
Alex, this - http://translate.sourceforge.net/wiki/l10n/pluralforms - looks like more comprehensive resource on plural forms for different languages.
Michael Krelin - hacker
+2  A: 

In Perl, this is comprehensively solved by Lingua::EN::Inflect. It uses a large dictionary and carefully handles all the exceptions to rules. It also does things like 'a' or 'an', and handles comparisons as well!

See the paper for the gory details.

ire_and_curses
One problem with this reference is that it's not language-agnostic, no matter what do you refer to as *language* here ;-) The other problem is that the problem goes beyond plural form of the noun, there *is* this thing called the whole sentence (and there *are* these things called whole sentences).
Michael Krelin - hacker
But I upvote it as an interesting reference, anyway ;)
Michael Krelin - hacker
@hacker: You are quite right - this module is English-specific. But it handles that language very well.
ire_and_curses
@hacker: 'is' and 'are' type-problems are handled correctly. e.g.: print inflect "PL_ADJ(This) PL_N(error) PL_V(was) fatal.\n" if $severity > 1;
ire_and_curses
ire_and_curses, nice to know, I've only briefly skimmed through the code. But only English and only Perl problem is still there. Another reason why this link worth upvoting is that if English language is used as a reference for translation, this tool is useful for guessing plural product name from singular.
Michael Krelin - hacker