EnglishInflector.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. // coupon (coupons)
  164. ['nop', 3, true, true, 'pons'],
  165. // seasons (season), treasons (treason), poisons (poison), lessons (lesson)
  166. ['nos', 3, true, true, 'sons'],
  167. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  168. ['no', 2, true, true, 'a'],
  169. // echoes (echo)
  170. ['ohce', 4, true, true, 'echoes'],
  171. // heroes (hero)
  172. ['oreh', 4, true, true, 'heroes'],
  173. // atlases (atlas)
  174. ['salta', 5, true, true, 'atlases'],
  175. // irises (iris)
  176. ['siri', 4, true, true, 'irises'],
  177. // analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
  178. // theses (thesis), emphases (emphasis), oases (oasis),
  179. // crises (crisis)
  180. ['sis', 3, true, true, 'ses'],
  181. // accesses (access), addresses (address), kisses (kiss)
  182. ['ss', 2, true, false, 'sses'],
  183. // syllabi (syllabus)
  184. ['suballys', 8, true, true, 'syllabi'],
  185. // buses (bus)
  186. ['sub', 3, true, true, 'buses'],
  187. // circuses (circus)
  188. ['suc', 3, true, true, 'cuses'],
  189. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  190. ['su', 2, true, true, 'i'],
  191. // news (news)
  192. ['swen', 4, true, true, 'news'],
  193. // feet (foot)
  194. ['toof', 4, true, true, 'feet'],
  195. // chateaux (chateau), bureaus (bureau)
  196. ['uae', 3, false, true, ['eaus', 'eaux']],
  197. // oxen (ox)
  198. ['xo', 2, false, false, 'oxen'],
  199. // hoaxes (hoax)
  200. ['xaoh', 4, true, false, 'hoaxes'],
  201. // indices (index)
  202. ['xedni', 5, false, true, ['indicies', 'indexes']],
  203. // boxes (box)
  204. ['xo', 2, false, true, 'oxes'],
  205. // indexes (index), matrixes (matrix)
  206. ['x', 1, true, false, ['cies', 'xes']],
  207. // appendices (appendix)
  208. ['xi', 2, false, true, 'ices'],
  209. // babies (baby)
  210. ['y', 1, false, true, 'ies'],
  211. // quizzes (quiz)
  212. ['ziuq', 4, true, false, 'quizzes'],
  213. // waltzes (waltz)
  214. ['z', 1, true, true, 'zes'],
  215. ];
  216. /**
  217. * A list of words which should not be inflected, reversed.
  218. */
  219. private const UNINFLECTED = [
  220. '',
  221. 'atad',
  222. 'reed',
  223. 'kcabdeef',
  224. 'hsif',
  225. 'ofni',
  226. 'esoom',
  227. 'seires',
  228. 'peehs',
  229. 'seiceps',
  230. ];
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function singularize(string $plural): array
  235. {
  236. $pluralRev = strrev($plural);
  237. $lowerPluralRev = strtolower($pluralRev);
  238. $pluralLength = \strlen($lowerPluralRev);
  239. // Check if the word is one which is not inflected, return early if so
  240. if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
  241. return [$plural];
  242. }
  243. // The outer loop iterates over the entries of the plural table
  244. // The inner loop $j iterates over the characters of the plural suffix
  245. // in the plural table to compare them with the characters of the actual
  246. // given plural suffix
  247. foreach (self::PLURAL_MAP as $map) {
  248. $suffix = $map[0];
  249. $suffixLength = $map[1];
  250. $j = 0;
  251. // Compare characters in the plural table and of the suffix of the
  252. // given plural one by one
  253. while ($suffix[$j] === $lowerPluralRev[$j]) {
  254. // Let $j point to the next character
  255. ++$j;
  256. // Successfully compared the last character
  257. // Add an entry with the singular suffix to the singular array
  258. if ($j === $suffixLength) {
  259. // Is there any character preceding the suffix in the plural string?
  260. if ($j < $pluralLength) {
  261. $nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
  262. if (!$map[2] && $nextIsVocal) {
  263. // suffix may not succeed a vocal but next char is one
  264. break;
  265. }
  266. if (!$map[3] && !$nextIsVocal) {
  267. // suffix may not succeed a consonant but next char is one
  268. break;
  269. }
  270. }
  271. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  272. $newSuffix = $map[4];
  273. // Check whether the first character in the plural suffix
  274. // is uppercased. If yes, uppercase the first character in
  275. // the singular suffix too
  276. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  277. if (\is_array($newSuffix)) {
  278. $singulars = [];
  279. foreach ($newSuffix as $newSuffixEntry) {
  280. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  281. }
  282. return $singulars;
  283. }
  284. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  285. }
  286. // Suffix is longer than word
  287. if ($j === $pluralLength) {
  288. break;
  289. }
  290. }
  291. }
  292. // Assume that plural and singular is identical
  293. return [$plural];
  294. }
  295. /**
  296. * {@inheritdoc}
  297. */
  298. public function pluralize(string $singular): array
  299. {
  300. $singularRev = strrev($singular);
  301. $lowerSingularRev = strtolower($singularRev);
  302. $singularLength = \strlen($lowerSingularRev);
  303. // Check if the word is one which is not inflected, return early if so
  304. if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
  305. return [$singular];
  306. }
  307. // The outer loop iterates over the entries of the singular table
  308. // The inner loop $j iterates over the characters of the singular suffix
  309. // in the singular table to compare them with the characters of the actual
  310. // given singular suffix
  311. foreach (self::SINGULAR_MAP as $map) {
  312. $suffix = $map[0];
  313. $suffixLength = $map[1];
  314. $j = 0;
  315. // Compare characters in the singular table and of the suffix of the
  316. // given plural one by one
  317. while ($suffix[$j] === $lowerSingularRev[$j]) {
  318. // Let $j point to the next character
  319. ++$j;
  320. // Successfully compared the last character
  321. // Add an entry with the plural suffix to the plural array
  322. if ($j === $suffixLength) {
  323. // Is there any character preceding the suffix in the plural string?
  324. if ($j < $singularLength) {
  325. $nextIsVocal = false !== strpos('aeiou', $lowerSingularRev[$j]);
  326. if (!$map[2] && $nextIsVocal) {
  327. // suffix may not succeed a vocal but next char is one
  328. break;
  329. }
  330. if (!$map[3] && !$nextIsVocal) {
  331. // suffix may not succeed a consonant but next char is one
  332. break;
  333. }
  334. }
  335. $newBase = substr($singular, 0, $singularLength - $suffixLength);
  336. $newSuffix = $map[4];
  337. // Check whether the first character in the singular suffix
  338. // is uppercased. If yes, uppercase the first character in
  339. // the singular suffix too
  340. $firstUpper = ctype_upper($singularRev[$j - 1]);
  341. if (\is_array($newSuffix)) {
  342. $plurals = [];
  343. foreach ($newSuffix as $newSuffixEntry) {
  344. $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  345. }
  346. return $plurals;
  347. }
  348. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  349. }
  350. // Suffix is longer than word
  351. if ($j === $singularLength) {
  352. break;
  353. }
  354. }
  355. }
  356. // Assume that plural is singular with a trailing `s`
  357. return [$singular.'s'];
  358. }
  359. }