EnglishInflector.php 16 KB

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