EnglishInflector.php 18 KB

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