Skip to content

Instantly share code, notes, and snippets.

@tbrianjones
Last active February 21, 2024 21:28
Show Gist options
  • Star 82 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save tbrianjones/ba0460cc1d55f357e00b to your computer and use it in GitHub Desktop.
Save tbrianjones/ba0460cc1d55f357e00b to your computer and use it in GitHub Desktop.
A PHP Class for converting English words between Singular and Plural.
<?php
// original source: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
/*
The MIT License (MIT)
Copyright (c) 2015
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// ORIGINAL NOTES
//
// Thanks to http://www.eval.ca/articles/php-pluralize (MIT license)
// http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/inflections.rb (MIT license)
// http://www.fortunecity.com/bally/durrus/153/gramch13.html
// http://www2.gsu.edu/~wwwesl/egw/crump.htm
//
// Changes (12/17/07)
// Major changes
// --
// Fixed irregular noun algorithm to use regular expressions just like the original Ruby source.
// (this allows for things like fireman -> firemen
// Fixed the order of the singular array, which was backwards.
//
// Minor changes
// --
// Removed incorrect pluralization rule for /([^aeiouy]|qu)ies$/ => $1y
// Expanded on the list of exceptions for *o -> *oes, and removed rule for buffalo -> buffaloes
// Removed dangerous singularization rule for /([^f])ves$/ => $1fe
// Added more specific rules for singularizing lives, wives, knives, sheaves, loaves, and leaves and thieves
// Added exception to /(us)es$/ => $1 rule for houses => house and blouses => blouse
// Added excpetions for feet, geese and teeth
// Added rule for deer -> deer
// Changes:
// Removed rule for virus -> viri
// Added rule for potato -> potatoes
// Added rule for *us -> *uses
class Inflect
{
static $plural = array(
'/(quiz)$/i' => "$1zes",
'/^(ox)$/i' => "$1en",
'/([m|l])ouse$/i' => "$1ice",
'/(matr|vert|ind)ix|ex$/i' => "$1ices",
'/(x|ch|ss|sh)$/i' => "$1es",
'/([^aeiouy]|qu)y$/i' => "$1ies",
'/(hive)$/i' => "$1s",
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
'/(shea|lea|loa|thie)f$/i' => "$1ves",
'/sis$/i' => "ses",
'/([ti])um$/i' => "$1a",
'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
'/(bu)s$/i' => "$1ses",
'/(alias)$/i' => "$1es",
'/(octop)us$/i' => "$1i",
'/(ax|test)is$/i' => "$1es",
'/(us)$/i' => "$1es",
'/s$/i' => "s",
'/$/' => "s"
);
static $singular = array(
'/(quiz)zes$/i' => "$1",
'/(matr)ices$/i' => "$1ix",
'/(vert|ind)ices$/i' => "$1ex",
'/^(ox)en$/i' => "$1",
'/(alias)es$/i' => "$1",
'/(octop|vir)i$/i' => "$1us",
'/(cris|ax|test)es$/i' => "$1is",
'/(shoe)s$/i' => "$1",
'/(o)es$/i' => "$1",
'/(bus)es$/i' => "$1",
'/([m|l])ice$/i' => "$1ouse",
'/(x|ch|ss|sh)es$/i' => "$1",
'/(m)ovies$/i' => "$1ovie",
'/(s)eries$/i' => "$1eries",
'/([^aeiouy]|qu)ies$/i' => "$1y",
'/([lr])ves$/i' => "$1f",
'/(tive)s$/i' => "$1",
'/(hive)s$/i' => "$1",
'/(li|wi|kni)ves$/i' => "$1fe",
'/(shea|loa|lea|thie)ves$/i'=> "$1f",
'/(^analy)ses$/i' => "$1sis",
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
'/([ti])a$/i' => "$1um",
'/(n)ews$/i' => "$1ews",
'/(h|bl)ouses$/i' => "$1ouse",
'/(corpse)s$/i' => "$1",
'/(us)es$/i' => "$1",
'/s$/i' => ""
);
static $irregular = array(
'move' => 'moves',
'foot' => 'feet',
'goose' => 'geese',
'sex' => 'sexes',
'child' => 'children',
'man' => 'men',
'tooth' => 'teeth',
'person' => 'people',
'valve' => 'valves'
);
static $uncountable = array(
'sheep',
'fish',
'deer',
'series',
'species',
'money',
'rice',
'information',
'equipment'
);
public static function pluralize( $string )
{
// save some time in the case that singular and plural are the same
if ( in_array( strtolower( $string ), self::$uncountable ) )
return $string;
// check for irregular singular forms
foreach ( self::$irregular as $pattern => $result )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for matches using regular expressions
foreach ( self::$plural as $pattern => $result )
{
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string );
}
return $string;
}
public static function singularize( $string )
{
// save some time in the case that singular and plural are the same
if ( in_array( strtolower( $string ), self::$uncountable ) )
return $string;
// check for irregular plural forms
foreach ( self::$irregular as $result => $pattern )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for matches using regular expressions
foreach ( self::$singular as $pattern => $result )
{
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string );
}
return $string;
}
public static function pluralize_if($count, $string)
{
if ($count == 1)
return "1 $string";
else
return $count . " " . self::pluralize($string);
}
}
?>
@tbrianjones
Copy link
Author

Added Valve to the irregular list.

@codebird
Copy link

codebird commented Nov 3, 2015

Hello,
I found out that business is being transformed to busines, so I had to add it to the singular array...

I added:

'/(business)$/i' => "$1"

to the singular array

@Cipa
Copy link

Cipa commented May 3, 2016

Can there be a rule for wellness, fitness, wellness etc?

@nithi2023
Copy link

Common uncountable nouns list is here: http://ieltsliz.com/uncountable-nouns-word-list/

@atswann
Copy link

atswann commented May 10, 2017

One more adjustment please. return the same word if the word is already plural or singular.

eg. pluralize('men'); // returns 'men'
eg singularize('bag'); // returns 'bag'

@arvindpdmn
Copy link

Wrong plural: mongoose gives mongeese.
Right singular: mongooses gives mongoose.

@zulkarnain-shah
Copy link

zulkarnain-shah commented Jul 4, 2017

It still doesn't cover the whole English language, does it?

@yarcowang
Copy link

...Em... I think ask America's President and England Queen to make all English words _s as plural / no _s as single will be more simple then do it fully by computer... As a Chinese, I insist on it 😄 .

@effone
Copy link

effone commented Feb 26, 2018

Appreciate the effort but there are certain basic plural literature rules (other than exception list) which I expect a class like this to follow first.:

General Plural Literature Suffix Rules:

  • 'ies' rule (ends in a consonant + y : baby/lady)
  • 'ves' rule (ends in f or fe : leaf/knife) --- roof : rooves (correct but old english, roofs is ok).
  • 'es' rule 1 (ends in a consonant + o : volcano/mango)
  • 'es' rule 2 (ends in ch, sh, s, ss, x, z : match/dish/bus/glass/fox/buzz)
  • 's' rule 1 (ends in a vowel + y or o : boy/radio) [Can be ignored in total]
  • 's' rule 2 (ends in other than above : cat/ball)

I have a simple function gist which may outline the same.

@xtempore
Copy link

Suggestion:
Add "police" to uncountable, otherwise Inflect::singularize('police') returns "polouse".
Could also solve by adjusting...
'/^([m|l])ice$/i' => "$1ouse",
.. but that would rule out things like "doormice" => "doormouse", "headlice" => "headlouse".

@AkinOlawale
Copy link

"Statuses" when converted to singular returns "Statu". How do i fix this?

@AkinOlawale
Copy link

Also "Addresses" returns "Addres"

@djcowan
Copy link

djcowan commented Apr 27, 2020

Elegant solution. Thankingses

@rachid-aachich
Copy link

Please add all those words:

Furniture
Information
Knowledge
Jewelry
Homework
Marketing
Livestock
Education
Courage
Bravery
Luck
Cowardice
Greed
Clarity
Honesty
Evidence
Insurance
Butter
Love
News
Curiosity
Satisfaction
Work
Mud
Weather
Racism
Sexism
Patriotism
Chaos
Scenery
Help
Advice
Water
Fun
Wisdom
Silence
Sugar
Coal
Spelling
Money

@vikingjs
Copy link

"octopi" is not a correct plural for octopus. It never has been, ever, in any language. Octopus is Greek, and "-i" is a Latin plural form. The correct works is "octopuses", or if you really insist on false pedantry, "octopodes".

@xtempore
Copy link

"octopi" is not a correct plural for octopus. It never has been, ever, in any language. Octopus is Greek, and "-i" is a Latin plural form. The correct works is "octopuses", or if you really insist on false pedantry, "octopodes".

There is absolutely nothing wrong with "octopi". It was the earliest used plural and the most common for a long time. It may have originally come from a Greek word, but:

"While octopus may ultimately come from Greek it had a stay in New Latin before arriving here (in English)"

https://www.merriam-webster.com/words-at-play/the-many-plurals-of-octopus-octopi-octopuses-octopodes

@rachid-aachich
Copy link

Here's a bit more extensive class with more words taken into account that i created, feel free to add or suggest more:
https://github.com/rachid-aachich/php-pluralizer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment