homepage Welcome to WebmasterWorld Guest from 50.19.172.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Singular/Plural Function
For english
neophyte

10+ Year Member



 
Msg#: 3281240 posted 2:39 am on Mar 14, 2007 (gmt 0)

Hello All -

I've got a client (an ex English professor) who is absoluty a demon when it comes to "singular/plural" text display on his site.

"It's never '1 adults', it's '1 adult'" and so on. He's right of course... and it just makes the output look more professional overall.

So far, I've tackled this issue with various conditional statements used as necessary, like:

$prevAdults == 0 ¦¦ $prevAdults > 1? $adultForm .= ' Adults' : $adultForm .= ' Adult';

This "solution" is getting somewhat time consuming to implement, and I thought it would be a perfect candidate for a function.

After researching the (American) English rules - given the many possible variations depending upon how and where a particular word is used, this has become more of a daunting nut to crack as I had initially expected.

Does anyone know of a pre-rolled solution to this that I can simply plug in an use?

Neophyte

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3281240 posted 12:29 pm on Mar 14, 2007 (gmt 0)

Although there may be different rules for making a noun plural, you could also "tell" the function what to use for that particular case. So maybe something like this:

function noun($num,$word,$plural_suffix) {
return ($num == 1)?$word:$word.$plural_suffix;
}

Examples:

echo noun(4,'bus','es'); #echos 'buses'
echo noun(1,'bus','es'); #echos 'bus'
echo noun(3,'cat','s'); #echos 'cats'

Hope this helps :)

PSWorx

10+ Year Member



 
Msg#: 3281240 posted 3:32 pm on Mar 14, 2007 (gmt 0)

Ive just been plodding around the house and came up with a great solution but the chap in the last post already got it.

Deffo the best solution for the situation!

joelgreen

5+ Year Member



 
Msg#: 3281240 posted 4:02 pm on Mar 14, 2007 (gmt 0)

This will not work for some words like mouse (plural mice), index (indices), etc.

So you could create array of words like this

$plural= array(
'bus' => 'buses',
'cat' => 'cats',
'mouse' => 'mice',
'index' => 'indices',
...
);

function noun($num,$word) {
global $plural;
return ($num == 1)? $word : $plural[$word];
}

echo noun(4,'bus'); #echos 'buses'
echo noun(1,'bus'); #echos 'bus'
echo noun(3,'mouse'); #echos 'mice'

joelgreen

5+ Year Member



 
Msg#: 3281240 posted 4:08 pm on Mar 14, 2007 (gmt 0)

You could also use these rules:

$plural_rules = array( '/(x¦ch¦ss¦sh)$/' => '\1es', # search, switch, fix, box, process, address
'/series$/' => '\1series',
'/([^aeiouy]¦qu)ies$/' => '\1y',
'/([^aeiouy]¦qu)y$/' => '\1ies', # query, ability, agency
'/(?:([^f])fe¦([lr])f)$/' => '\1\2ves', # half, safe, wife
'/sis$/' => 'ses', # basis, diagnosis
'/([ti])um$/' => '\1a', # datum, medium
'/person$/' => 'people', # person, salesperson
'/man$/' => 'men', # man, woman, spokesman
'/child$/' => 'children', # child
'/(.*)status$/' => '\1statuses',
'/s$/' => 's', # no change (compatibility)
'/$/' => 's'
)

PSWorx

10+ Year Member



 
Msg#: 3281240 posted 4:30 pm on Mar 14, 2007 (gmt 0)

Couldnt you just use the first function eelixduppy posted, with an associative array of words that wont produce the plurar expected with most other words i.e.

mouse = mice

So with the existing function that has the associative array in there (or stored sepereate in global var) to itterate through the array until the match is found depending on parameters fead to the function i.e. feed NULL as $plurar_suffix:

eelixduppy's code with modifications:

function noun($num,$word,$plural_suffix) {

//Array of awkward words - could be stored in a config file and made global by $_SERVER['wordArray']
$wordArray = array (
'mouse' => 'mice',
'index' => 'indeces'
);

//For loop to check for a match - use < count here as we start on 0 and the
//array count will start from 1, if we use <= then we perform an itteration for no reason at the end
for($x=0;$x<count($wordArray);$x++){

//If statement to check for awkward words - only if num > (greater than) 1
//think this is right havent tested it - concept is there
//Possibly use!empty() for checking process
if( $num>1 && isset($wordArray[$word]) ){

$word=$wordArray[$word];
return $word;
//Breaks out of the loop as we found a match
break;

//No awkward word match so process as normal
}else{

return ($num == 1)?$word:$word.$plural_suffix;

}

}

}

