FrenchInflector.php
130 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 | namespace IAWPSCOPED\Symfony\Component\String\Inflector; |
| 12 | |
| 13 | /** |
| 14 | * French inflector. |
| 15 | * |
| 16 | * This class does only inflect nouns; not adjectives nor composed words like "soixante-dix". |
| 17 | * @internal |
| 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 | // Words finishing with "s", "x" or "z" are invariables |
| 30 | // Les mots finissant par "s", "x" ou "z" sont invariables |
| 31 | ['/(s|x|z)$/i', '\\1'], |
| 32 | // Words finishing with "eau" are pluralized with a "x" |
| 33 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 34 | ['/(eau)$/i', '\\1x'], |
| 35 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 36 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 37 | ['/^(landau)$/i', '\\1s'], |
| 38 | ['/(au)$/i', '\\1x'], |
| 39 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 40 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 41 | ['/^(pneu|bleu|émeu)$/i', '\\1s'], |
| 42 | ['/(eu)$/i', '\\1x'], |
| 43 | // Words finishing with "al" are pluralized with a "aux" excepted |
| 44 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 45 | ['/^(bal|carnaval|caracal|chacal|choral|corral|étal|festival|récital|val)$/i', '\\1s'], |
| 46 | ['/al$/i', '\\1aux'], |
| 47 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 48 | ['/^(aspir|b|cor|ém|ferm|soupir|trav|vant|vitr)ail$/i', '\\1aux'], |
| 49 | // Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel |
| 50 | ['/^(bij|caill|ch|gen|hib|jouj|p)ou$/i', '\\1oux'], |
| 51 | // Invariable words |
| 52 | ['/^(cinquante|soixante|mille)$/i', '\\1'], |
| 53 | // French titles |
| 54 | ['/^(mon|ma)(sieur|dame|demoiselle|seigneur)$/', 'IAWPSCOPED\\mes\\2s'], |
| 55 | ['/^(Mon|Ma)(sieur|dame|demoiselle|seigneur)$/', 'IAWPSCOPED\\Mes\\2s'], |
| 56 | ]; |
| 57 | /** |
| 58 | * A list of all rules for singularize. |
| 59 | */ |
| 60 | private const SINGULARIZE_REGEXP = [ |
| 61 | // First entry: regexp |
| 62 | // Second entry: replacement |
| 63 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 64 | ['/((aspir|b|cor|ém|ferm|soupir|trav|vant|vitr))aux$/i', '\\1ail'], |
| 65 | // Words finishing with "eau" are pluralized with a "x" |
| 66 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 67 | ['/(eau)x$/i', '\\1'], |
| 68 | // Words finishing with "al" are pluralized with a "aux" expected |
| 69 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 70 | ['/(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'], |
| 71 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 72 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 73 | ['/(au)x$/i', '\\1'], |
| 74 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 75 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 76 | ['/(eu)x$/i', '\\1'], |
| 77 | // Words finishing with "ou" are pluralized with a "s" excepted bijou, caillou, chou, genou, hibou, joujou, pou |
| 78 | // Les mots finissant par "ou" prennent un "s" sauf bijou, caillou, chou, genou, hibou, joujou, pou |
| 79 | ['/(bij|caill|ch|gen|hib|jouj|p)oux$/i', '\\1ou'], |
| 80 | // French titles |
| 81 | ['/^mes(dame|demoiselle)s$/', 'IAWPSCOPED\\ma\\1'], |
| 82 | ['/^Mes(dame|demoiselle)s$/', 'IAWPSCOPED\\Ma\\1'], |
| 83 | ['/^mes(sieur|seigneur)s$/', 'IAWPSCOPED\\mon\\1'], |
| 84 | ['/^Mes(sieur|seigneur)s$/', 'IAWPSCOPED\\Mon\\1'], |
| 85 | // Default rule |
| 86 | ['/s$/i', ''], |
| 87 | ]; |
| 88 | /** |
| 89 | * A list of words which should not be inflected. |
| 90 | * This list is only used by singularize. |
| 91 | */ |
| 92 | 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'; |
| 93 | /** |
| 94 | * {@inheritdoc} |
| 95 | */ |
| 96 | public function singularize(string $plural) : array |
| 97 | { |
| 98 | if ($this->isInflectedWord($plural)) { |
| 99 | return [$plural]; |
| 100 | } |
| 101 | foreach (self::SINGULARIZE_REGEXP as $rule) { |
| 102 | [$regexp, $replace] = $rule; |
| 103 | if (1 === \preg_match($regexp, $plural)) { |
| 104 | return [\preg_replace($regexp, $replace, $plural)]; |
| 105 | } |
| 106 | } |
| 107 | return [$plural]; |
| 108 | } |
| 109 | /** |
| 110 | * {@inheritdoc} |
| 111 | */ |
| 112 | public function pluralize(string $singular) : array |
| 113 | { |
| 114 | if ($this->isInflectedWord($singular)) { |
| 115 | return [$singular]; |
| 116 | } |
| 117 | foreach (self::PLURALIZE_REGEXP as $rule) { |
| 118 | [$regexp, $replace] = $rule; |
| 119 | if (1 === \preg_match($regexp, $singular)) { |
| 120 | return [\preg_replace($regexp, $replace, $singular)]; |
| 121 | } |
| 122 | } |
| 123 | return [$singular . 's']; |
| 124 | } |
| 125 | private function isInflectedWord(string $word) : bool |
| 126 | { |
| 127 | return 1 === \preg_match(self::UNINFLECTED, $word); |
| 128 | } |
| 129 | } |
| 130 |