Debian Codegraph @

vendor/symfony/string/Inflector/EnglishInflector.php

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