123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- <?php
- namespace Symfony\Component\String\Inflector;
- final class EnglishInflector implements InflectorInterface
- {
-
- private const PLURAL_MAP = [
-
-
-
-
-
-
- ['a', 1, true, true, ['on', 'um']],
-
- ['ea', 2, true, true, 'a'],
-
- ['secivres', 8, true, true, 'service'],
-
- ['eci', 3, false, true, 'ouse'],
-
- ['esee', 4, false, true, 'oose'],
-
- ['i', 1, true, true, 'us'],
-
- ['nem', 3, true, true, 'man'],
-
- ['nerdlihc', 8, true, true, 'child'],
-
- ['nexo', 4, false, false, 'ox'],
-
- ['seci', 4, false, true, ['ex', 'ix', 'ice']],
-
- ['sedoc', 5, false, true, 'code'],
-
- ['seifles', 7, true, true, 'selfie'],
-
- ['seibmoz', 7, true, true, 'zombie'],
-
- ['seivom', 6, true, true, 'movie'],
-
- ['seman', 5, true, false, 'name'],
-
- ['sesutcep', 8, true, true, 'pectus'],
-
- ['teef', 4, true, true, 'foot'],
-
- ['eseeg', 5, true, true, 'goose'],
-
- ['hteet', 5, true, true, 'tooth'],
-
- ['swen', 4, true, true, 'news'],
-
- ['seires', 6, true, true, 'series'],
-
- ['sei', 3, false, true, 'y'],
-
- ['sess', 4, true, false, 'ss'],
-
- ['sesutats', 8, true, true, 'status'],
-
-
-
-
- ['ses', 3, true, true, ['s', 'se', 'sis']],
-
- ['sevit', 5, true, true, 'tive'],
-
- ['sevird', 6, false, true, 'drive'],
-
- ['sevi', 4, false, true, 'ife'],
-
- ['sevom', 5, true, true, 'move'],
-
- ['sev', 3, true, true, ['f', 've', 'ff']],
-
- ['sexa', 4, false, false, ['ax', 'axe', 'axis']],
-
- ['sex', 3, true, false, 'x'],
-
- ['sezz', 4, true, false, 'z'],
-
- ['suae', 4, false, true, 'eau'],
-
- ['see', 3, true, true, 'ee'],
-
- ['segd', 4, true, true, 'dge'],
-
-
-
- ['se', 2, true, true, ['', 'e']],
-
- ['sutats', 6, true, true, 'status'],
-
- ['s', 1, true, true, ''],
-
- ['xuae', 4, false, true, 'eau'],
-
- ['elpoep', 6, true, true, 'person'],
- ];
-
- private const SINGULAR_MAP = [
-
-
-
-
-
-
- ['airetirc', 8, false, false, 'criterion'],
-
- ['aluben', 6, false, false, 'nebulae'],
-
- ['dlihc', 5, true, true, 'children'],
-
- ['eci', 3, false, true, 'ices'],
-
- ['ecivres', 7, true, true, 'services'],
-
- ['efi', 3, false, true, 'ives'],
-
- ['eifles', 6, true, true, 'selfies'],
-
- ['eivom', 5, true, true, 'movies'],
-
- ['esuol', 5, false, true, 'lice'],
-
- ['esuom', 5, false, true, 'mice'],
-
- ['esoo', 4, false, true, 'eese'],
-
- ['es', 2, true, true, 'ses'],
-
- ['esoog', 5, true, true, 'geese'],
-
- ['ev', 2, true, true, 'ves'],
-
- ['evird', 5, false, true, 'drives'],
-
- ['evit', 4, true, true, 'tives'],
-
- ['evom', 4, true, true, 'moves'],
-
- ['ffats', 5, true, true, 'staves'],
-
- ['ff', 2, true, true, 'ffs'],
-
- ['f', 1, true, true, ['fs', 'ves']],
-
- ['hc', 2, true, true, 'ches'],
-
- ['hs', 2, true, true, 'shes'],
-
- ['htoot', 5, true, true, 'teeth'],
-
- ['mu', 2, true, true, 'a'],
-
- ['nam', 3, true, true, 'men'],
-
- ['nosrep', 6, true, true, ['persons', 'people']],
-
- ['noi', 3, true, true, 'ions'],
-
- ['nop', 3, true, true, 'pons'],
-
- ['nos', 3, true, true, 'sons'],
-
- ['noc', 3, true, true, 'cons'],
-
- ['no', 2, true, true, 'a'],
-
- ['ohce', 4, true, true, 'echoes'],
-
- ['oreh', 4, true, true, 'heroes'],
-
- ['salta', 5, true, true, 'atlases'],
-
- ['siri', 4, true, true, 'irises'],
-
-
-
- ['sis', 3, true, true, 'ses'],
-
- ['ss', 2, true, false, 'sses'],
-
- ['suballys', 8, true, true, 'syllabi'],
-
- ['sub', 3, true, true, 'buses'],
-
- ['suc', 3, true, true, 'cuses'],
-
- ['sutats', 6, true, true, ['status', 'statuses']],
-
- ['sutcep', 6, true, true, 'pectuses'],
-
- ['su', 2, true, true, 'i'],
-
- ['swen', 4, true, true, 'news'],
-
- ['toof', 4, true, true, 'feet'],
-
- ['uae', 3, false, true, ['eaus', 'eaux']],
-
- ['xo', 2, false, false, 'oxen'],
-
- ['xaoh', 4, true, false, 'hoaxes'],
-
- ['xedni', 5, false, true, ['indicies', 'indexes']],
-
- ['xo', 2, false, true, 'oxes'],
-
- ['x', 1, true, false, ['cies', 'xes']],
-
- ['xi', 2, false, true, 'ices'],
-
- ['y', 1, false, true, 'ies'],
-
- ['ziuq', 4, true, false, 'quizzes'],
-
- ['z', 1, true, true, 'zes'],
- ];
-
- private const UNINFLECTED = [
- '',
-
- 'atad',
-
- 'reed',
-
- 'tnempiuqe',
-
- 'kcabdeef',
-
- 'hsif',
-
- 'htlaeh',
-
- 'yrotsih',
-
- 'ofni',
-
- 'noitamrofni',
-
- 'yenom',
-
- 'esoom',
-
- 'seires',
-
- 'peehs',
-
- 'seiceps',
-
- 'ciffart',
- ];
-
- public function singularize(string $plural): array
- {
- $pluralRev = strrev($plural);
- $lowerPluralRev = strtolower($pluralRev);
- $pluralLength = \strlen($lowerPluralRev);
-
- if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
- return [$plural];
- }
-
-
-
-
- foreach (self::PLURAL_MAP as $map) {
- $suffix = $map[0];
- $suffixLength = $map[1];
- $j = 0;
-
-
- while ($suffix[$j] === $lowerPluralRev[$j]) {
-
- ++$j;
-
-
- if ($j === $suffixLength) {
-
- if ($j < $pluralLength) {
- $nextIsVowel = false !== strpos('aeiou', $lowerPluralRev[$j]);
- if (!$map[2] && $nextIsVowel) {
-
- break;
- }
- if (!$map[3] && !$nextIsVowel) {
-
- break;
- }
- }
- $newBase = substr($plural, 0, $pluralLength - $suffixLength);
- $newSuffix = $map[4];
-
-
-
- $firstUpper = ctype_upper($pluralRev[$j - 1]);
- if (\is_array($newSuffix)) {
- $singulars = [];
- foreach ($newSuffix as $newSuffixEntry) {
- $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
- }
- return $singulars;
- }
- return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
- }
-
- if ($j === $pluralLength) {
- break;
- }
- }
- }
-
- return [$plural];
- }
-
- public function pluralize(string $singular): array
- {
- $singularRev = strrev($singular);
- $lowerSingularRev = strtolower($singularRev);
- $singularLength = \strlen($lowerSingularRev);
-
- if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
- return [$singular];
- }
-
-
-
-
- foreach (self::SINGULAR_MAP as $map) {
- $suffix = $map[0];
- $suffixLength = $map[1];
- $j = 0;
-
-
- while ($suffix[$j] === $lowerSingularRev[$j]) {
-
- ++$j;
-
-
- if ($j === $suffixLength) {
-
- if ($j < $singularLength) {
- $nextIsVowel = false !== strpos('aeiou', $lowerSingularRev[$j]);
- if (!$map[2] && $nextIsVowel) {
-
- break;
- }
- if (!$map[3] && !$nextIsVowel) {
-
- break;
- }
- }
- $newBase = substr($singular, 0, $singularLength - $suffixLength);
- $newSuffix = $map[4];
-
-
-
- $firstUpper = ctype_upper($singularRev[$j - 1]);
- if (\is_array($newSuffix)) {
- $plurals = [];
- foreach ($newSuffix as $newSuffixEntry) {
- $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
- }
- return $plurals;
- }
- return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
- }
-
- if ($j === $singularLength) {
- break;
- }
- }
- }
-
- return [$singular.'s'];
- }
- }
|