EnglishInflector.php 16 KB

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