EnglishInflector.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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\String\Inflector;
  11. final class EnglishInflector implements InflectorInterface
  12. {
  13. /**
  14. * Map English plural to singular suffixes.
  15. *
  16. * @see http://english-zone.com/spelling/plurals.html
  17. */
  18. private const PLURAL_MAP = [
  19. // First entry: plural suffix, reversed
  20. // Second entry: length of plural suffix
  21. // Third entry: Whether the suffix may succeed a vocal
  22. // Fourth entry: Whether the suffix may succeed a consonant
  23. // Fifth entry: singular suffix, normal
  24. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  25. ['a', 1, true, true, ['on', 'um']],
  26. // nebulae (nebula)
  27. ['ea', 2, true, true, 'a'],
  28. // services (service)
  29. ['secivres', 8, true, true, 'service'],
  30. // mice (mouse), lice (louse)
  31. ['eci', 3, false, true, 'ouse'],
  32. // geese (goose)
  33. ['esee', 4, false, true, 'oose'],
  34. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  35. ['i', 1, true, true, 'us'],
  36. // men (man), women (woman)
  37. ['nem', 3, true, true, 'man'],
  38. // children (child)
  39. ['nerdlihc', 8, true, true, 'child'],
  40. // oxen (ox)
  41. ['nexo', 4, false, false, 'ox'],
  42. // indices (index), appendices (appendix), prices (price)
  43. ['seci', 4, false, true, ['ex', 'ix', 'ice']],
  44. // selfies (selfie)
  45. ['seifles', 7, true, true, 'selfie'],
  46. // movies (movie)
  47. ['seivom', 6, true, true, 'movie'],
  48. // feet (foot)
  49. ['teef', 4, true, true, 'foot'],
  50. // geese (goose)
  51. ['eseeg', 5, true, true, 'goose'],
  52. // teeth (tooth)
  53. ['hteet', 5, true, true, 'tooth'],
  54. // news (news)
  55. ['swen', 4, true, true, 'news'],
  56. // series (series)
  57. ['seires', 6, true, true, 'series'],
  58. // babies (baby)
  59. ['sei', 3, false, true, 'y'],
  60. // accesses (access), addresses (address), kisses (kiss)
  61. ['sess', 4, true, false, 'ss'],
  62. // analyses (analysis), ellipses (ellipsis), fungi (fungus),
  63. // neuroses (neurosis), theses (thesis), emphases (emphasis),
  64. // oases (oasis), crises (crisis), houses (house), bases (base),
  65. // atlases (atlas)
  66. ['ses', 3, true, true, ['s', 'se', 'sis']],
  67. // objectives (objective), alternative (alternatives)
  68. ['sevit', 5, true, true, 'tive'],
  69. // drives (drive)
  70. ['sevird', 6, false, true, 'drive'],
  71. // lives (life), wives (wife)
  72. ['sevi', 4, false, true, 'ife'],
  73. // moves (move)
  74. ['sevom', 5, true, true, 'move'],
  75. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
  76. ['sev', 3, true, true, ['f', 've', 'ff']],
  77. // axes (axis), axes (ax), axes (axe)
  78. ['sexa', 4, false, false, ['ax', 'axe', 'axis']],
  79. // indexes (index), matrixes (matrix)
  80. ['sex', 3, true, false, 'x'],
  81. // quizzes (quiz)
  82. ['sezz', 4, true, false, 'z'],
  83. // bureaus (bureau)
  84. ['suae', 4, false, true, 'eau'],
  85. // fees (fee), trees (tree), employees (employee)
  86. ['see', 3, true, true, 'ee'],
  87. // roses (rose), garages (garage), cassettes (cassette),
  88. // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
  89. // shoes (shoe)
  90. ['se', 2, true, true, ['', 'e']],
  91. // tags (tag)
  92. ['s', 1, true, true, ''],
  93. // chateaux (chateau)
  94. ['xuae', 4, false, true, 'eau'],
  95. // people (person)
  96. ['elpoep', 6, true, true, 'person'],
  97. ];
  98. /**
  99. * Map English singular to plural suffixes.
  100. *
  101. * @see http://english-zone.com/spelling/plurals.html
  102. */
  103. private const SINGULAR_MAP = [
  104. // First entry: singular suffix, reversed
  105. // Second entry: length of singular suffix
  106. // Third entry: Whether the suffix may succeed a vocal
  107. // Fourth entry: Whether the suffix may succeed a consonant
  108. // Fifth entry: plural suffix, normal
  109. // criterion (criteria)
  110. ['airetirc', 8, false, false, 'criterion'],
  111. // nebulae (nebula)
  112. ['aluben', 6, false, false, 'nebulae'],
  113. // children (child)
  114. ['dlihc', 5, true, true, 'children'],
  115. // prices (price)
  116. ['eci', 3, false, true, 'ices'],
  117. // services (service)
  118. ['ecivres', 7, true, true, 'services'],
  119. // lives (life), wives (wife)
  120. ['efi', 3, false, true, 'ives'],
  121. // selfies (selfie)
  122. ['eifles', 6, true, true, 'selfies'],
  123. // movies (movie)
  124. ['eivom', 5, true, true, 'movies'],
  125. // lice (louse)
  126. ['esuol', 5, false, true, 'lice'],
  127. // mice (mouse)
  128. ['esuom', 5, false, true, 'mice'],
  129. // geese (goose)
  130. ['esoo', 4, false, true, 'eese'],
  131. // houses (house), bases (base)
  132. ['es', 2, true, true, 'ses'],
  133. // geese (goose)
  134. ['esoog', 5, true, true, 'geese'],
  135. // caves (cave)
  136. ['ev', 2, true, true, 'ves'],
  137. // drives (drive)
  138. ['evird', 5, false, true, 'drives'],
  139. // objectives (objective), alternative (alternatives)
  140. ['evit', 4, true, true, 'tives'],
  141. // moves (move)
  142. ['evom', 4, true, true, 'moves'],
  143. // staves (staff)
  144. ['ffats', 5, true, true, 'staves'],
  145. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  146. ['ff', 2, true, true, 'ffs'],
  147. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  148. ['f', 1, true, true, ['fs', 'ves']],
  149. // arches (arch)
  150. ['hc', 2, true, true, 'ches'],
  151. // bushes (bush)
  152. ['hs', 2, true, true, 'shes'],
  153. // teeth (tooth)
  154. ['htoot', 5, true, true, 'teeth'],
  155. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  156. ['mu', 2, true, true, 'a'],
  157. // men (man), women (woman)
  158. ['nam', 3, true, true, 'men'],
  159. // people (person)
  160. ['nosrep', 6, true, true, ['persons', 'people']],
  161. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  162. ['noi', 3, true, true, 'ions'],
  163. // seasons (season), treasons (treason), poisons (poison), lessons (lesson)
  164. ['nos', 3, true, true, 'sons'],
  165. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  166. ['no', 2, true, true, 'a'],
  167. // echoes (echo)
  168. ['ohce', 4, true, true, 'echoes'],
  169. // heroes (hero)
  170. ['oreh', 4, true, true, 'heroes'],
  171. // atlases (atlas)
  172. ['salta', 5, true, true, 'atlases'],
  173. // irises (iris)
  174. ['siri', 4, true, true, 'irises'],
  175. // analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
  176. // theses (thesis), emphases (emphasis), oases (oasis),
  177. // crises (crisis)
  178. ['sis', 3, true, true, 'ses'],
  179. // accesses (access), addresses (address), kisses (kiss)
  180. ['ss', 2, true, false, 'sses'],
  181. // syllabi (syllabus)
  182. ['suballys', 8, true, true, 'syllabi'],
  183. // buses (bus)
  184. ['sub', 3, true, true, 'buses'],
  185. // circuses (circus)
  186. ['suc', 3, true, true, 'cuses'],
  187. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  188. ['su', 2, true, true, 'i'],
  189. // news (news)
  190. ['swen', 4, true, true, 'news'],
  191. // feet (foot)
  192. ['toof', 4, true, true, 'feet'],
  193. // chateaux (chateau), bureaus (bureau)
  194. ['uae', 3, false, true, ['eaus', 'eaux']],
  195. // oxen (ox)
  196. ['xo', 2, false, false, 'oxen'],
  197. // hoaxes (hoax)
  198. ['xaoh', 4, true, false, 'hoaxes'],
  199. // indices (index)
  200. ['xedni', 5, false, true, ['indicies', 'indexes']],
  201. // boxes (box)
  202. ['xo', 2, false, true, 'oxes'],
  203. // indexes (index), matrixes (matrix)
  204. ['x', 1, true, false, ['cies', 'xes']],
  205. // appendices (appendix)
  206. ['xi', 2, false, true, 'ices'],
  207. // babies (baby)
  208. ['y', 1, false, true, 'ies'],
  209. // quizzes (quiz)
  210. ['ziuq', 4, true, false, 'quizzes'],
  211. // waltzes (waltz)
  212. ['z', 1, true, true, 'zes'],
  213. ];
  214. /**
  215. * A list of words which should not be inflected, reversed.
  216. */
  217. private const UNINFLECTED = [
  218. '',
  219. 'atad',
  220. 'reed',
  221. 'kcabdeef',
  222. 'hsif',
  223. 'ofni',
  224. 'esoom',
  225. 'seires',
  226. 'peehs',
  227. 'seiceps',
  228. ];
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function singularize(string $plural): array
  233. {
  234. $pluralRev = strrev($plural);
  235. $lowerPluralRev = strtolower($pluralRev);
  236. $pluralLength = \strlen($lowerPluralRev);
  237. // Check if the word is one which is not inflected, return early if so
  238. if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
  239. return [$plural];
  240. }
  241. // The outer loop iterates over the entries of the plural table
  242. // The inner loop $j iterates over the characters of the plural suffix
  243. // in the plural table to compare them with the characters of the actual
  244. // given plural suffix
  245. foreach (self::PLURAL_MAP as $map) {
  246. $suffix = $map[0];
  247. $suffixLength = $map[1];
  248. $j = 0;
  249. // Compare characters in the plural table and of the suffix of the
  250. // given plural one by one
  251. while ($suffix[$j] === $lowerPluralRev[$j]) {
  252. // Let $j point to the next character
  253. ++$j;
  254. // Successfully compared the last character
  255. // Add an entry with the singular suffix to the singular array
  256. if ($j === $suffixLength) {
  257. // Is there any character preceding the suffix in the plural string?
  258. if ($j < $pluralLength) {
  259. $nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
  260. if (!$map[2] && $nextIsVocal) {
  261. // suffix may not succeed a vocal but next char is one
  262. break;
  263. }
  264. if (!$map[3] && !$nextIsVocal) {
  265. // suffix may not succeed a consonant but next char is one
  266. break;
  267. }
  268. }
  269. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  270. $newSuffix = $map[4];
  271. // Check whether the first character in the plural suffix
  272. // is uppercased. If yes, uppercase the first character in
  273. // the singular suffix too
  274. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  275. if (\is_array($newSuffix)) {
  276. $singulars = [];
  277. foreach ($newSuffix as $newSuffixEntry) {
  278. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  279. }
  280. return $singulars;
  281. }
  282. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  283. }
  284. // Suffix is longer than word
  285. if ($j === $pluralLength) {
  286. break;
  287. }
  288. }
  289. }
  290. // Assume that plural and singular is identical
  291. return [$plural];
  292. }
  293. /**
  294. * {@inheritdoc}
  295. */
  296. public function pluralize(string $singular): array
  297. {
  298. $singularRev = strrev($singular);
  299. $lowerSingularRev = strtolower($singularRev);
  300. $singularLength = \strlen($lowerSingularRev);
  301. // Check if the word is one which is not inflected, return early if so
  302. if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
  303. return [$singular];
  304. }
  305. // The outer loop iterates over the entries of the singular table
  306. // The inner loop $j iterates over the characters of the singular suffix
  307. // in the singular table to compare them with the characters of the actual
  308. // given singular suffix
  309. foreach (self::SINGULAR_MAP as $map) {
  310. $suffix = $map[0];
  311. $suffixLength = $map[1];
  312. $j = 0;
  313. // Compare characters in the singular table and of the suffix of the
  314. // given plural one by one
  315. while ($suffix[$j] === $lowerSingularRev[$j]) {
  316. // Let $j point to the next character
  317. ++$j;
  318. // Successfully compared the last character
  319. // Add an entry with the plural suffix to the plural array
  320. if ($j === $suffixLength) {
  321. // Is there any character preceding the suffix in the plural string?
  322. if ($j < $singularLength) {
  323. $nextIsVocal = false !== strpos('aeiou', $lowerSingularRev[$j]);
  324. if (!$map[2] && $nextIsVocal) {
  325. // suffix may not succeed a vocal but next char is one
  326. break;
  327. }
  328. if (!$map[3] && !$nextIsVocal) {
  329. // suffix may not succeed a consonant but next char is one
  330. break;
  331. }
  332. }
  333. $newBase = substr($singular, 0, $singularLength - $suffixLength);
  334. $newSuffix = $map[4];
  335. // Check whether the first character in the singular suffix
  336. // is uppercased. If yes, uppercase the first character in
  337. // the singular suffix too
  338. $firstUpper = ctype_upper($singularRev[$j - 1]);
  339. if (\is_array($newSuffix)) {
  340. $plurals = [];
  341. foreach ($newSuffix as $newSuffixEntry) {
  342. $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  343. }
  344. return $plurals;
  345. }
  346. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  347. }
  348. // Suffix is longer than word
  349. if ($j === $singularLength) {
  350. break;
  351. }
  352. }
  353. }
  354. // Assume that plural is singular with a trailing `s`
  355. return [$singular.'s'];
  356. }
  357. }