Inflector.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Inflector;
  11. /**
  12. * Converts words between singular and plural forms.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class Inflector
  19. {
  20. /**
  21. * Map English plural to singular suffixes.
  22. *
  23. * @var array
  24. *
  25. * @see http://english-zone.com/spelling/plurals.html
  26. */
  27. private static $pluralMap = array(
  28. // First entry: plural suffix, reversed
  29. // Second entry: length of plural suffix
  30. // Third entry: Whether the suffix may succeed a vocal
  31. // Fourth entry: Whether the suffix may succeed a consonant
  32. // Fifth entry: singular suffix, normal
  33. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  34. array('a', 1, true, true, array('on', 'um')),
  35. // nebulae (nebula)
  36. array('ea', 2, true, true, 'a'),
  37. // services (service)
  38. array('secivres', 8, true, true, 'service'),
  39. // mice (mouse), lice (louse)
  40. array('eci', 3, false, true, 'ouse'),
  41. // geese (goose)
  42. array('esee', 4, false, true, 'oose'),
  43. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  44. array('i', 1, true, true, 'us'),
  45. // men (man), women (woman)
  46. array('nem', 3, true, true, 'man'),
  47. // children (child)
  48. array('nerdlihc', 8, true, true, 'child'),
  49. // oxen (ox)
  50. array('nexo', 4, false, false, 'ox'),
  51. // indices (index), appendices (appendix), prices (price)
  52. array('seci', 4, false, true, array('ex', 'ix', 'ice')),
  53. // selfies (selfie)
  54. array('seifles', 7, true, true, 'selfie'),
  55. // movies (movie)
  56. array('seivom', 6, true, true, 'movie'),
  57. // feet (foot)
  58. array('teef', 4, true, true, 'foot'),
  59. // geese (goose)
  60. array('eseeg', 5, true, true, 'goose'),
  61. // teeth (tooth)
  62. array('hteet', 5, true, true, 'tooth'),
  63. // news (news)
  64. array('swen', 4, true, true, 'news'),
  65. // series (series)
  66. array('seires', 6, true, true, 'series'),
  67. // babies (baby)
  68. array('sei', 3, false, true, 'y'),
  69. // accesses (access), addresses (address), kisses (kiss)
  70. array('sess', 4, true, false, 'ss'),
  71. // analyses (analysis), ellipses (ellipsis), fungi (fungus),
  72. // neuroses (neurosis), theses (thesis), emphases (emphasis),
  73. // oases (oasis), crises (crisis), houses (house), bases (base),
  74. // atlases (atlas)
  75. array('ses', 3, true, true, array('s', 'se', 'sis')),
  76. // objectives (objective), alternative (alternatives)
  77. array('sevit', 5, true, true, 'tive'),
  78. // drives (drive)
  79. array('sevird', 6, false, true, 'drive'),
  80. // lives (life), wives (wife)
  81. array('sevi', 4, false, true, 'ife'),
  82. // moves (move)
  83. array('sevom', 5, true, true, 'move'),
  84. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
  85. array('sev', 3, true, true, array('f', 've', 'ff')),
  86. // axes (axis), axes (ax), axes (axe)
  87. array('sexa', 4, false, false, array('ax', 'axe', 'axis')),
  88. // indexes (index), matrixes (matrix)
  89. array('sex', 3, true, false, 'x'),
  90. // quizzes (quiz)
  91. array('sezz', 4, true, false, 'z'),
  92. // bureaus (bureau)
  93. array('suae', 4, false, true, 'eau'),
  94. // roses (rose), garages (garage), cassettes (cassette),
  95. // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
  96. // shoes (shoe)
  97. array('se', 2, true, true, array('', 'e')),
  98. // tags (tag)
  99. array('s', 1, true, true, ''),
  100. // chateaux (chateau)
  101. array('xuae', 4, false, true, 'eau'),
  102. // people (person)
  103. array('elpoep', 6, true, true, 'person'),
  104. );
  105. /**
  106. * This class should not be instantiated.
  107. */
  108. private function __construct()
  109. {
  110. }
  111. /**
  112. * Returns the singular form of a word.
  113. *
  114. * If the method can't determine the form with certainty, an array of the
  115. * possible singulars is returned.
  116. *
  117. * @param string $plural A word in plural form
  118. *
  119. * @return string|array The singular form or an array of possible singular
  120. * forms
  121. *
  122. * @internal
  123. */
  124. public static function singularize(string $plural)
  125. {
  126. $pluralRev = strrev($plural);
  127. $lowerPluralRev = strtolower($pluralRev);
  128. $pluralLength = \strlen($lowerPluralRev);
  129. // The outer loop iterates over the entries of the plural table
  130. // The inner loop $j iterates over the characters of the plural suffix
  131. // in the plural table to compare them with the characters of the actual
  132. // given plural suffix
  133. foreach (self::$pluralMap as $map) {
  134. $suffix = $map[0];
  135. $suffixLength = $map[1];
  136. $j = 0;
  137. // Compare characters in the plural table and of the suffix of the
  138. // given plural one by one
  139. while ($suffix[$j] === $lowerPluralRev[$j]) {
  140. // Let $j point to the next character
  141. ++$j;
  142. // Successfully compared the last character
  143. // Add an entry with the singular suffix to the singular array
  144. if ($j === $suffixLength) {
  145. // Is there any character preceding the suffix in the plural string?
  146. if ($j < $pluralLength) {
  147. $nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
  148. if (!$map[2] && $nextIsVocal) {
  149. // suffix may not succeed a vocal but next char is one
  150. break;
  151. }
  152. if (!$map[3] && !$nextIsVocal) {
  153. // suffix may not succeed a consonant but next char is one
  154. break;
  155. }
  156. }
  157. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  158. $newSuffix = $map[4];
  159. // Check whether the first character in the plural suffix
  160. // is uppercased. If yes, uppercase the first character in
  161. // the singular suffix too
  162. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  163. if (\is_array($newSuffix)) {
  164. $singulars = array();
  165. foreach ($newSuffix as $newSuffixEntry) {
  166. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  167. }
  168. return $singulars;
  169. }
  170. return $newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix);
  171. }
  172. // Suffix is longer than word
  173. if ($j === $pluralLength) {
  174. break;
  175. }
  176. }
  177. }
  178. // Assume that plural and singular is identical
  179. return $plural;
  180. }
  181. }