This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Inflector/Inflector.php

494 lines
15 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Inflector;
/**
* Converts words between singular and plural forms.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class Inflector
{
/**
* Map English plural to singular suffixes.
*
* @see http://english-zone.com/spelling/plurals.html
*/
2019-01-16 09:39:14 +00:00
private static $pluralMap = [
// First entry: plural suffix, reversed
// Second entry: length of plural suffix
// Third entry: Whether the suffix may succeed a vocal
// Fourth entry: Whether the suffix may succeed a consonant
// Fifth entry: singular suffix, normal
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
2019-01-16 09:39:14 +00:00
['a', 1, true, true, ['on', 'um']],
// nebulae (nebula)
2019-01-16 09:39:14 +00:00
['ea', 2, true, true, 'a'],
// services (service)
2019-01-16 09:39:14 +00:00
['secivres', 8, true, true, 'service'],
// mice (mouse), lice (louse)
2019-01-16 09:39:14 +00:00
['eci', 3, false, true, 'ouse'],
// geese (goose)
2019-01-16 09:39:14 +00:00
['esee', 4, false, true, 'oose'],
// fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
2019-01-16 09:39:14 +00:00
['i', 1, true, true, 'us'],
// men (man), women (woman)
2019-01-16 09:39:14 +00:00
['nem', 3, true, true, 'man'],
// children (child)
2019-01-16 09:39:14 +00:00
['nerdlihc', 8, true, true, 'child'],
// oxen (ox)
2019-01-16 09:39:14 +00:00
['nexo', 4, false, false, 'ox'],
// indices (index), appendices (appendix), prices (price)
2019-01-16 09:39:14 +00:00
['seci', 4, false, true, ['ex', 'ix', 'ice']],
// selfies (selfie)
2019-01-16 09:39:14 +00:00
['seifles', 7, true, true, 'selfie'],
// movies (movie)
2019-01-16 09:39:14 +00:00
['seivom', 6, true, true, 'movie'],
2016-05-29 13:11:17 +01:00
// feet (foot)
2019-01-16 09:39:14 +00:00
['teef', 4, true, true, 'foot'],
2016-05-29 13:11:17 +01:00
// geese (goose)
2019-01-16 09:39:14 +00:00
['eseeg', 5, true, true, 'goose'],
2016-05-29 13:11:17 +01:00
// teeth (tooth)
2019-01-16 09:39:14 +00:00
['hteet', 5, true, true, 'tooth'],
2016-05-29 13:11:17 +01:00
// news (news)
2019-01-16 09:39:14 +00:00
['swen', 4, true, true, 'news'],
// series (series)
2019-01-16 09:39:14 +00:00
['seires', 6, true, true, 'series'],
// babies (baby)
2019-01-16 09:39:14 +00:00
['sei', 3, false, true, 'y'],
// accesses (access), addresses (address), kisses (kiss)
2019-01-16 09:39:14 +00:00
['sess', 4, true, false, 'ss'],
// analyses (analysis), ellipses (ellipsis), fungi (fungus),
// neuroses (neurosis), theses (thesis), emphases (emphasis),
// oases (oasis), crises (crisis), houses (house), bases (base),
// atlases (atlas)
2019-01-16 09:39:14 +00:00
['ses', 3, true, true, ['s', 'se', 'sis']],
// objectives (objective), alternative (alternatives)
2019-01-16 09:39:14 +00:00
['sevit', 5, true, true, 'tive'],
// drives (drive)
2019-01-16 09:39:14 +00:00
['sevird', 6, false, true, 'drive'],
// lives (life), wives (wife)
2019-01-16 09:39:14 +00:00
['sevi', 4, false, true, 'ife'],
// moves (move)
2019-01-16 09:39:14 +00:00
['sevom', 5, true, true, 'move'],
// hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
2019-01-16 09:39:14 +00:00
['sev', 3, true, true, ['f', 've', 'ff']],
// axes (axis), axes (ax), axes (axe)
2019-01-16 09:39:14 +00:00
['sexa', 4, false, false, ['ax', 'axe', 'axis']],
// indexes (index), matrixes (matrix)
2019-01-16 09:39:14 +00:00
['sex', 3, true, false, 'x'],
// quizzes (quiz)
2019-01-16 09:39:14 +00:00
['sezz', 4, true, false, 'z'],
// bureaus (bureau)
2019-01-16 09:39:14 +00:00
['suae', 4, false, true, 'eau'],
// roses (rose), garages (garage), cassettes (cassette),
// waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
// shoes (shoe)
2019-01-16 09:39:14 +00:00
['se', 2, true, true, ['', 'e']],
// tags (tag)
2019-01-16 09:39:14 +00:00
['s', 1, true, true, ''],
// chateaux (chateau)
2019-01-16 09:39:14 +00:00
['xuae', 4, false, true, 'eau'],
2016-05-20 17:03:47 +01:00
// people (person)
2019-01-16 09:39:14 +00:00
['elpoep', 6, true, true, 'person'],
];
2018-04-11 18:33:35 +01:00
/**
* Map English singular to plural suffixes.
*
* @see http://english-zone.com/spelling/plurals.html
*/
private static $singularMap = [
2018-04-11 18:33:35 +01:00
// First entry: singular suffix, reversed
// Second entry: length of singular suffix
// Third entry: Whether the suffix may succeed a vocal
// Fourth entry: Whether the suffix may succeed a consonant
// Fifth entry: plural suffix, normal
// criterion (criteria)
['airetirc', 8, false, false, 'criterion'],
2018-04-11 18:33:35 +01:00
// nebulae (nebula)
['aluben', 6, false, false, 'nebulae'],
2018-04-11 18:33:35 +01:00
// children (child)
['dlihc', 5, true, true, 'children'],
2018-04-11 18:33:35 +01:00
// prices (price)
['eci', 3, false, true, 'ices'],
2018-04-11 18:33:35 +01:00
// services (service)
['ecivres', 7, true, true, 'services'],
2018-04-11 18:33:35 +01:00
// lives (life), wives (wife)
['efi', 3, false, true, 'ives'],
2018-04-11 18:33:35 +01:00
// selfies (selfie)
['eifles', 6, true, true, 'selfies'],
2018-04-11 18:33:35 +01:00
// movies (movie)
['eivom', 5, true, true, 'movies'],
2018-04-11 18:33:35 +01:00
// lice (louse)
['esuol', 5, false, true, 'lice'],
2018-04-11 18:33:35 +01:00
// mice (mouse)
['esuom', 5, false, true, 'mice'],
2018-04-11 18:33:35 +01:00
// geese (goose)
['esoo', 4, false, true, 'eese'],
2018-04-11 18:33:35 +01:00
// houses (house), bases (base)
['es', 2, true, true, 'ses'],
2018-04-11 18:33:35 +01:00
// geese (goose)
['esoog', 5, true, true, 'geese'],
2018-04-11 18:33:35 +01:00
// caves (cave)
['ev', 2, true, true, 'ves'],
2018-04-11 18:33:35 +01:00
// drives (drive)
['evird', 5, false, true, 'drives'],
2018-04-11 18:33:35 +01:00
// objectives (objective), alternative (alternatives)
['evit', 4, true, true, 'tives'],
2018-04-11 18:33:35 +01:00
// moves (move)
['evom', 4, true, true, 'moves'],
2018-04-11 18:33:35 +01:00
// staves (staff)
['ffats', 5, true, true, 'staves'],
2018-04-11 18:33:35 +01:00
// hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
['ff', 2, true, true, 'ffs'],
2018-04-11 18:33:35 +01:00
// hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
['f', 1, true, true, ['fs', 'ves']],
2018-04-11 18:33:35 +01:00
// arches (arch)
['hc', 2, true, true, 'ches'],
2018-04-11 18:33:35 +01:00
// bushes (bush)
['hs', 2, true, true, 'shes'],
2018-04-11 18:33:35 +01:00
// teeth (tooth)
['htoot', 5, true, true, 'teeth'],
2018-04-11 18:33:35 +01:00
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
['mu', 2, true, true, 'a'],
2018-04-11 18:33:35 +01:00
// men (man), women (woman)
['nam', 3, true, true, 'men'],
2018-04-11 18:33:35 +01:00
// people (person)
['nosrep', 6, true, true, ['persons', 'people']],
2018-04-11 18:33:35 +01:00
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
['noi', 3, true, true, 'ions'],
2018-04-11 18:33:35 +01:00
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
['no', 2, true, true, 'a'],
// echoes (echo)
['ohce', 4, true, true, 'echoes'],
// heroes (hero)
['oreh', 4, true, true, 'heroes'],
2018-04-11 18:33:35 +01:00
// atlases (atlas)
['salta', 5, true, true, 'atlases'],
2018-04-11 18:33:35 +01:00
// irises (iris)
['siri', 4, true, true, 'irises'],
2018-04-11 18:33:35 +01:00
// analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
// theses (thesis), emphases (emphasis), oases (oasis),
// crises (crisis)
['sis', 3, true, true, 'ses'],
2018-04-11 18:33:35 +01:00
// accesses (access), addresses (address), kisses (kiss)
['ss', 2, true, false, 'sses'],
2018-04-11 18:33:35 +01:00
// syllabi (syllabus)
['suballys', 8, true, true, 'syllabi'],
2018-04-11 18:33:35 +01:00
// buses (bus)
['sub', 3, true, true, 'buses'],
// circuses (circus)
['suc', 3, true, true, 'cuses'],
2018-04-11 18:33:35 +01:00
// fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
['su', 2, true, true, 'i'],
2018-04-11 18:33:35 +01:00
// news (news)
['swen', 4, true, true, 'news'],
2018-04-11 18:33:35 +01:00
// feet (foot)
['toof', 4, true, true, 'feet'],
2018-04-11 18:33:35 +01:00
// chateaux (chateau), bureaus (bureau)
['uae', 3, false, true, ['eaus', 'eaux']],
2018-04-11 18:33:35 +01:00
// oxen (ox)
['xo', 2, false, false, 'oxen'],
2018-04-11 18:33:35 +01:00
// hoaxes (hoax)
['xaoh', 4, true, false, 'hoaxes'],
2018-04-11 18:33:35 +01:00
// indices (index)
['xedni', 5, false, true, ['indicies', 'indexes']],
2018-04-11 18:33:35 +01:00
// indexes (index), matrixes (matrix)
['x', 1, true, false, ['cies', 'xes']],
2018-04-11 18:33:35 +01:00
// appendices (appendix)
['xi', 2, false, true, 'ices'],
2018-04-11 18:33:35 +01:00
// babies (baby)
['y', 1, false, true, 'ies'],
2018-04-11 18:33:35 +01:00
// quizzes (quiz)
['ziuq', 4, true, false, 'quizzes'],
2018-04-11 18:33:35 +01:00
// waltzes (waltz)
['z', 1, true, true, 'zes'],
];
2018-04-11 18:33:35 +01:00
/**
* A list of words which should not be inflected, reversed.
2018-04-11 18:33:35 +01:00
*/
private static $uninflected = [
'atad',
'reed',
'kcabdeef',
'hsif',
'ofni',
'esoom',
'seires',
'peehs',
];
2018-04-11 18:33:35 +01:00
/**
* This class should not be instantiated.
*/
private function __construct()
{
}
/**
* Returns the singular form of a word.
*
* If the method can't determine the form with certainty, an array of the
* possible singulars is returned.
*
* @param string $plural A word in plural form
*
* @return string|array The singular form or an array of possible singular forms
*/
public static function singularize(string $plural)
{
$pluralRev = strrev($plural);
$lowerPluralRev = strtolower($pluralRev);
$pluralLength = \strlen($lowerPluralRev);
2018-04-11 18:33:35 +01:00
// Check if the word is one which is not inflected, return early if so
if (\in_array($lowerPluralRev, self::$uninflected, true)) {
2018-04-11 18:33:35 +01:00
return $plural;
}
// The outer loop iterates over the entries of the plural table
// The inner loop $j iterates over the characters of the plural suffix
// in the plural table to compare them with the characters of the actual
// given plural suffix
foreach (self::$pluralMap as $map) {
$suffix = $map[0];
$suffixLength = $map[1];
$j = 0;
// Compare characters in the plural table and of the suffix of the
// given plural one by one
while ($suffix[$j] === $lowerPluralRev[$j]) {
// Let $j point to the next character
++$j;
// Successfully compared the last character
// Add an entry with the singular suffix to the singular array
if ($j === $suffixLength) {
// Is there any character preceding the suffix in the plural string?
if ($j < $pluralLength) {
$nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
if (!$map[2] && $nextIsVocal) {
// suffix may not succeed a vocal but next char is one
break;
}
if (!$map[3] && !$nextIsVocal) {
// suffix may not succeed a consonant but next char is one
break;
}
}
$newBase = substr($plural, 0, $pluralLength - $suffixLength);
$newSuffix = $map[4];
// Check whether the first character in the plural suffix
// is uppercased. If yes, uppercase the first character in
// the singular suffix too
$firstUpper = ctype_upper($pluralRev[$j - 1]);
if (\is_array($newSuffix)) {
2019-01-16 09:39:14 +00:00
$singulars = [];
foreach ($newSuffix as $newSuffixEntry) {
$singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
}
return $singulars;
}
return $newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix);
}
// Suffix is longer than word
if ($j === $pluralLength) {
break;
}
}
}
// Assume that plural and singular is identical
return $plural;
}
2018-04-11 18:33:35 +01:00
/**
* Returns the plural form of a word.
*
* If the method can't determine the form with certainty, an array of the
* possible plurals is returned.
*
* @param string $singular A word in singular form
2018-04-11 18:33:35 +01:00
*
* @return string|array The plural form or an array of possible plural forms
2018-04-11 18:33:35 +01:00
*/
public static function pluralize(string $singular)
{
$singularRev = strrev($singular);
$lowerSingularRev = strtolower($singularRev);
$singularLength = \strlen($lowerSingularRev);
2018-04-11 18:33:35 +01:00
// Check if the word is one which is not inflected, return early if so
if (\in_array($lowerSingularRev, self::$uninflected, true)) {
2018-04-11 18:33:35 +01:00
return $singular;
}
// The outer loop iterates over the entries of the singular table
// The inner loop $j iterates over the characters of the singular suffix
// in the singular table to compare them with the characters of the actual
// given singular suffix
foreach (self::$singularMap as $map) {
$suffix = $map[0];
$suffixLength = $map[1];
$j = 0;
// Compare characters in the singular table and of the suffix of the
// given plural one by one
while ($suffix[$j] === $lowerSingularRev[$j]) {
// Let $j point to the next character
++$j;
// Successfully compared the last character
// Add an entry with the plural suffix to the plural array
if ($j === $suffixLength) {
// Is there any character preceding the suffix in the plural string?
if ($j < $singularLength) {
$nextIsVocal = false !== strpos('aeiou', $lowerSingularRev[$j]);
if (!$map[2] && $nextIsVocal) {
// suffix may not succeed a vocal but next char is one
break;
}
if (!$map[3] && !$nextIsVocal) {
// suffix may not succeed a consonant but next char is one
break;
}
}
$newBase = substr($singular, 0, $singularLength - $suffixLength);
$newSuffix = $map[4];
// Check whether the first character in the singular suffix
// is uppercased. If yes, uppercase the first character in
// the singular suffix too
$firstUpper = ctype_upper($singularRev[$j - 1]);
if (\is_array($newSuffix)) {
$plurals = [];
2018-04-11 18:33:35 +01:00
foreach ($newSuffix as $newSuffixEntry) {
$plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
}
return $plurals;
}
return $newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix);
}
// Suffix is longer than word
if ($j === $singularLength) {
break;
}
}
}
// Assume that plural is singular with a trailing `s`
return $singular.'s';
}
}