-
-
Save tbrianjones/ba0460cc1d55f357e00b to your computer and use it in GitHub Desktop.
| <?php | |
| // 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); | |
| } | |
| } | |
| ?> |
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
Can there be a rule for wellness, fitness, wellness etc?
Common uncountable nouns list is here: http://ieltsliz.com/uncountable-nouns-word-list/
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'
Wrong plural: mongoose gives mongeese.
Right singular: mongooses gives mongoose.
It still doesn't cover the whole English language, does it?
...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 😄 .
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.
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".
"Statuses" when converted to singular returns "Statu". How do i fix this?
Also "Addresses" returns "Addres"
Elegant solution. Thankingses
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
plenty of words and introductory phrases for essays and data representation: https://crack-ielts.com/2021/02/06/ielts-voacbulary-and-magical-words-for-writing-task-2/
https://crack-ielts.com/2021/02/02/punch-lines-for-ielts-writing-task-2/
https://crack-ielts.com/2021/01/29/vocabulary-for-writing-task-1-in-ielts/
"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".
"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
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
Added Valve to the irregular list.