EnglishInflector.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  236. ['su', 2, true, true, 'i'],
  237. // news (news)
  238. ['swen', 4, true, true, 'news'],
  239. // feet (foot)
  240. ['toof', 4, true, true, 'feet'],
  241. // chateaux (chateau), bureaus (bureau)
  242. ['uae', 3, false, true, ['eaus', 'eaux']],
  243. // oxen (ox)
  244. ['xo', 2, false, false, 'oxen'],
  245. // hoaxes (hoax)
  246. ['xaoh', 4, true, false, 'hoaxes'],
  247. // indices (index)
  248. ['xedni', 5, false, true, ['indicies', 'indexes']],
  249. // boxes (box)
  250. ['xo', 2, false, true, 'oxes'],
  251. // indexes (index), matrixes (matrix)
  252. ['x', 1, true, false, ['cies', 'xes']],
  253. // appendices (appendix)
  254. ['xi', 2, false, true, 'ices'],
  255. // babies (baby)
  256. ['y', 1, false, true, 'ies'],
  257. // quizzes (quiz)
  258. ['ziuq', 4, true, false, 'quizzes'],
  259. // waltzes (waltz)
  260. ['z', 1, true, true, 'zes'],
  261. ];
  262. /**
  263. * A list of words which should not be inflected, reversed.
  264. */
  265. private const UNINFLECTED = [
  266. '',
  267. // data
  268. 'atad',
  269. // deer
  270. 'reed',
  271. // equipment
  272. 'tnempiuqe',
  273. // feedback
  274. 'kcabdeef',
  275. // fish
  276. 'hsif',
  277. // health
  278. 'htlaeh',
  279. // history
  280. 'yrotsih',
  281. // info
  282. 'ofni',
  283. // information
  284. 'noitamrofni',
  285. // money
  286. 'yenom',
  287. // moose
  288. 'esoom',
  289. // series
  290. 'seires',
  291. // sheep
  292. 'peehs',
  293. // species
  294. 'seiceps',
  295. // traffic
  296. 'ciffart',
  297. // aircraft
  298. 'tfarcria',
  299. // hardware
  300. 'erawdrah',
  301. ];
  302. public function singularize(string $plural): array
  303. {
  304. $pluralRev = strrev($plural);
  305. $lowerPluralRev = strtolower($pluralRev);
  306. $pluralLength = \strlen($lowerPluralRev);
  307. // Check if the word is one which is not inflected, return early if so
  308. if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
  309. return [$plural];
  310. }
  311. // The outer loop iterates over the entries of the plural table
  312. // The inner loop $j iterates over the characters of the plural suffix
  313. // in the plural table to compare them with the characters of the actual
  314. // given plural suffix
  315. foreach (self::PLURAL_MAP as $map) {
  316. $suffix = $map[0];
  317. $suffixLength = $map[1];
  318. $j = 0;
  319. // Compare characters in the plural table and of the suffix of the
  320. // given plural one by one
  321. while ($suffix[$j] === $lowerPluralRev[$j]) {
  322. // Let $j point to the next character
  323. ++$j;
  324. // Successfully compared the last character
  325. // Add an entry with the singular suffix to the singular array
  326. if ($j === $suffixLength) {
  327. // Is there any character preceding the suffix in the plural string?
  328. if ($j < $pluralLength) {
  329. $nextIsVowel = str_contains('aeiou', $lowerPluralRev[$j]);
  330. if (!$map[2] && $nextIsVowel) {
  331. // suffix may not succeed a vowel but next char is one
  332. break;
  333. }
  334. if (!$map[3] && !$nextIsVowel) {
  335. // suffix may not succeed a consonant but next char is one
  336. break;
  337. }
  338. }
  339. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  340. $newSuffix = $map[4];
  341. // Check whether the first character in the plural suffix
  342. // is uppercased. If yes, uppercase the first character in
  343. // the singular suffix too
  344. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  345. if (\is_array($newSuffix)) {
  346. $singulars = [];
  347. foreach ($newSuffix as $newSuffixEntry) {
  348. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  349. }
  350. return $singulars;
  351. }
  352. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  353. }
  354. // Suffix is longer than word
  355. if ($j === $pluralLength) {
  356. break;
  357. }
  358. }
  359. }
  360. // Assume that plural and singular is identical
  361. return [$plural];
  362. }
  363. public function pluralize(string $singular): array
  364. {
  365. $singularRev = strrev($singular);
  366. $lowerSingularRev = strtolower($singularRev);
  367. $singularLength = \strlen($lowerSingularRev);
  368. // Check if the word is one which is not inflected, return early if so
  369. if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
  370. return [$singular];
  371. }
  372. // The outer loop iterates over the entries of the singular table
  373. // The inner loop $j iterates over the characters of the singular suffix
  374. // in the singular table to compare them with the characters of the actual
  375. // given singular suffix
  376. foreach (self::SINGULAR_MAP as $map) {
  377. $suffix = $map[0];
  378. $suffixLength = $map[1];
  379. $j = 0;
  380. // Compare characters in the singular table and of the suffix of the
  381. // given plural one by one
  382. while ($suffix[$j] === $lowerSingularRev[$j]) {
  383. // Let $j point to the next character
  384. ++$j;
  385. // Successfully compared the last character
  386. // Add an entry with the plural suffix to the plural array
  387. if ($j === $suffixLength) {
  388. // Is there any character preceding the suffix in the plural string?
  389. if ($j < $singularLength) {
  390. $nextIsVowel = str_contains('aeiou', $lowerSingularRev[$j]);
  391. if (!$map[2] && $nextIsVowel) {
  392. // suffix may not succeed a vowel but next char is one
  393. break;
  394. }
  395. if (!$map[3] && !$nextIsVowel) {
  396. // suffix may not succeed a consonant but next char is one
  397. break;
  398. }
  399. }
  400. $newBase = substr($singular, 0, $singularLength - $suffixLength);
  401. $newSuffix = $map[4];
  402. // Check whether the first character in the singular suffix
  403. // is uppercased. If yes, uppercase the first character in
  404. // the singular suffix too
  405. $firstUpper = ctype_upper($singularRev[$j - 1]);
  406. if (\is_array($newSuffix)) {
  407. $plurals = [];
  408. foreach ($newSuffix as $newSuffixEntry) {
  409. $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  410. }
  411. return $plurals;
  412. }
  413. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  414. }
  415. // Suffix is longer than word
  416. if ($j === $singularLength) {
  417. break;
  418. }
  419. }
  420. }
  421. // Assume that plural is singular with a trailing `s`
  422. return [$singular.'s'];
  423. }
  424. }