function switching between singular and plural?

English Only Solution

Here is a PHP class that has worked flawlessly for me thus far: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/

Here it is as a Gist in case that site goes away: https://gist.github.com/tbrianjones/ba0460cc1d55f357e00b

This is a small class and could easily and quickly be converted to other languages.


Here is my handy function:

function plural( $amount, $singular = '', $plural = 's' ) {
    if ( $amount === 1 ) {
        return $singular;
    }
    return $plural;
}

By default, it just adds the 's' after the string. For example:

echo $posts . ' post' . plural( $posts );

This will echo '0 posts', '1 post', '2 posts', '3 posts', etc. But you can also do:

echo $replies . ' repl' . plural( $replies, 'y', 'ies' );

Which will echo '0 replies', '1 reply', '2 replies', '3 replies', etc. Or alternatively:

echo $replies . ' ' . plural( $replies, 'reply', 'replies' );

And it works for some other languages too. For example, in Spanish I do:

echo $comentarios . ' comentario' . plural( $comentarios );

Which will echo '0 comentarios', '1 comentario', '2 comentarios', '3 comentarios', etc. Or if adding an 's' is not the way, then:

echo $canciones . ' canci' . plural( $canciones, 'ón', 'ones' );

Which will echo '0 canciones', '1 canción', '2 canciones', '3 canciones', etc.


This is not easy: each language has its own rules for forming plurals of nouns. In English it tends to be that you put "-s" on the end of a word, unless it ends in "-x", "-s", "-z", "-sh", "-ch" in which case you add "-es". But then there's "mouse"=>"mice", "sheep"=>"sheep" etc.

The first thing to do, then, is to find out what the rule(s) are for forming the plural from the singular noun in the language(s) you want to work with. But that's not the whole solution. Another problem is recognising nouns. If you are given a noun, and need to convert it from singular to plural that's not too hard, but if you are given a piece of free text and you have to find the singular nouns and convert them to plural, that's a lot harder. You can get lists of nouns, but of course some words can be nouns and verbs ("hunt", "fish", "break" etc.) so then you need to parse the text to identify the nouns.

It's a big problem. There's probably an AI system out there that would do what you need, but I don't imagine there'll be anything free that does it all.


There is no function built into PHP for this type of operation. There are, however, some tools that you may be able to use to help accomplish your goal.

There is an unofficial Google Dictionary API which contains information on plurals. You can read more about that method here. You'll need to parse a JSON object to find the appropriate items and this method tends to be a little bit slow.

The other method that comes to mind is to use aspell, or one of its relatives. I'm not sure how accurate the dictionaries are for languages other than English but I've used aspell to expand words into their various forms.

I know this is not the most helpful answer, but hopefully it gives you a general direction to search in.