Examples:

echo noun(4,'bus','es'); #echos 'buses'
echo noun(1,'bus','es'); #echos 'bus'
echo noun(3,'mouse',NULL); #echos 'mice'
echo noun(1,'mouse',NULL); #echos 'mouse'

NOTES: Just looking over it a little in efficient as it will loop through the array till it finds a match and if not then it will perform the last operation using the plurar parameter.

AFTERTHOUGHT: You could use the $plural_suffix to trigger the type of operation in the function i.e. if plural_suffix == 0/FALSE/NULL then check and replace via the array, otherwise check and replace via other method?!?

ADDITIONAL AFTERTHOIGHT: Just reading joes post there thats great but you would need an array for all possible words :-s eek, if what i wrote works then all you need to do is find out the awkward words populate the array, preferably global in a config.inc.php file and your done, other words taken care of using the $plural_appendix parameter.

Spank me if im wrong...

[edited by: PSWorx at 4:38 pm (utc) on Mar. 14, 2007]

PSWorx

10+ Year Member



 
Msg#: 3281240 posted 4:41 pm on Mar 14, 2007 (gmt 0)

Just tested the modifications, works a treat, hope it suits your needs.

joelgreen

5+ Year Member



 
Msg#: 3281240 posted 5:02 pm on Mar 14, 2007 (gmt 0)

With those rules I posted above you will not have to pass suffixes for regular words. Script will take care of them.

something like this (not tested):

function getplural($word) {
$result = $word;
foreach($rules as $pattern=>$repl) {
$result = preg_replace ($pattern, $repl, $word);
if ($result!= $word) break; // leave if plural found
}
return $result;
}

echo getplural('search') would return "searches".

But that array will have to be populated with all non-regular words anyway :(

PSWorx

10+ Year Member



 
Msg#: 3281240 posted 5:12 pm on Mar 14, 2007 (gmt 0)

Well either way looks like an array needs populating just a case of whats best for the guy whos gonna use it.

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3281240 posted 7:02 pm on Mar 14, 2007 (gmt 0)

Certainly coming up with all possible values for the plural noun could prove to be difficult. And what do you do in case you cannot find a match? I'd say it would be efficient enough to just pass the plural as a parameter of the function:


function noun($num,$singular,$plural) {
return ($num == 1)?$singular:$plural;
}

joelgreen

5+ Year Member



 
Msg#: 3281240 posted 7:54 pm on Mar 14, 2007 (gmt 0)

this will be the most stable solution in case of multilingual site

justageek

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3281240 posted 8:06 pm on Mar 14, 2007 (gmt 0)

Might be an overkill for this app but I use wordnet data sometimes. That'll find the plural/singular (and a whole lot more) easy enough. Just download the data, populate a few tables for your needs and have fun.

JAG

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3281240 posted 11:52 pm on Mar 14, 2007 (gmt 0)

That is indeed another valid solution. It does seem to be a little overkill, unless you don't know the actual word that you are going to use, in which case you'd have to look it up. If you only need a specific word to change between singular and plural it wouldn't be necessary to look up the plural form, and you could implement a simple replacement function like I have shown above. :)

neophyte

10+ Year Member



 
Msg#: 3281240 posted 3:06 am on Mar 15, 2007 (gmt 0)

eelixduppy, PSWorx, joelgreen -

Wow, of all the posts I've done on WebmasterWorld I never expacted so much action on this one!

Thank you all for weighing in - all interesting solutions.

From a "maintaining associative arrays" standpoint, I'm leaning toward exlixduppy's final solution - without the suffix but just the complete plural so I can stay out of the fire with "mice", "indices", etc.

>>>>>>>

function noun($num, $singular, $plural)
{
return ($num == 1)? $singular : $plural;
}

echo noun(0, 'mouse', 'mice');

>>>>>>>

Thank you all (as always!) for your guidance!

Neophyte

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved