FrenchInflector.php 5.9 KB

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