Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-lingual inflectors and pluralize/singularize methods #170

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions src/Inflections/En.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Stringy\Inflections;

/**
* English inflection rules.
*/
class En extends Inflector
{
/**
* Return an array of pluralization rules, from most to least specific, in the form $rule => $replacement
*
* @return array
*/
public function pluralRules()
{
return [
'/(quiz)$/i' => '\1zes',
'/^(oxen)$/i' => '\1',
'/^(ox)$/i' => '\1en',
'/^(m|l)ice$/i' => '\1ice',
'/^(m|l)ouse$/i' => '\1ice',
'/(matr|vert|ind)(?:ix|ex)$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(hive)$/i' => '\1s',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/sis$/i' => 'ses',
'/([ti])a$/i' => '\1a',
'/([ti])um$/i' => '\1a',
'/(buffal|tomat|potat|volcan|her)o$/i' => '\1oes',
'/(bu)s$/i' => '\1ses',
'/(alias|status)$/i' => '\1es',
'/^(ax|test)is$/i' => '\1es',
'/s$/i' => 's',
'/$/' => 's',
];
}

/**
* Return an array of singularization rules, from most to least specific, in the form $rule => $replacement
*
*
* @return array
*/
public function singularRules()
{
return [
'/(database)s$/i' => '\1',
'/(quiz)zes$/i' => '\1',
'/(matr)ices$/i' => '\1ix',
'/(vert|ind)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(alias|status)(es)?$/i' => '\1',
'/^(a)x[ie]s$/i' => '\1xis',
'/(cris|test)(is|es)$/i' => '\1is',
'/(shoe)s$/i' => '\1',
'/(o)es$/i' => '\1',
'/(bus)(es)?$/i' => '\1',
'/^(m|l)ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1ovie',
'/(s)eries$/i' => '\1eries',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/([^f])ves$/i' => '\1fe',
'/(^analy)(sis|ses)$/i' => '\1sis',
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i' => '\1sis',
'/([ti])a$/i' => '\1um',
'/(n)ews$/i' => '\1ews',
'/(ss)$/i' => '\1',
'/s$/i' => '',
];
}

/**
* Return an array of irregular replacements, in the form singular => plural ('goose' => 'geese')
*
* @return array
*/
public function irregularRules()
{
return [
'leaf' => 'leaves',
'loaf' => 'loaves',
'octopus' => 'octopuses',
'virus' => 'viruses',
'person' => 'people',
'man' => 'men',
'child' => 'children',
'sex' => 'sexes',
'move' => 'moves',
'zombie' => 'zombies',
'goose' => 'geese',
'genus' => 'genera',
];
}

/**
* Return an array of uncountable rules (sheep, police)
*
* @return array
*/
public function uncountableRules()
{
return [
'advice',
'aircraft',
'art',
'baggage',
'butter',
'clothing',
'coal',
'cotton',
'deer',
'equipment',
'experience',
'feedback',
'fish',
'flour',
'food',
'furniture',
'gas',
'homework',
'impatience',
'information',
'jeans',
'knowledge',
'leather',
'love',
'luggage',
'management',
'money',
'moose',
'music',
'news',
'oil',
'patience',
'police',
'polish',
'progress',
'research',
'rice',
'salmon',
'sand',
'series',
'sheep',
'silk',
'sms',
'soap',
'spam',
'species',
'staff',
'sugar',
'swine',
'talent',
'toothpaste',
'traffic',
'travel',
'vinegar',
'weather',
'wood',
'wool',
'work',
];
}
}
76 changes: 76 additions & 0 deletions src/Inflections/Es.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Stringy\Inflections;

/**
* Spanish inflection rules.
*/
class Es extends Inflector
{
/**
* Return an array of pluralization rules, from most to least specific, in the form $rule => $replacement
*
* @return array
*/
public function pluralRules()
{
return [
'/ú([sn])$/i' => 'u\1es',
'/ó([sn])$/i' => 'o\1es',
'/í([sn])$/i' => 'i\1es',
'/é([sn])$/i' => 'e\1es',
'/á([sn])$/i' => 'a\1es',
'/z$/i' => 'ces',
'/([aeiou]s)$/i' => '\1',
'/([^aeéiou])$/i' => '\1es',
'/$/' => 's',
];
}

/**
* Return an array of singularization rules, from most to least specific, in the form $rule => $replacement
*
*
* @return array
*/
public function singularRules()
{
return [
'/ereses$/' => 'erés',
'/iones$/' => 'ión',
'/ces$/' => 'z',
'/es$/' => '',
'/s$/' => '',
];
}

/**
* Return an array of irregular replacements, in the form singular => plural ('goose' => 'geese')
*
* @return array
*/
public function irregularRules()
{
return [
'el' => 'los',
'lunes' => 'lunes',
'rompecabezas' => 'rompecabezas',
'crisis' => 'crisis',
'papá' => 'papás',
'mamá' => 'mamás',
'sofá' => 'sofás',
// because 'mes' is considered already a plural
'mes' => 'meses',
];
}

/**
* Return an array of uncountable rules (sheep, police)
*
* @return array
*/
public function uncountableRules()
{
return [];
}
}
68 changes: 68 additions & 0 deletions src/Inflections/Fr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Stringy\Inflections;

/**
* French inflection rules.
*/
class Fr extends Inflector
{
/**
* Return an array of pluralization rules, from most to least specific, in the form $rule => $replacement
*
* @return array
*/
public function pluralRules()
{
return [
'/(s|x|z)$/' => '\1',
'/(b|cor|ém|gemm|soupir|trav|vant|vitr)ail$/' => '\1aux',
'/ail$/' => 'ails',
'/al$/' => 'aux',
'/(bleu|émeu|landau|lieu|pneu|sarrau)$/' => '\1s',
'/(bijou|caillou|chou|genou|hibou|joujou|pou|au|eu|eau)$/' => '\1x',
'/$/' => 's',
];
}

/**
* Return an array of singularization rules, from most to least specific, in the form $rule => $replacement
*
*
* @return array
*/
public function singularRules()
{
return [
'/(b|cor|ém|gemm|soupir|trav|vant|vitr)aux$/' => '\1ail',
'/ails$/' => 'ail',
'/(journ|chev)aux$/' => '\1al',
'/(bijou|caillou|chou|genou|hibou|joujou|pou|au|eu|eau)x$/' => '\1',
'/s$/' => '',
];
}

/**
* Return an array of irregular replacements, in the form singular => plural ('goose' => 'geese')
*
* @return array
*/
public function irregularRules()
{
return [
'monsieur' => 'messieurs',
'madame' => 'mesdames',
'mademoiselle' => 'mesdemoiselles',
];
}

/**
* Return an array of uncountable rules (sheep, police)
*
* @return array
*/
public function uncountableRules()
{
return [];
}
}
Loading