The realization of the method of English word single plural conversion in the program

Source: Internet
Author: User

Here is the implementation of PHP, in fact, in CSharp or Java, the implementation of the same approach.

/** * Wirror:pluralize A String * @param $string/function pluralize ($string) {$plural = Array ('/(Quiz) $/i ', "$1zes"), Array ('/^ (ox) $/i ', "$1en"), Array ('/([m|l]) ouse$/i ', "$1ice"), Array ('/(Matr|vert|ind) ix|ex$/i ', "$1ices" ), Array ('/(X|CH|SS|SH) $/i ', ' $1es '), Array ('/([^aeiouy]|qu) y$/i ', ' $1ies '), Array ('/([^aeiouy]|qu) ies$/i ', ' $1y '), Array ('/(hive) $/i ', "$1s"), Array ('/(?:( [^f]) fe| ([LR] f) $/i ', "$1$2ves"), Array ('/sis$/i ', "ses"), Array ('/[[Ti]] um$/i ', "$1a"), Array ('/(Buffal|tomat) o$/i ', "$1oes "), Array (' bu) s$/i '," $1ses "), Array ('/(alias|status) $/i ', ' $1es '), Array (' Octop|vir ' us$/i ', ' $1i '), Array ('/( ax|test) is$/i ', "$1es"), Array ('/s$/i ', "s"), Array ('/$/', "s")); $singular = Array (Array ("/s$/", ""), Array ("/(n) ews$/", "$1ews"), Array ("[Ti]) a$/", "$1um"), Array ("/((a) naly| ( b) a| (d) iagno| (p) arenthe| (p) rogno| (s) ynop| (t) He) ses$/"," $1$2sis "), Array ("/(^analy) ses$/"," $1sis "), Array (" [^f]) ves$/"," $1fe "), Array ("/(Hive) s$/","$"), Array ("/(tive) s$/", "$"), Array ("/[[LR]) ves$/", "$1f"), Array ("/[^aeiouy]|qu) ies$/", "$1y"), Array ("/(s) eries$/"," $1eries "), Array ("/(m) ovies$/"," $1ovie "), Array ("/(X|CH|SS|SH) es$/"," $ "), Array (" [M|l]) ice$/"," $ 1ouse "), Array ("/(BUS) es$/"," $ "), Array ("/(O) es$/"," $ "), Array ("/(shoe) s$/"," $ "), Array ("/(Cris|ax|test) es$/"," $1is "), Array ("/[[Octop|vir]) i$/"," $1us "), Array ("/(Alias|status) es$/"," $ "), Array ("/^ (ox) en/"," $ "), Array ("/( Vert|ind) ices$/"," $1ex "), Array ("/(MATR) ices$/"," $1ix "), Array ("/(Quiz) zes$/"," $ "); $irregular = Array (' Move ', ' moves '), array (' sex ', ' sexes '), array (' child ', ' children '), Array (' Mans ', ' Men ') , array (' person ', ' people ')); $uncountable = Array (' sheep ', ' Fish ', ' series ', ' species ', ' money ', ' rice ', ' information ', ' equipment '); Save some time in the "case" singular and plural are the same if (In_array ($string), $uncountable) return $string; Check for irregular singular forms foreach ($iRregular as $noun) {if (Strtolower ($string) = = $noun [0]) return $noun [1];}//Check for matches using regular Expre Ssions foreach ($plural as $pattern) {if (Preg_match ($pattern [0], $string)) return preg_replace ($pattern [0], $patt ERN[1], $string); return $string; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.