EnglishInflector.php 17 KB

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