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