EnglishInflector.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. // roses (rose), garages (garage), cassettes (cassette),
  131. // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
  132. // shoes (shoe)
  133. ['se', 2, true, true, ['', 'e']],
  134. // status (status)
  135. ['sutats', 6, true, true, 'status'],
  136. // tags (tag)
  137. ['s', 1, true, true, ''],
  138. // chateaux (chateau)
  139. ['xuae', 4, false, true, 'eau'],
  140. // people (person)
  141. ['elpoep', 6, true, true, 'person'],
  142. ];
  143. /**
  144. * Map English singular to plural suffixes.
  145. *
  146. * @see http://english-zone.com/spelling/plurals.html
  147. */
  148. private const SINGULAR_MAP = [
  149. // First entry: singular suffix, reversed
  150. // Second entry: length of singular suffix
  151. // Third entry: Whether the suffix may succeed a vowel
  152. // Fourth entry: Whether the suffix may succeed a consonant
  153. // Fifth entry: plural suffix, normal
  154. // passerby (passersby)
  155. ['ybressap', 8, true, true, 'passersby'],
  156. // insigne (insignia, insignias)
  157. ['engisni', 7, true, true, ['insignia', 'insignias']],
  158. // nodes (node)
  159. ['edon', 4, true, true, 'nodes'],
  160. // axes (axis)
  161. ['sixa', 4, false, false, 'axes'],
  162. // criterion (criteria)
  163. ['airetirc', 8, false, false, 'criterion'],
  164. // nebulae (nebula)
  165. ['aluben', 6, false, false, 'nebulae'],
  166. // children (child)
  167. ['dlihc', 5, true, true, 'children'],
  168. // prices (price)
  169. ['eci', 3, false, true, 'ices'],
  170. // services (service)
  171. ['ecivres', 7, true, true, 'services'],
  172. // lives (life), wives (wife)
  173. ['efi', 3, false, true, 'ives'],
  174. // selfies (selfie)
  175. ['eifles', 6, true, true, 'selfies'],
  176. // movies (movie)
  177. ['eivom', 5, true, true, 'movies'],
  178. // lice (louse)
  179. ['esuol', 5, false, true, 'lice'],
  180. // mice (mouse)
  181. ['esuom', 5, false, true, 'mice'],
  182. // geese (goose)
  183. ['esoo', 4, false, true, 'eese'],
  184. // houses (house), bases (base)
  185. ['es', 2, true, true, 'ses'],
  186. // geese (goose)
  187. ['esoog', 5, true, true, 'geese'],
  188. // caves (cave)
  189. ['ev', 2, true, true, 'ves'],
  190. // drives (drive)
  191. ['evird', 5, false, true, 'drives'],
  192. // objectives (objective), alternative (alternatives)
  193. ['evit', 4, true, true, 'tives'],
  194. // moves (move)
  195. ['evom', 4, true, true, 'moves'],
  196. // staves (staff)
  197. ['ffats', 5, true, true, 'staves'],
  198. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  199. ['ff', 2, true, true, 'ffs'],
  200. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  201. ['f', 1, true, true, ['fs', 'ves']],
  202. // arches (arch)
  203. ['hc', 2, true, true, 'ches'],
  204. // bushes (bush)
  205. ['hs', 2, true, true, 'shes'],
  206. // teeth (tooth)
  207. ['htoot', 5, true, true, 'teeth'],
  208. // albums (album)
  209. ['mubla', 5, true, true, 'albums'],
  210. // quorums (quorum)
  211. ['murouq', 6, true, true, ['quora', 'quorums']],
  212. // bacteria (bacterium), curricula (curriculum), media (medium), memoranda (memorandum), phenomena (phenomenon), strata (stratum)
  213. ['mu', 2, true, true, 'a'],
  214. // men (man), women (woman)
  215. ['nam', 3, true, true, 'men'],
  216. // people (person)
  217. ['nosrep', 6, true, true, ['persons', 'people']],
  218. // criteria (criterion)
  219. ['noiretirc', 9, true, true, 'criteria'],
  220. // phenomena (phenomenon)
  221. ['nonemonehp', 10, true, true, 'phenomena'],
  222. // echoes (echo)
  223. ['ohce', 4, true, true, 'echoes'],
  224. // heroes (hero)
  225. ['oreh', 4, true, true, 'heroes'],
  226. // atlases (atlas)
  227. ['salta', 5, true, true, 'atlases'],
  228. // aliases (alias)
  229. ['saila', 5, true, true, 'aliases'],
  230. // irises (iris)
  231. ['siri', 4, true, true, 'irises'],
  232. // analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
  233. // theses (thesis), emphases (emphasis), oases (oasis),
  234. // crises (crisis)
  235. ['sis', 3, true, true, 'ses'],
  236. // accesses (access), addresses (address), kisses (kiss)
  237. ['ss', 2, true, false, 'sses'],
  238. // syllabi (syllabus)
  239. ['suballys', 8, true, true, 'syllabi'],
  240. // buses (bus)
  241. ['sub', 3, true, true, 'buses'],
  242. // circuses (circus)
  243. ['suc', 3, true, true, 'cuses'],
  244. // hippocampi (hippocampus)
  245. ['supmacoppih', 11, false, false, 'hippocampi'],
  246. // campuses (campus)
  247. ['sup', 3, true, true, 'puses'],
  248. // status (status)
  249. ['sutats', 6, true, true, ['status', 'statuses']],
  250. // conspectuses (conspectus), prospectuses (prospectus)
  251. ['sutcep', 6, true, true, 'pectuses'],
  252. // nexuses (nexus)
  253. ['suxen', 5, false, false, 'nexuses'],
  254. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  255. ['su', 2, true, true, 'i'],
  256. // news (news)
  257. ['swen', 4, true, true, 'news'],
  258. // feet (foot)
  259. ['toof', 4, true, true, 'feet'],
  260. // chateaux (chateau), bureaus (bureau)
  261. ['uae', 3, false, true, ['eaus', 'eaux']],
  262. // oxen (ox)
  263. ['xo', 2, false, false, 'oxen'],
  264. // hoaxes (hoax)
  265. ['xaoh', 4, true, false, 'hoaxes'],
  266. // indices (index)
  267. ['xedni', 5, false, true, ['indicies', 'indexes']],
  268. // fax (faxes, faxxes)
  269. ['xaf', 3, true, true, ['faxes', 'faxxes']],
  270. // boxes (box)
  271. ['xo', 2, false, true, 'oxes'],
  272. // indexes (index), matrixes (matrix), appendices (appendix)
  273. ['x', 1, true, false, ['ces', 'xes']],
  274. // babies (baby)
  275. ['y', 1, false, true, 'ies'],
  276. // quizzes (quiz)
  277. ['ziuq', 4, true, false, 'quizzes'],
  278. // waltzes (waltz)
  279. ['z', 1, true, true, 'zes'],
  280. ];
  281. /**
  282. * A list of words which should not be inflected, reversed.
  283. */
  284. private const UNINFLECTED = [
  285. '',
  286. // data
  287. 'atad',
  288. // deer
  289. 'reed',
  290. // equipment
  291. 'tnempiuqe',
  292. // feedback
  293. 'kcabdeef',
  294. // fish
  295. 'hsif',
  296. // health
  297. 'htlaeh',
  298. // history
  299. 'yrotsih',
  300. // info
  301. 'ofni',
  302. // information
  303. 'noitamrofni',
  304. // money
  305. 'yenom',
  306. // moose
  307. 'esoom',
  308. // series
  309. 'seires',
  310. // sheep
  311. 'peehs',
  312. // species
  313. 'seiceps',
  314. // traffic
  315. 'ciffart',
  316. // aircraft
  317. 'tfarcria',
  318. // hardware
  319. 'erawdrah',
  320. ];
  321. public function singularize(string $plural): array
  322. {
  323. $pluralRev = strrev($plural);
  324. $lowerPluralRev = strtolower($pluralRev);
  325. $pluralLength = \strlen($lowerPluralRev);
  326. // Check if the word is one which is not inflected, return early if so
  327. if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
  328. return [$plural];
  329. }
  330. // The outer loop iterates over the entries of the plural table
  331. // The inner loop $j iterates over the characters of the plural suffix
  332. // in the plural table to compare them with the characters of the actual
  333. // given plural suffix
  334. foreach (self::PLURAL_MAP as $map) {
  335. $suffix = $map[0];
  336. $suffixLength = $map[1];
  337. $j = 0;
  338. // Compare characters in the plural table and of the suffix of the
  339. // given plural one by one
  340. while ($suffix[$j] === $lowerPluralRev[$j]) {
  341. // Let $j point to the next character
  342. ++$j;
  343. // Successfully compared the last character
  344. // Add an entry with the singular suffix to the singular array
  345. if ($j === $suffixLength) {
  346. // Is there any character preceding the suffix in the plural string?
  347. if ($j < $pluralLength) {
  348. $nextIsVowel = str_contains('aeiou', $lowerPluralRev[$j]);
  349. if (!$map[2] && $nextIsVowel) {
  350. // suffix may not succeed a vowel but next char is one
  351. break;
  352. }
  353. if (!$map[3] && !$nextIsVowel) {
  354. // suffix may not succeed a consonant but next char is one
  355. break;
  356. }
  357. }
  358. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  359. $newSuffix = $map[4];
  360. // Check whether the first character in the plural suffix
  361. // is uppercased. If yes, uppercase the first character in
  362. // the singular suffix too
  363. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  364. if (\is_array($newSuffix)) {
  365. $singulars = [];
  366. foreach ($newSuffix as $newSuffixEntry) {
  367. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  368. }
  369. return $singulars;
  370. }
  371. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  372. }
  373. // Suffix is longer than word
  374. if ($j === $pluralLength) {
  375. break;
  376. }
  377. }
  378. }
  379. // Assume that plural and singular is identical
  380. return [$plural];
  381. }
  382. public function pluralize(string $singular): array
  383. {
  384. $singularRev = strrev($singular);
  385. $lowerSingularRev = strtolower($singularRev);
  386. $singularLength = \strlen($lowerSingularRev);
  387. // Check if the word is one which is not inflected, return early if so
  388. if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
  389. return [$singular];
  390. }
  391. // The outer loop iterates over the entries of the singular table
  392. // The inner loop $j iterates over the characters of the singular suffix
  393. // in the singular table to compare them with the characters of the actual
  394. // given singular suffix
  395. foreach (self::SINGULAR_MAP as $map) {
  396. $suffix = $map[0];
  397. $suffixLength = $map[1];
  398. $j = 0;
  399. // Compare characters in the singular table and of the suffix of the
  400. // given plural one by one
  401. while ($suffix[$j] === $lowerSingularRev[$j]) {
  402. // Let $j point to the next character
  403. ++$j;
  404. // Successfully compared the last character
  405. // Add an entry with the plural suffix to the plural array
  406. if ($j === $suffixLength) {
  407. // Is there any character preceding the suffix in the plural string?
  408. if ($j < $singularLength) {
  409. $nextIsVowel = str_contains('aeiou', $lowerSingularRev[$j]);
  410. if (!$map[2] && $nextIsVowel) {
  411. // suffix may not succeed a vowel but next char is one
  412. break;
  413. }
  414. if (!$map[3] && !$nextIsVowel) {
  415. // suffix may not succeed a consonant but next char is one
  416. break;
  417. }
  418. }
  419. $newBase = substr($singular, 0, $singularLength - $suffixLength);
  420. $newSuffix = $map[4];
  421. // Check whether the first character in the singular suffix
  422. // is uppercased. If yes, uppercase the first character in
  423. // the singular suffix too
  424. $firstUpper = ctype_upper($singularRev[$j - 1]);
  425. if (\is_array($newSuffix)) {
  426. $plurals = [];
  427. foreach ($newSuffix as $newSuffixEntry) {
  428. $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  429. }
  430. return $plurals;
  431. }
  432. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  433. }
  434. // Suffix is longer than word
  435. if ($j === $singularLength) {
  436. break;
  437. }
  438. }
  439. }
  440. // Assume that plural is singular with a trailing `s`
  441. return [$singular.'s'];
  442. }
  443. }