You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

508 lines
15 KiB

3 years ago
  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 vocal
  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. // selfies (selfie)
  45. ['seifles', 7, true, true, 'selfie'],
  46. // movies (movie)
  47. ['seivom', 6, true, true, 'movie'],
  48. // conspectuses (conspectus), prospectuses (prospectus)
  49. ['sesutcep', 8, true, true, 'pectus'],
  50. // feet (foot)
  51. ['teef', 4, true, true, 'foot'],
  52. // geese (goose)
  53. ['eseeg', 5, true, true, 'goose'],
  54. // teeth (tooth)
  55. ['hteet', 5, true, true, 'tooth'],
  56. // news (news)
  57. ['swen', 4, true, true, 'news'],
  58. // series (series)
  59. ['seires', 6, true, true, 'series'],
  60. // babies (baby)
  61. ['sei', 3, false, true, 'y'],
  62. // accesses (access), addresses (address), kisses (kiss)
  63. ['sess', 4, true, false, 'ss'],
  64. // analyses (analysis), ellipses (ellipsis), fungi (fungus),
  65. // neuroses (neurosis), theses (thesis), emphases (emphasis),
  66. // oases (oasis), crises (crisis), houses (house), bases (base),
  67. // atlases (atlas)
  68. ['ses', 3, true, true, ['s', 'se', 'sis']],
  69. // objectives (objective), alternative (alternatives)
  70. ['sevit', 5, true, true, 'tive'],
  71. // drives (drive)
  72. ['sevird', 6, false, true, 'drive'],
  73. // lives (life), wives (wife)
  74. ['sevi', 4, false, true, 'ife'],
  75. // moves (move)
  76. ['sevom', 5, true, true, 'move'],
  77. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
  78. ['sev', 3, true, true, ['f', 've', 'ff']],
  79. // axes (axis), axes (ax), axes (axe)
  80. ['sexa', 4, false, false, ['ax', 'axe', 'axis']],
  81. // indexes (index), matrixes (matrix)
  82. ['sex', 3, true, false, 'x'],
  83. // quizzes (quiz)
  84. ['sezz', 4, true, false, 'z'],
  85. // bureaus (bureau)
  86. ['suae', 4, false, true, 'eau'],
  87. // fees (fee), trees (tree), employees (employee)
  88. ['see', 3, true, true, 'ee'],
  89. // edges (edge)
  90. ['segd', 4, true, true, 'dge'],
  91. // roses (rose), garages (garage), cassettes (cassette),
  92. // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
  93. // shoes (shoe)
  94. ['se', 2, true, true, ['', 'e']],
  95. // tags (tag)
  96. ['s', 1, true, true, ''],
  97. // chateaux (chateau)
  98. ['xuae', 4, false, true, 'eau'],
  99. // people (person)
  100. ['elpoep', 6, true, true, 'person'],
  101. ];
  102. /**
  103. * Map English singular to plural suffixes.
  104. *
  105. * @see http://english-zone.com/spelling/plurals.html
  106. */
  107. private const SINGULAR_MAP = [
  108. // First entry: singular suffix, reversed
  109. // Second entry: length of singular suffix
  110. // Third entry: Whether the suffix may succeed a vocal
  111. // Fourth entry: Whether the suffix may succeed a consonant
  112. // Fifth entry: plural suffix, normal
  113. // criterion (criteria)
  114. ['airetirc', 8, false, false, 'criterion'],
  115. // nebulae (nebula)
  116. ['aluben', 6, false, false, 'nebulae'],
  117. // children (child)
  118. ['dlihc', 5, true, true, 'children'],
  119. // prices (price)
  120. ['eci', 3, false, true, 'ices'],
  121. // services (service)
  122. ['ecivres', 7, true, true, 'services'],
  123. // lives (life), wives (wife)
  124. ['efi', 3, false, true, 'ives'],
  125. // selfies (selfie)
  126. ['eifles', 6, true, true, 'selfies'],
  127. // movies (movie)
  128. ['eivom', 5, true, true, 'movies'],
  129. // lice (louse)
  130. ['esuol', 5, false, true, 'lice'],
  131. // mice (mouse)
  132. ['esuom', 5, false, true, 'mice'],
  133. // geese (goose)
  134. ['esoo', 4, false, true, 'eese'],
  135. // houses (house), bases (base)
  136. ['es', 2, true, true, 'ses'],
  137. // geese (goose)
  138. ['esoog', 5, true, true, 'geese'],
  139. // caves (cave)
  140. ['ev', 2, true, true, 'ves'],
  141. // drives (drive)
  142. ['evird', 5, false, true, 'drives'],
  143. // objectives (objective), alternative (alternatives)
  144. ['evit', 4, true, true, 'tives'],
  145. // moves (move)
  146. ['evom', 4, true, true, 'moves'],
  147. // staves (staff)
  148. ['ffats', 5, true, true, 'staves'],
  149. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  150. ['ff', 2, true, true, 'ffs'],
  151. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
  152. ['f', 1, true, true, ['fs', 'ves']],
  153. // arches (arch)
  154. ['hc', 2, true, true, 'ches'],
  155. // bushes (bush)
  156. ['hs', 2, true, true, 'shes'],
  157. // teeth (tooth)
  158. ['htoot', 5, true, true, 'teeth'],
  159. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  160. ['mu', 2, true, true, 'a'],
  161. // men (man), women (woman)
  162. ['nam', 3, true, true, 'men'],
  163. // people (person)
  164. ['nosrep', 6, true, true, ['persons', 'people']],
  165. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  166. ['noi', 3, true, true, 'ions'],
  167. // coupon (coupons)
  168. ['nop', 3, true, true, 'pons'],
  169. // seasons (season), treasons (treason), poisons (poison), lessons (lesson)
  170. ['nos', 3, true, true, 'sons'],
  171. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  172. ['no', 2, true, true, 'a'],
  173. // echoes (echo)
  174. ['ohce', 4, true, true, 'echoes'],
  175. // heroes (hero)
  176. ['oreh', 4, true, true, 'heroes'],
  177. // atlases (atlas)
  178. ['salta', 5, true, true, 'atlases'],
  179. // irises (iris)
  180. ['siri', 4, true, true, 'irises'],
  181. // analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
  182. // theses (thesis), emphases (emphasis), oases (oasis),
  183. // crises (crisis)
  184. ['sis', 3, true, true, 'ses'],
  185. // accesses (access), addresses (address), kisses (kiss)
  186. ['ss', 2, true, false, 'sses'],
  187. // syllabi (syllabus)
  188. ['suballys', 8, true, true, 'syllabi'],
  189. // buses (bus)
  190. ['sub', 3, true, true, 'buses'],
  191. // circuses (circus)
  192. ['suc', 3, true, true, 'cuses'],
  193. // conspectuses (conspectus), prospectuses (prospectus)
  194. ['sutcep', 6, true, true, 'pectuses'],
  195. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  196. ['su', 2, true, true, 'i'],
  197. // news (news)
  198. ['swen', 4, true, true, 'news'],
  199. // feet (foot)
  200. ['toof', 4, true, true, 'feet'],
  201. // chateaux (chateau), bureaus (bureau)
  202. ['uae', 3, false, true, ['eaus', 'eaux']],
  203. // oxen (ox)
  204. ['xo', 2, false, false, 'oxen'],
  205. // hoaxes (hoax)
  206. ['xaoh', 4, true, false, 'hoaxes'],
  207. // indices (index)
  208. ['xedni', 5, false, true, ['indicies', 'indexes']],
  209. // boxes (box)
  210. ['xo', 2, false, true, 'oxes'],
  211. // indexes (index), matrixes (matrix)
  212. ['x', 1, true, false, ['cies', 'xes']],
  213. // appendices (appendix)
  214. ['xi', 2, false, true, 'ices'],
  215. // babies (baby)
  216. ['y', 1, false, true, 'ies'],
  217. // quizzes (quiz)
  218. ['ziuq', 4, true, false, 'quizzes'],
  219. // waltzes (waltz)
  220. ['z', 1, true, true, 'zes'],
  221. ];
  222. /**
  223. * A list of words which should not be inflected, reversed.
  224. */
  225. private const UNINFLECTED = [
  226. '',
  227. // data
  228. 'atad',
  229. // deer
  230. 'reed',
  231. // feedback
  232. 'kcabdeef',
  233. // fish
  234. 'hsif',
  235. // info
  236. 'ofni',
  237. // moose
  238. 'esoom',
  239. // series
  240. 'seires',
  241. // sheep
  242. 'peehs',
  243. // species
  244. 'seiceps',
  245. ];
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function singularize(string $plural): array
  250. {
  251. $pluralRev = strrev($plural);
  252. $lowerPluralRev = strtolower($pluralRev);
  253. $pluralLength = \strlen($lowerPluralRev);
  254. // Check if the word is one which is not inflected, return early if so
  255. if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
  256. return [$plural];
  257. }
  258. // The outer loop iterates over the entries of the plural table
  259. // The inner loop $j iterates over the characters of the plural suffix
  260. // in the plural table to compare them with the characters of the actual
  261. // given plural suffix
  262. foreach (self::PLURAL_MAP as $map) {
  263. $suffix = $map[0];
  264. $suffixLength = $map[1];
  265. $j = 0;
  266. // Compare characters in the plural table and of the suffix of the
  267. // given plural one by one
  268. while ($suffix[$j] === $lowerPluralRev[$j]) {
  269. // Let $j point to the next character
  270. ++$j;
  271. // Successfully compared the last character
  272. // Add an entry with the singular suffix to the singular array
  273. if ($j === $suffixLength) {
  274. // Is there any character preceding the suffix in the plural string?
  275. if ($j < $pluralLength) {
  276. $nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
  277. if (!$map[2] && $nextIsVocal) {
  278. // suffix may not succeed a vocal but next char is one
  279. break;
  280. }
  281. if (!$map[3] && !$nextIsVocal) {
  282. // suffix may not succeed a consonant but next char is one
  283. break;
  284. }
  285. }
  286. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  287. $newSuffix = $map[4];
  288. // Check whether the first character in the plural suffix
  289. // is uppercased. If yes, uppercase the first character in
  290. // the singular suffix too
  291. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  292. if (\is_array($newSuffix)) {
  293. $singulars = [];
  294. foreach ($newSuffix as $newSuffixEntry) {
  295. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  296. }
  297. return $singulars;
  298. }
  299. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  300. }
  301. // Suffix is longer than word
  302. if ($j === $pluralLength) {
  303. break;
  304. }
  305. }
  306. }
  307. // Assume that plural and singular is identical
  308. return [$plural];
  309. }
  310. /**
  311. * {@inheritdoc}
  312. */
  313. public function pluralize(string $singular): array
  314. {
  315. $singularRev = strrev($singular);
  316. $lowerSingularRev = strtolower($singularRev);
  317. $singularLength = \strlen($lowerSingularRev);
  318. // Check if the word is one which is not inflected, return early if so
  319. if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
  320. return [$singular];
  321. }
  322. // The outer loop iterates over the entries of the singular table
  323. // The inner loop $j iterates over the characters of the singular suffix
  324. // in the singular table to compare them with the characters of the actual
  325. // given singular suffix
  326. foreach (self::SINGULAR_MAP as $map) {
  327. $suffix = $map[0];
  328. $suffixLength = $map[1];
  329. $j = 0;
  330. // Compare characters in the singular table and of the suffix of the
  331. // given plural one by one
  332. while ($suffix[$j] === $lowerSingularRev[$j]) {
  333. // Let $j point to the next character
  334. ++$j;
  335. // Successfully compared the last character
  336. // Add an entry with the plural suffix to the plural array
  337. if ($j === $suffixLength) {
  338. // Is there any character preceding the suffix in the plural string?
  339. if ($j < $singularLength) {
  340. $nextIsVocal = false !== strpos('aeiou', $lowerSingularRev[$j]);
  341. if (!$map[2] && $nextIsVocal) {
  342. // suffix may not succeed a vocal but next char is one
  343. break;
  344. }
  345. if (!$map[3] && !$nextIsVocal) {
  346. // suffix may not succeed a consonant but next char is one
  347. break;
  348. }
  349. }
  350. $newBase = substr($singular, 0, $singularLength - $suffixLength);
  351. $newSuffix = $map[4];
  352. // Check whether the first character in the singular suffix
  353. // is uppercased. If yes, uppercase the first character in
  354. // the singular suffix too
  355. $firstUpper = ctype_upper($singularRev[$j - 1]);
  356. if (\is_array($newSuffix)) {
  357. $plurals = [];
  358. foreach ($newSuffix as $newSuffixEntry) {
  359. $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  360. }
  361. return $plurals;
  362. }
  363. return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
  364. }
  365. // Suffix is longer than word
  366. if ($j === $singularLength) {
  367. break;
  368. }
  369. }
  370. }
  371. // Assume that plural is singular with a trailing `s`
  372. return [$singular.'s'];
  373. }
  374. }