Inflection Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * @package Classes
  5.  * @author Sho Kuwamoto <[email protected]>
  6.  * @link http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/ Source
  7.  */
  8.  
  9. class Inflect
  10. {
  11. /**
  12. * The plural form of a word
  13. * @var array
  14. */
  15. static $plural = array(
  16. '/(quiz)$/i' => '$1zes',
  17. '/^(ox)$/i' => '$1en',
  18. '/([m|l])ouse$/i' => '$1ice',
  19. '/(matr|vert|ind)ix|ex$/i' => '$1ices',
  20. '/(x|ch|ss|sh)$/i' => '$1es',
  21. '/([^aeiouy]|qu)y$/i' => '$1ies',
  22. '/(hive)$/i' => '$1s',
  23. '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves',
  24. '/(shea|lea|loa|thie)f$/i' => '$1ves',
  25. '/sis$/i' => 'ses',
  26. '/([ti])um$/i' => '$1a',
  27. '/(tomat|potat|ech|her|vet)o$/i'=> '$1oes',
  28. '/(bu)s$/i' => '$1ses',
  29. '/(alias)$/i' => '$1es',
  30. '/(octop)us$/i' => '$1i',
  31. '/(ax|test)is$/i' => '$1es',
  32. '/(us)$/i' => '$1es',
  33. '/s$/i' => 's',
  34. '/$/' => 's'
  35. );
  36.  
  37. /**
  38. * The singular form of a word
  39. * @var array
  40. */
  41. static $singular = array(
  42. '/(quiz)zes$/i' => '$1',
  43. '/(matr)ices$/i' => '$1ix',
  44. '/(vert|ind)ices$/i' => '$1ex',
  45. '/^(ox)en$/i' => '$1',
  46. '/(alias)es$/i' => '$1',
  47. '/(octop|vir)i$/i' => '$1us',
  48. '/(cris|ax|test)es$/i' => '$1is',
  49. '/(shoe)s$/i' => '$1',
  50. '/(o)es$/i' => '$1',
  51. '/(bus)es$/i' => '$1',
  52. '/([m|l])ice$/i' => '$1ouse',
  53. '/(x|ch|ss|sh)es$/i' => '$1',
  54. '/(m)ovies$/i' => '$1ovie',
  55. '/(s)eries$/i' => '$1eries',
  56. '/([^aeiouy]|qu)ies$/i' => '$1y',
  57. '/([lr])ves$/i' => '$1f',
  58. '/(tive)s$/i' => '$1',
  59. '/(hive)s$/i' => '$1',
  60. '/(li|wi|kni)ves$/i' => '$1fe',
  61. '/(shea|loa|lea|thie)ves$/i'=> '$1f',
  62. '/(^analy)ses$/i' => '$1sis',
  63. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '$1$2sis',
  64. '/([ti])a$/i' => '$1um',
  65. '/(n)ews$/i' => '$1ews',
  66. '/(h|bl)ouses$/i' => '$1ouse',
  67. '/(corpse)s$/i' => '$1',
  68. '/(us)es$/i' => '$1',
  69. '/s$/i' => ''
  70. );
  71.  
  72. /**
  73. * Words that aren't easily desribable via generic regex
  74. * @var array
  75. */
  76. static $irregular = array(
  77. 'move' => 'moves',
  78. 'foot' => 'feet',
  79. 'goose' => 'geese',
  80. 'sex' => 'sexes',
  81. 'child' => 'children',
  82. 'man' => 'men',
  83. 'tooth' => 'teeth',
  84. 'person' => 'people'
  85. );
  86.  
  87. /**
  88. * Words that can be used as singular or plural without change
  89. * @var array
  90. */
  91. static $uncountable = array(
  92. 'sheep',
  93. 'fish',
  94. 'deer',
  95. 'series',
  96. 'species',
  97. 'money',
  98. 'rice',
  99. 'information',
  100. 'equipment'
  101. );
  102.  
  103. /**
  104. * Unconditionally pluralize a word
  105. * @param string $string The string to pluralize
  106. * @return string The pluralized string
  107. */
  108. public static function pluralize( $string )
  109. {
  110. // save some time in the case that singular and plural are the same
  111. if ( in_array( strtolower( $string ), self::$uncountable ) )
  112. return $string;
  113.  
  114. // check for irregular singular forms
  115. foreach ( self::$irregular as $pattern => $result )
  116. {
  117. $pattern = '/' . $pattern . '$/i';
  118.  
  119. if ( preg_match( $pattern, $string ) )
  120. return preg_replace( $pattern, $result, $string);
  121. }
  122.  
  123. // check for matches using regular expressions
  124. foreach ( self::$plural as $pattern => $result )
  125. {
  126. if ( preg_match( $pattern, $string ) )
  127. return preg_replace( $pattern, $result, $string );
  128. }
  129.  
  130. return $string;
  131. }
  132.  
  133. /**
  134. * Unconditionally singularize a word
  135. * @param string $string The string to singularize
  136. * @return string The singularized string
  137. */
  138. public static function singularize( $string )
  139. {
  140. // save some time in the case that singular and plural are the same
  141. if ( in_array( strtolower( $string ), self::$uncountable ) )
  142. return $string;
  143.  
  144. // check for irregular plural forms
  145. foreach ( self::$irregular as $result => $pattern )
  146. {
  147. $pattern = '/' . $pattern . '$/i';
  148.  
  149. if ( preg_match( $pattern, $string ) )
  150. return preg_replace( $pattern, $result, $string);
  151. }
  152.  
  153. // check for matches using regular expressions
  154. foreach ( self::$singular as $pattern => $result )
  155. {
  156. if ( preg_match( $pattern, $string ) )
  157. return preg_replace( $pattern, $result, $string );
  158. }
  159.  
  160. return $string;
  161. }
  162.  
  163. /**
  164. * Conditionally pluralize a word
  165. * @param int $count The number of items $string references
  166. * @param string $string The word to pluralize
  167. * @return string "{$count} " . $pluralized_string
  168. */
  169. public static function pluralize_if($count, $string)
  170. {
  171. if ($count == 1)
  172. return "1 $string";
  173. else
  174. return $count . " " . self::pluralize($string);
  175. }
  176. }
  177.  
  178. ?>

URL: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/Source

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.