5 way(s) to pluralize
Programmers are lazy or maybe they just can’t count. More and more I see sites out there with dumb things like “1 page(s)” or worse just “1 pages”. I could find lots of Yahoo pages with this kind of stuff, but that’s beside the point – I’m providing help for lazy programmers who can’t count.
The rule is simple. Words are plural for every value except 1. But now you don’t even have to remember that. Just cut and paste one of the functions below.
To use any of them, call them with the count of things and then the singular form of the word and the plural form. For the truly lazy, if you can pluralize your word by just appending an “s” to the end, the last argument is optional. But don’t do this with words like “quantity”, duh.
PHP Example:
I know 5 lazy < ?php echo pluralize(5, 'programmer', 'programmers'); ?>. I know 5 lazy < ?php echo pluralize(5, 'programmer'); ?>. Their < ?php echo pluralize(1, 'mommy', 'mommies') ?> didn't teach them to count.
Spits out:
I know 5 lazy programmers. I know 5 lazy programmers. Their mommy didn't teach them to count.
Here’s the code to cut ‘n paste:
PHP
function pluralize($count, $singular, $plural = false)
{
if (!$plural) $plural = $singular . 's';
return ($count == 1 ? $singular : $plural) ;
}
I’m surprised this isn’t already Yet Another PHP Function
Perl
sub pluralize()
{
my ($count, $singular, $plural) = @_;
$plural = $singular . 's' if (!$plural) ;
return $singular if ($count == 1);
return $plural;
}
I’m sure someone will come along and rewrite this in one illegible line. Of course there’s already a CPAN module for this if you’re afraid of writing any code yourself.
Java
public static String pluralize(int count, String singular)
{
return pluralize(count, singular, singular.concat('s'));
}
public static String pluralize(int count, String singular, String plural)
{
return (count == 1 ? singular : plural)
}
Boy I wish Java had optional arguments.
JavaScript
String.prototype.pluralize = function(count, plural)
{
if (plural == null)
plural = this + 's';
return (count == 1 ? this : plural)
}
OO JavaScript is cool
Please contribute your favorite oddball language. It’s too early in the morning for me to write in C and I get hives anytime I look at Visual Basic.
April 22nd, 2005 at 11:50 am
Oracle PL/SQL
CREATE OR REPLACE FUNCTION pluralize (
lcount IN NUMBER,
lsingular IN VARCHAR2,
lplural IN VARCHAR2
)
RETURN VARCHAR2
IS
lpluralstring VARCHAR2 (100);
BEGIN
IF lcount = 1 THEN
lpluralstring := lsingular;
ELSIF lcount != 1 AND lplural IS NULL THEN
lpluralstring := lsingular || ‘s’;
ELSIF lcount != 1 AND lplural IS NOT NULL THEN
lpluralstring := lplural;
END IF;
RETURN lpluralstring;
END;
/
April 22nd, 2005 at 1:24 pm
For how to handle singular/plural fom not just with nouns, but adjectives, verbs, etc. check out Conway’s paper on the topic:
http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
and his implementation:
http://search.cpan.org/~dconway/Lingua-EN-Inflect-1.88/lib/Lingua/EN/Inflect.pm
-Bill
April 22nd, 2005 at 2:25 pm
Shouldn’t it be “Their mommies didn’t teach them to count” since there are 5 of them? grin
April 22nd, 2005 at 4:28 pm
Well, yeah, and mommy should be singular. Well, the engineer being discussed is named “Heather”. I’d also note that Mommy is a nice example of where adding ‘s’ just isn’t sufficient. So are “moose”,”hippopotamus”, and the singular of “data”*, but those are (another|different) issue(s).
*(yes, I know that New Collegiate allows both “data” for singular and plural like they allow “hippopotamuses”, but the real versions are “datum” for singular and “hippopotami” for plural. Of those words, not the same thing. Dang, Where’s that dangling modifier CPAN module again?)
April 22nd, 2005 at 4:40 pm
The number of mommies is based on whether or not the programmers are siblings, not how many programmers there are.
April 22nd, 2005 at 8:56 pm
Ok…
sub pluralize {$_[0]==1?$_[1]:$_[2]?$_[2]:$_[1].’s'}
May 20th, 2005 at 8:59 am
In your example, if the number of programmers is one (the NY Times Style guide suggests spelling out the first nine cardinal and ordinal numbers in ordinary copy), the the last sentence should read: “His mommy didn’t teach him to count.”
November 6th, 2005 at 6:30 pm
Here’s it in BASIC (sorry, it’s not a sub, whaddya gonna do about it?):
10 INPUT “What is the number?”; count$
20 INPUT “What is the singular?”; singular$
30 INPUT “What is the plural? (leave blank for laziness)”; plural$
40 IF plural$ = “” THEN GOTO 70
50 IF count$ = 1 THEN GOTO 100
60 PRINT “There are “; count$; plural$; “!”
65 EXIT
70 plural$ = singular$; “s”
71 GOTO 50
100 PRINT “There is “; count$; singular$; “!”
110 EXIT
December 10th, 2005 at 9:39 am
How to pluralize using PHP
This is one of those technical nitpick things. Check out the screenshot below of the new Yahoo Answers and see if you can spot the error.
The title of this thread kind of gives it away. Note the “1 Answers” should actually read “…
December 22nd, 2005 at 12:41 pm
I take it there is no easy (reliable) way in PHP to:
a | Make singular words plural
b | Make plural words singular
For example, simply removing the S’s isn’t a good option since a word like grass would become gras. Adding an S is not ideal as fish becomes fishs… I guess if there is no vowel on the end, “es” could be added. Is there a function out there that anyone knows of that handles the various possibilities? If so, I would be very grateful if someone could email the link to me.
Many thanks.
August 14th, 2006 at 11:53 am
I wrote this function today after not finding one in the PHP manual. I tried a few test cases but I’m no grammar expert so I may have missed some conditions. It’s really hard to tell how to pluralize words ending in ‘o’, so a lot of those might not work well. Also, for the words in the $irregulars array, the comparison is case sensitive, so you may have to add your own formatting in there. It’s not perfect, but it’s not bad for 10 mintues. If you see any more irregulars I am missing please post them in response.
function pluralize($string)
{
$irregulars = array(‘man’ => ‘men’, ‘woman’ => ‘women’, ‘fungus’ => ‘fungi’, ‘thief’ => ‘thieves’, ‘species’ => ‘species’,
‘medium’ => ‘media’,'person’ => ‘people’, ‘echo’ => ‘echoes’, ‘hero’ => ‘heroes’, ‘potato’ => ‘potatoes’,
‘veto’ => ‘vetoes’, ‘auto’ => ‘autos’, ‘memo’ => ‘memos’, ‘pimento’ => ‘pimentos’, ‘pro’ => ‘pros’);
$es = array(‘s’, ‘z’, ‘ch’, ‘sh’, ‘x’);
$last_letter = $string{strlen($string) – 1};
if(array_key_exists($string, $irregulars))
{
return $irregulars[$string];
}
else if($last_letter == ‘y’)
{
if($string{strlen($string) – 2} == ‘e’)
return substr($string, 0, strlen($string) – 2) . ‘ies’;
else
return substr($string, 0, strlen($string) – 1) . ‘ies’;
}
else if(in_array(substr($string,0,-2),$es) || in_array($last_letter, $es))
{
return $string . ‘es’;
}
else
{
return $string . ‘s’;
}
}
March 17th, 2007 at 6:33 pm
Shamelessly lifted from the rails inflector class:
http://paulosman.com/node/23
April 5th, 2007 at 1:17 pm
The same as yours, in one line, but yes a little less understandable:
function pluralize($count, $singular, $plural = false)
{
return ($count == 1 ? $singular : ($plural ? $plural : $singular . ‘s’)) ;
}
November 17th, 2009 at 10:34 am
# File actionpack/lib/action_view/helpers/text_helper.rb, line 183
def pluralize(count, singular, plural = nil)
“#{count || 0} ” + ((count == 1 || count == ’1′) ? singular : (plural || singular.pluralize))
end
Welcome to the Rails community.
April 13th, 2011 at 1:19 am
Love Rails
pluralize(1, ‘person’)
# => 1 person
pluralize(2, ‘person’)
# => 2 people
pluralize(3, ‘person’, ‘users’)
# => 3 users
pluralize(0, ‘person’)
# => 0 people
July 9th, 2011 at 9:06 pm
Thanks for this, exactly what I was looking for and the code works perfectly!
December 6th, 2011 at 11:41 pm
Bit of inline PHP, which also pluralizes in the case of 0 (your search returned 0 results).
your search returned result1?’s':”;?>
December 6th, 2011 at 11:45 pm
Bit of inline PHP, which also pluralizes in the case of 0 (your search returned 0 results).
<p>your search returned <?php echo count($search_results);?> result<?php count($search_results)==0||count($search_results)>1?'s':'';?>