FrenchInflector.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | /** |
| 15 | * French inflector. |
| 16 | * |
| 17 | * This class does only inflect nouns; not adjectives nor composed words like "soixante-dix". |
| 18 | */ |
| 19 | final class FrenchInflector implements InflectorInterface |
| 20 | { |
| 21 | /** |
| 22 | * A list of all rules for pluralise. |
| 23 | * |
| 24 | * @see https://la-conjugaison.nouvelobs.com/regles/grammaire/le-pluriel-des-noms-121.php |
| 25 | */ |
| 26 | private const PLURALIZE_REGEXP = [ |
| 27 | // First entry: regexp |
| 28 | // Second entry: replacement |
| 29 | |
| 30 | // Words finishing with "s", "x" or "z" are invariables |
| 31 | // Les mots finissant par "s", "x" ou "z" sont invariables |
| 32 | ['/(s|x|z)$/i', '\1'], |
| 33 | |
| 34 | // Words finishing with "eau" are pluralized with a "x" |
| 35 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 36 | ['/(eau)$/i', '\1x'], |
| 37 | |
| 38 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 39 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 40 | ['/^(landau)$/i', '\1s'], |
| 41 | ['/(au)$/i', '\1x'], |
| 42 | |
| 43 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 44 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 45 | ['/^(pneu|bleu|émeu)$/i', '\1s'], |
| 46 | ['/(eu)$/i', '\1x'], |
| 47 | |
| 48 | // Words finishing with "al" are pluralized with a "aux" excepted |
| 49 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 50 | ['/^(bal|carnaval|caracal|chacal|choral|corral|étal|festival|récital|val)$/i', '\1s'], |
| 51 | ['/al$/i', '\1aux'], |
| 52 | |
| 53 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 54 | ['/^(aspir|b|cor|ém|ferm|soupir|trav|vant|vitr)ail$/i', '\1aux'], |
| 55 | |
| 56 | // Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel |
| 57 | ['/^(bij|caill|ch|gen|hib|jouj|p)ou$/i', '\1oux'], |
| 58 | |
| 59 | // Invariable words |
| 60 | ['/^(cinquante|soixante|mille)$/i', '\1'], |
| 61 | |
| 62 | // French titles |
| 63 | ['/^(mon|ma)(sieur|dame|demoiselle|seigneur)$/', 'mes\2s'], |
| 64 | ['/^(Mon|Ma)(sieur|dame|demoiselle|seigneur)$/', 'Mes\2s'], |
| 65 | ]; |
| 66 | |
| 67 | /** |
| 68 | * A list of all rules for singularize. |
| 69 | */ |
| 70 | private const SINGULARIZE_REGEXP = [ |
| 71 | // First entry: regexp |
| 72 | // Second entry: replacement |
| 73 | |
| 74 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 75 | ['/((aspir|b|cor|ém|ferm|soupir|trav|vant|vitr))aux$/i', '\1ail'], |
| 76 | |
| 77 | // Words finishing with "eau" are pluralized with a "x" |
| 78 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 79 | ['/(eau)x$/i', '\1'], |
| 80 | |
| 81 | // Words finishing with "al" are pluralized with a "aux" expected |
| 82 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 83 | ['/(amir|anim|arsen|boc|can|capit|capor|chev|crist|génér|hopit|hôpit|idé|journ|littor|loc|m|mét|minér|princip|radic|termin)aux$/i', '\1al'], |
| 84 | |
| 85 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 86 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 87 | ['/(au)x$/i', '\1'], |
| 88 | |
| 89 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 90 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 91 | ['/(eu)x$/i', '\1'], |
| 92 | |
| 93 | // Words finishing with "ou" are pluralized with a "s" excepted bijou, caillou, chou, genou, hibou, joujou, pou |
| 94 | // Les mots finissant par "ou" prennent un "s" sauf bijou, caillou, chou, genou, hibou, joujou, pou |
| 95 | ['/(bij|caill|ch|gen|hib|jouj|p)oux$/i', '\1ou'], |
| 96 | |
| 97 | // French titles |
| 98 | ['/^mes(dame|demoiselle)s$/', 'ma\1'], |
| 99 | ['/^Mes(dame|demoiselle)s$/', 'Ma\1'], |
| 100 | ['/^mes(sieur|seigneur)s$/', 'mon\1'], |
| 101 | ['/^Mes(sieur|seigneur)s$/', 'Mon\1'], |
| 102 | |
| 103 | // Default rule |
| 104 | ['/s$/i', ''], |
| 105 | ]; |
| 106 | |
| 107 | /** |
| 108 | * A list of words which should not be inflected. |
| 109 | * This list is only used by singularize. |
| 110 | */ |
| 111 | private const UNINFLECTED = '/^(abcès|accès|abus|albatros|anchois|anglais|autobus|bois|brebis|carquois|cas|chas|colis|concours|corps|cours|cyprès|décès|devis|discours|dos|embarras|engrais|entrelacs|excès|fils|fois|gâchis|gars|glas|héros|intrus|jars|jus|kermès|lacis|legs|lilas|marais|mars|matelas|mépris|mets|mois|mors|obus|os|palais|paradis|parcours|pardessus|pays|plusieurs|poids|pois|pouls|printemps|processus|progrès|puits|pus|rabais|radis|recors|recours|refus|relais|remords|remous|rictus|rhinocéros|repas|rubis|sans|sas|secours|sens|souris|succès|talus|tapis|tas|taudis|temps|tiers|univers|velours|verglas|vernis|virus)$/i'; |
| 112 | |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function singularize(string $plural): array |
| 117 | { |
| 118 | if ($this->isInflectedWord($plural)) { |
| 119 | return [$plural]; |
| 120 | } |
| 121 | |
| 122 | foreach (self::SINGULARIZE_REGEXP as $rule) { |
| 123 | [$regexp, $replace] = $rule; |
| 124 | |
| 125 | if (1 === preg_match($regexp, $plural)) { |
| 126 | return [preg_replace($regexp, $replace, $plural)]; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return [$plural]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritdoc} |
| 135 | */ |
| 136 | public function pluralize(string $singular): array |
| 137 | { |
| 138 | if ($this->isInflectedWord($singular)) { |
| 139 | return [$singular]; |
| 140 | } |
| 141 | |
| 142 | foreach (self::PLURALIZE_REGEXP as $rule) { |
| 143 | [$regexp, $replace] = $rule; |
| 144 | |
| 145 | if (1 === preg_match($regexp, $singular)) { |
| 146 | return [preg_replace($regexp, $replace, $singular)]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return [$singular.'s']; |
| 151 | } |
| 152 | |
| 153 | private function isInflectedWord(string $word): bool |
| 154 | { |
| 155 | return 1 === preg_match(self::UNINFLECTED, $word); |
| 156 | } |
| 157 | } |
| 158 |