StringUtil.php 6.6 KB

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