EnglishInflector.php 15 KB

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