| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\String\Inflector; |
| 13 |
|
| 14 |
final class EnglishInflector implements InflectorInterface |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private const PLURAL_MAP = [ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
['saingisni', 9, true, true, 'insigne'], |
| 30 |
['aingisni', 8, true, true, 'insigne'], |
| 31 |
|
| 32 |
|
| 33 |
['ybsressap', 9, true, true, 'passerby'], |
| 34 |
|
| 35 |
|
| 36 |
['sedon', 5, true, true, 'node'], |
| 37 |
|
| 38 |
|
| 39 |
['airetcab', 8, true, true, 'bacterium'], |
| 40 |
|
| 41 |
|
| 42 |
['seussi', 6, true, true, 'issue'], |
| 43 |
|
| 44 |
|
| 45 |
['aroproc', 7, true, true, 'corpus'], |
| 46 |
|
| 47 |
|
| 48 |
['airetirc', 8, true, true, 'criterion'], |
| 49 |
|
| 50 |
|
| 51 |
['alucirruc', 9, true, true, 'curriculum'], |
| 52 |
|
| 53 |
|
| 54 |
['arouq', 5, true, true, 'quorum'], |
| 55 |
|
| 56 |
|
| 57 |
['areneg', 6, true, true, 'genus'], |
| 58 |
|
| 59 |
|
| 60 |
['aidem', 5, true, true, 'medium'], |
| 61 |
|
| 62 |
|
| 63 |
['adnaromem', 9, true, true, 'memorandum'], |
| 64 |
|
| 65 |
|
| 66 |
['anemonehp', 9, true, true, 'phenomenon'], |
| 67 |
|
| 68 |
|
| 69 |
['atarts', 6, true, true, 'stratum'], |
| 70 |
|
| 71 |
|
| 72 |
['ea', 2, true, true, 'a'], |
| 73 |
|
| 74 |
|
| 75 |
['secivres', 8, true, true, 'service'], |
| 76 |
|
| 77 |
|
| 78 |
['eci', 3, false, true, 'ouse'], |
| 79 |
|
| 80 |
|
| 81 |
['esee', 4, false, true, 'oose'], |
| 82 |
|
| 83 |
|
| 84 |
['i', 1, true, true, 'us'], |
| 85 |
|
| 86 |
|
| 87 |
['nem', 3, true, true, 'man'], |
| 88 |
|
| 89 |
|
| 90 |
['nerdlihc', 8, true, true, 'child'], |
| 91 |
|
| 92 |
|
| 93 |
['nexo', 4, false, false, 'ox'], |
| 94 |
|
| 95 |
|
| 96 |
['seci', 4, false, true, ['ex', 'ix', 'ice']], |
| 97 |
|
| 98 |
|
| 99 |
['sedoc', 5, false, true, 'code'], |
| 100 |
|
| 101 |
|
| 102 |
['seifles', 7, true, true, 'selfie'], |
| 103 |
|
| 104 |
|
| 105 |
['seibmoz', 7, true, true, 'zombie'], |
| 106 |
|
| 107 |
|
| 108 |
['seivom', 6, true, true, 'movie'], |
| 109 |
|
| 110 |
|
| 111 |
['seman', 5, true, false, 'name'], |
| 112 |
|
| 113 |
|
| 114 |
['sesutcep', 8, true, true, 'pectus'], |
| 115 |
|
| 116 |
|
| 117 |
['teef', 4, true, true, 'foot'], |
| 118 |
|
| 119 |
|
| 120 |
['eseeg', 5, true, true, 'goose'], |
| 121 |
|
| 122 |
|
| 123 |
['hteet', 5, true, true, 'tooth'], |
| 124 |
|
| 125 |
|
| 126 |
['swen', 4, true, true, 'news'], |
| 127 |
|
| 128 |
|
| 129 |
['seires', 6, true, true, 'series'], |
| 130 |
|
| 131 |
|
| 132 |
['sei', 3, false, true, 'y'], |
| 133 |
|
| 134 |
|
| 135 |
['sess', 4, true, false, 'ss'], |
| 136 |
|
| 137 |
|
| 138 |
['sesutats', 8, true, true, 'status'], |
| 139 |
|
| 140 |
|
| 141 |
['sel', 3, true, true, 'le'], |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
['ses', 3, true, true, ['s', 'se', 'sis']], |
| 148 |
|
| 149 |
|
| 150 |
['sevit', 5, true, true, 'tive'], |
| 151 |
|
| 152 |
|
| 153 |
['sevird', 6, false, true, 'drive'], |
| 154 |
|
| 155 |
|
| 156 |
['sevi', 4, false, true, 'ife'], |
| 157 |
|
| 158 |
|
| 159 |
['sevom', 5, true, true, 'move'], |
| 160 |
|
| 161 |
|
| 162 |
['sev', 3, true, true, ['f', 've', 'ff']], |
| 163 |
|
| 164 |
|
| 165 |
['sexa', 4, false, false, ['ax', 'axe', 'axis']], |
| 166 |
|
| 167 |
|
| 168 |
['sex', 3, true, false, 'x'], |
| 169 |
|
| 170 |
|
| 171 |
['sezz', 4, true, false, 'z'], |
| 172 |
|
| 173 |
|
| 174 |
['suae', 4, false, true, 'eau'], |
| 175 |
|
| 176 |
|
| 177 |
['see', 3, true, true, 'ee'], |
| 178 |
|
| 179 |
|
| 180 |
['segd', 4, true, true, 'dge'], |
| 181 |
|
| 182 |
|
| 183 |
['segatuo', 7, true, true, 'outage'], |
| 184 |
|
| 185 |
|
| 186 |
['sec', 3, true, true, 'ce'], |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
['se', 2, true, true, ['', 'e']], |
| 192 |
|
| 193 |
|
| 194 |
['sutats', 6, true, true, 'status'], |
| 195 |
|
| 196 |
|
| 197 |
['s', 1, true, true, ''], |
| 198 |
|
| 199 |
|
| 200 |
['xuae', 4, false, true, 'eau'], |
| 201 |
|
| 202 |
|
| 203 |
['elpoep', 6, true, true, 'person'], |
| 204 |
]; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private const SINGULAR_MAP = [ |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
['ybressap', 8, true, true, 'passersby'], |
| 220 |
|
| 221 |
|
| 222 |
['engisni', 7, true, true, ['insignia', 'insignias']], |
| 223 |
|
| 224 |
|
| 225 |
['edon', 4, true, true, 'nodes'], |
| 226 |
|
| 227 |
|
| 228 |
['sixa', 4, false, false, 'axes'], |
| 229 |
|
| 230 |
|
| 231 |
['airetirc', 8, false, false, 'criterion'], |
| 232 |
|
| 233 |
|
| 234 |
['aluben', 6, false, false, 'nebulae'], |
| 235 |
|
| 236 |
|
| 237 |
['dlihc', 5, true, true, 'children'], |
| 238 |
|
| 239 |
|
| 240 |
['eci', 3, false, true, 'ices'], |
| 241 |
|
| 242 |
|
| 243 |
['ecivres', 7, true, true, 'services'], |
| 244 |
|
| 245 |
|
| 246 |
['efi', 3, false, true, 'ives'], |
| 247 |
|
| 248 |
|
| 249 |
['eifles', 6, true, true, 'selfies'], |
| 250 |
|
| 251 |
|
| 252 |
['eivom', 5, true, true, 'movies'], |
| 253 |
|
| 254 |
|
| 255 |
['esuol', 5, false, true, 'lice'], |
| 256 |
|
| 257 |
|
| 258 |
['esuom', 5, false, true, 'mice'], |
| 259 |
|
| 260 |
|
| 261 |
['esoo', 4, false, true, 'eese'], |
| 262 |
|
| 263 |
|
| 264 |
['es', 2, true, true, 'ses'], |
| 265 |
|
| 266 |
|
| 267 |
['esoog', 5, true, true, 'geese'], |
| 268 |
|
| 269 |
|
| 270 |
['ev', 2, true, true, 'ves'], |
| 271 |
|
| 272 |
|
| 273 |
['evird', 5, false, true, 'drives'], |
| 274 |
|
| 275 |
|
| 276 |
['evit', 4, true, true, 'tives'], |
| 277 |
|
| 278 |
|
| 279 |
['evom', 4, true, true, 'moves'], |
| 280 |
|
| 281 |
|
| 282 |
['ffats', 5, true, true, 'staves'], |
| 283 |
|
| 284 |
|
| 285 |
['ff', 2, true, true, 'ffs'], |
| 286 |
|
| 287 |
|
| 288 |
['f', 1, true, true, ['fs', 'ves']], |
| 289 |
|
| 290 |
|
| 291 |
['hc', 2, true, true, 'ches'], |
| 292 |
|
| 293 |
|
| 294 |
['hs', 2, true, true, 'shes'], |
| 295 |
|
| 296 |
|
| 297 |
['htoot', 5, true, true, 'teeth'], |
| 298 |
|
| 299 |
|
| 300 |
['mubla', 5, true, true, 'albums'], |
| 301 |
|
| 302 |
|
| 303 |
['murouq', 6, true, true, ['quora', 'quorums']], |
| 304 |
|
| 305 |
|
| 306 |
['mu', 2, true, true, 'a'], |
| 307 |
|
| 308 |
|
| 309 |
['nam', 3, true, true, 'men'], |
| 310 |
|
| 311 |
|
| 312 |
['nosrep', 6, true, true, ['persons', 'people']], |
| 313 |
|
| 314 |
|
| 315 |
['noiretirc', 9, true, true, 'criteria'], |
| 316 |
|
| 317 |
|
| 318 |
['nonemonehp', 10, true, true, 'phenomena'], |
| 319 |
|
| 320 |
|
| 321 |
['ohce', 4, true, true, 'echoes'], |
| 322 |
|
| 323 |
|
| 324 |
['oreh', 4, true, true, 'heroes'], |
| 325 |
|
| 326 |
|
| 327 |
['salta', 5, true, true, 'atlases'], |
| 328 |
|
| 329 |
|
| 330 |
['saila', 5, true, true, 'aliases'], |
| 331 |
|
| 332 |
|
| 333 |
['siri', 4, true, true, 'irises'], |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
['sis', 3, true, true, 'ses'], |
| 339 |
|
| 340 |
|
| 341 |
['ss', 2, true, false, 'sses'], |
| 342 |
|
| 343 |
|
| 344 |
['suballys', 8, true, true, 'syllabi'], |
| 345 |
|
| 346 |
|
| 347 |
['sub', 3, true, true, 'buses'], |
| 348 |
|
| 349 |
|
| 350 |
['suc', 3, true, true, 'cuses'], |
| 351 |
|
| 352 |
|
| 353 |
['supmacoppih', 11, false, false, 'hippocampi'], |
| 354 |
|
| 355 |
|
| 356 |
['sup', 3, true, true, 'puses'], |
| 357 |
|
| 358 |
|
| 359 |
['sutats', 6, true, true, ['status', 'statuses']], |
| 360 |
|
| 361 |
|
| 362 |
['sutcep', 6, true, true, 'pectuses'], |
| 363 |
|
| 364 |
|
| 365 |
['suxen', 5, false, false, 'nexuses'], |
| 366 |
|
| 367 |
|
| 368 |
['su', 2, true, true, 'i'], |
| 369 |
|
| 370 |
|
| 371 |
['swen', 4, true, true, 'news'], |
| 372 |
|
| 373 |
|
| 374 |
['toof', 4, true, true, 'feet'], |
| 375 |
|
| 376 |
|
| 377 |
['uae', 3, false, true, ['eaus', 'eaux']], |
| 378 |
|
| 379 |
|
| 380 |
['xo', 2, false, false, 'oxen'], |
| 381 |
|
| 382 |
|
| 383 |
['xaoh', 4, true, false, 'hoaxes'], |
| 384 |
|
| 385 |
|
| 386 |
['xedni', 5, false, true, ['indicies', 'indexes']], |
| 387 |
|
| 388 |
|
| 389 |
['xaf', 3, true, true, ['faxes', 'faxxes']], |
| 390 |
|
| 391 |
|
| 392 |
['xo', 2, false, true, 'oxes'], |
| 393 |
|
| 394 |
|
| 395 |
['x', 1, true, false, ['ces', 'xes']], |
| 396 |
|
| 397 |
|
| 398 |
['y', 1, false, true, 'ies'], |
| 399 |
|
| 400 |
|
| 401 |
['ziuq', 4, true, false, 'quizzes'], |
| 402 |
|
| 403 |
|
| 404 |
['z', 1, true, true, 'zes'], |
| 405 |
]; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private const UNINFLECTED = [ |
| 411 |
'', |
| 412 |
|
| 413 |
|
| 414 |
'atad', |
| 415 |
|
| 416 |
|
| 417 |
'reed', |
| 418 |
|
| 419 |
|
| 420 |
'tnempiuqe', |
| 421 |
|
| 422 |
|
| 423 |
'kcabdeef', |
| 424 |
|
| 425 |
|
| 426 |
'hsif', |
| 427 |
|
| 428 |
|
| 429 |
'htlaeh', |
| 430 |
|
| 431 |
|
| 432 |
'yrotsih', |
| 433 |
|
| 434 |
|
| 435 |
'ofni', |
| 436 |
|
| 437 |
|
| 438 |
'noitamrofni', |
| 439 |
|
| 440 |
|
| 441 |
'yenom', |
| 442 |
|
| 443 |
|
| 444 |
'esoom', |
| 445 |
|
| 446 |
|
| 447 |
'seires', |
| 448 |
|
| 449 |
|
| 450 |
'peehs', |
| 451 |
|
| 452 |
|
| 453 |
'seiceps', |
| 454 |
|
| 455 |
|
| 456 |
'ciffart', |
| 457 |
|
| 458 |
|
| 459 |
'tfarcria', |
| 460 |
|
| 461 |
|
| 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 |
|
| 472 |
if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) { |
| 473 |
return [$plural]; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
foreach (self::PLURAL_MAP as $map) { |
| 481 |
$suffix = $map[0]; |
| 482 |
$suffixLength = $map[1]; |
| 483 |
$j = 0; |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
while ($suffix[$j] === $lowerPluralRev[$j]) { |
| 488 |
|
| 489 |
++$j; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
if ($j === $suffixLength) { |
| 494 |
|
| 495 |
if ($j < $pluralLength) { |
| 496 |
$nextIsVowel = str_contains('aeiou', $lowerPluralRev[$j]); |
| 497 |
|
| 498 |
if (!$map[2] && $nextIsVowel) { |
| 499 |
|
| 500 |
break; |
| 501 |
} |
| 502 |
|
| 503 |
if (!$map[3] && !$nextIsVowel) { |
| 504 |
|
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
$newBase = substr($plural, 0, $pluralLength - $suffixLength); |
| 510 |
$newSuffix = $map[4]; |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 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 |
|
| 531 |
if ($j === $pluralLength) { |
| 532 |
break; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 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 |
|
| 548 |
if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) { |
| 549 |
return [$singular]; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
foreach (self::SINGULAR_MAP as $map) { |
| 557 |
$suffix = $map[0]; |
| 558 |
$suffixLength = $map[1]; |
| 559 |
$j = 0; |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
while ($suffix[$j] === $lowerSingularRev[$j]) { |
| 565 |
|
| 566 |
++$j; |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
if ($j === $suffixLength) { |
| 571 |
|
| 572 |
if ($j < $singularLength) { |
| 573 |
$nextIsVowel = str_contains('aeiou', $lowerSingularRev[$j]); |
| 574 |
|
| 575 |
if (!$map[2] && $nextIsVowel) { |
| 576 |
|
| 577 |
break; |
| 578 |
} |
| 579 |
|
| 580 |
if (!$map[3] && !$nextIsVowel) { |
| 581 |
|
| 582 |
break; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
$newBase = substr($singular, 0, $singularLength - $suffixLength); |
| 587 |
$newSuffix = $map[4]; |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 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 |
|
| 608 |
if ($j === $singularLength) { |
| 609 |
break; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
return [$singular.'s']; |
| 616 |
} |
| 617 |
} |