Posted By

saturngod on 03/11/11


Tagged


Versions (?)

php-pluralize


 / Published in: PHP
 

  1. // Thanks to http://www.eval.ca/articles/php-pluralize (MIT license)
  2. // http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/inflections.rb (MIT license)
  3. // http://www.fortunecity.com/bally/durrus/153/gramch13.html
  4. // http://www2.gsu.edu/~wwwesl/egw/crump.htm
  5. //
  6. // Changes (12/17/07)
  7. // Major changes
  8. // --
  9. // Fixed irregular noun algorithm to use regular expressions just like the original Ruby source.
  10. // (this allows for things like fireman -> firemen
  11. // Fixed the order of the singular array, which was backwards.
  12. //
  13. // Minor changes
  14. // --
  15. // Removed incorrect pluralization rule for /([^aeiouy]|qu)ies$/ => $1y
  16. // Expanded on the list of exceptions for *o -> *oes, and removed rule for buffalo -> buffaloes
  17. // Removed dangerous singularization rule for /([^f])ves$/ => $1fe
  18. // Added more specific rules for singularizing lives, wives, knives, sheaves, loaves, and leaves and thieves
  19. // Added exception to /(us)es$/ => $1 rule for houses => house and blouses => blouse
  20. // Added excpetions for feet, geese and teeth
  21. // Added rule for deer -> deer
  22.  
  23. // Changes:
  24. // Removed rule for virus -> viri
  25. // Added rule for potato -> potatoes
  26. // Added rule for *us -> *uses
  27.  
  28. class Inflect
  29. {
  30. static $plural = array(
  31. '/(quiz)$/i' => "$1zes",
  32. '/^(ox)$/i' => "$1en",
  33. '/([m|l])ouse$/i' => "$1ice",
  34. '/(matr|vert|ind)ix|ex$/i' => "$1ices",
  35. '/(x|ch|ss|sh)$/i' => "$1es",
  36. '/([^aeiouy]|qu)y$/i' => "$1ies",
  37. '/(hive)$/i' => "$1s",
  38. '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
  39. '/(shea|lea|loa|thie)f$/i' => "$1ves",
  40. '/sis$/i' => "ses",
  41. '/([ti])um$/i' => "$1a",
  42. '/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
  43. '/(bu)s$/i' => "$1ses",
  44. '/(alias)$/i' => "$1es",
  45. '/(octop)us$/i' => "$1i",
  46. '/(ax|test)is$/i' => "$1es",
  47. '/(us)$/i' => "$1es",
  48. '/s$/i' => "s",
  49. '/$/' => "s"
  50. );
  51.  
  52. static $singular = array(
  53. '/(quiz)zes$/i' => "$1",
  54. '/(matr)ices$/i' => "$1ix",
  55. '/(vert|ind)ices$/i' => "$1ex",
  56. '/^(ox)en$/i' => "$1",
  57. '/(alias)es$/i' => "$1",
  58. '/(octop|vir)i$/i' => "$1us",
  59. '/(cris|ax|test)es$/i' => "$1is",
  60. '/(shoe)s$/i' => "$1",
  61. '/(o)es$/i' => "$1",
  62. '/(bus)es$/i' => "$1",
  63. '/([m|l])ice$/i' => "$1ouse",
  64. '/(x|ch|ss|sh)es$/i' => "$1",
  65. '/(m)ovies$/i' => "$1ovie",
  66. '/(s)eries$/i' => "$1eries",
  67. '/([^aeiouy]|qu)ies$/i' => "$1y",
  68. '/([lr])ves$/i' => "$1f",
  69. '/(tive)s$/i' => "$1",
  70. '/(hive)s$/i' => "$1",
  71. '/(li|wi|kni)ves$/i' => "$1fe",
  72. '/(shea|loa|lea|thie)ves$/i'=> "$1f",
  73. '/(^analy)ses$/i' => "$1sis",
  74. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
  75. '/([ti])a$/i' => "$1um",
  76. '/(n)ews$/i' => "$1ews",
  77. '/(h|bl)ouses$/i' => "$1ouse",
  78. '/(corpse)s$/i' => "$1",
  79. '/(us)es$/i' => "$1",
  80. '/s$/i' => ""
  81. );
  82.  
  83. static $irregular = array(
  84. 'move' => 'moves',
  85. 'foot' => 'feet',
  86. 'goose' => 'geese',
  87. 'sex' => 'sexes',
  88. 'child' => 'children',
  89. 'man' => 'men',
  90. 'tooth' => 'teeth',
  91. 'person' => 'people'
  92. );
  93.  
  94. static $uncountable = array(
  95. 'sheep',
  96. 'fish',
  97. 'deer',
  98. 'series',
  99. 'species',
  100. 'money',
  101. 'rice',
  102. 'information',
  103. 'equipment'
  104. );
  105.  
  106. public static function pluralize( $string )
  107. {
  108. // save some time in the case that singular and plural are the same
  109. if ( in_array( strtolower( $string ), self::$uncountable ) )
  110. return $string;
  111.  
  112. // check for irregular singular forms
  113. foreach ( self::$irregular as $pattern => $result )
  114. {
  115. $pattern = '/' . $pattern . '$/i';
  116.  
  117. if ( preg_match( $pattern, $string ) )
  118. return preg_replace( $pattern, $result, $string);
  119. }
  120.  
  121. // check for matches using regular expressions
  122. foreach ( self::$plural as $pattern => $result )
  123. {
  124. if ( preg_match( $pattern, $string ) )
  125. return preg_replace( $pattern, $result, $string );
  126. }
  127.  
  128. return $string;
  129. }
  130.  
  131. public static function singularize( $string )
  132. {
  133. // save some time in the case that singular and plural are the same
  134. if ( in_array( strtolower( $string ), self::$uncountable ) )
  135. return $string;
  136.  
  137. // check for irregular plural forms
  138. foreach ( self::$irregular as $result => $pattern )
  139. {
  140. $pattern = '/' . $pattern . '$/i';
  141.  
  142. if ( preg_match( $pattern, $string ) )
  143. return preg_replace( $pattern, $result, $string);
  144. }
  145.  
  146. // check for matches using regular expressions
  147. foreach ( self::$singular as $pattern => $result )
  148. {
  149. if ( preg_match( $pattern, $string ) )
  150. return preg_replace( $pattern, $result, $string );
  151. }
  152.  
  153. return $string;
  154. }
  155.  
  156. public static function pluralize_if($count, $string)
  157. {
  158. if ($count == 1)
  159. return "1 $string";
  160. else
  161. return $count . " " . self::pluralize($string);
  162. }
  163. }

Report this snippet  

You need to login to post a comment.