Skip to content
Permalink
Browse files

Optimize Inflector.

Use a single list of uninflected rules.
  • Loading branch information...
ADmad committed Aug 31, 2014
1 parent f8b941b commit aedc92df34979d0d948df8a8363aa2b7d35133c0
Showing with 44 additions and 66 deletions.
  1. +33 −54 src/Utility/Inflector.php
  2. +11 −12 tests/TestCase/Utility/InflectorTest.php
@@ -54,19 +54,6 @@ class Inflector {
'/^$/' => '',
'/$/' => 's',
),
'uninflected' => array(
'.*[nrlm]ese',
'.*data',
'.*deer',
'.*fish',
'.*measles',
'.*ois',
'.*pox',
'.*sheep',
'people',
'feedback',
'stadia'
),
'irregular' => array(
'atlas' => 'atlases',
'beef' => 'beefs',
@@ -151,10 +138,6 @@ class Inflector {
'/^(.*us)$/' => '\\1',
'/s$/i' => ''
),
'uninflected' => array(
'.*data',
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss', 'feedback'
),
'irregular' => array(
'foes' => 'foe',
)
@@ -166,18 +149,11 @@ class Inflector {
* @var array
*/
protected static $_uninflected = array(
'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
'proceedings', 'rabies', 'research', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
'Yengeese'
'.*[nrlm]ese', '.*data', '.*deer', '.*fish', '.*measles', '.*ois',
'.*pox', '.*sheep', '.*ss', 'people', 'feedback', 'stadia', '.*?media',
'chassis', 'clippers', 'debris', 'diabetes', 'equipment', 'gallows',
'graffiti', 'headquarters', 'information', 'innings', 'news', 'nexus',
'proceedings', 'research', 'sea[- ]bass', 'series', 'species', 'weather'
);
/**
@@ -484,13 +460,14 @@ public static function reset() {
* Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
* Inflector::rules('plural', array(
* 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
* 'uninflected' => array('dontinflectme'),
* 'irregular' => array('red' => 'redlings')
* ));
* Inflector::rules('uninflected', array('dontinflectme'));
* Inflector::rules('transliteration', array('/å/' => 'aa'));
* }}}
*
* @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
* @param string $type The type of inflection, either 'plural', 'singular',
* 'uninflected' or 'transliteration'.
* @param array $rules Array of rules to be added.
* @param bool $reset If true, will unset default inflections for all
* new rules that are being defined in $rules.
@@ -508,6 +485,17 @@ public static function rules($type, $rules, $reset = false) {
}
break;
case 'uninflected':
if ($reset) {
static::$_uninflected = $rules;
} else {
static::$_uninflected = array_merge(
$rules,
static::$_transliteration
);
}
break;
default:
foreach ($rules as $rule => $pattern) {
if (!is_array($pattern)) {
@@ -516,11 +504,7 @@ public static function rules($type, $rules, $reset = false) {
if ($reset) {
static::${$var}[$rule] = $pattern;
} else {
if ($rule === 'uninflected') {
static::${$var}[$rule] = array_merge($pattern, static::${$var}[$rule]);
} else {
static::${$var}[$rule] = $pattern + static::${$var}[$rule];
}
static::${$var}[$rule] = $pattern + static::${$var}[$rule];
}
unset($rules[$rule], static::${$var}['cache' . ucfirst($rule)]);
if (isset(static::${$var}['merged'][$rule])) {
@@ -552,12 +536,7 @@ public static function pluralize($word) {
static::$_plural['merged']['irregular'] = static::$_plural['irregular'];
}
if (!isset(static::$_plural['merged']['uninflected'])) {
static::$_plural['merged']['uninflected'] = array_merge(static::$_plural['uninflected'], static::$_uninflected);
}
if (!isset(static::$_plural['cacheUninflected']) || !isset(static::$_plural['cacheIrregular'])) {
static::$_plural['cacheUninflected'] = '(?:' . implode('|', static::$_plural['merged']['uninflected']) . ')';
if (!isset(static::$_plural['cacheIrregular'])) {
static::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_plural['merged']['irregular'])) . ')';
}
@@ -566,7 +545,11 @@ public static function pluralize($word) {
return static::$_cache['pluralize'][$word];
}
if (preg_match('/^(' . static::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
if (!isset(static::$_cache['uninflected'])) {
static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
}
if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
static::$_cache['pluralize'][$word] = $word;
return $word;
}
@@ -591,22 +574,14 @@ public static function singularize($word) {
return static::$_cache['singularize'][$word];
}
if (!isset(static::$_singular['merged']['uninflected'])) {
static::$_singular['merged']['uninflected'] = array_merge(
static::$_singular['uninflected'],
static::$_uninflected
);
}
if (!isset(static::$_singular['merged']['irregular'])) {
static::$_singular['merged']['irregular'] = array_merge(
static::$_singular['irregular'],
array_flip(static::$_plural['irregular'])
);
}
if (!isset(static::$_singular['cacheUninflected']) || !isset(static::$_singular['cacheIrregular'])) {
static::$_singular['cacheUninflected'] = '(?:' . implode('|', static::$_singular['merged']['uninflected']) . ')';
if (!isset(static::$_singular['cacheIrregular'])) {
static::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_singular['merged']['irregular'])) . ')';
}
@@ -615,8 +590,12 @@ public static function singularize($word) {
return static::$_cache['singularize'][$word];
}
if (preg_match('/^(' . static::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
static::$_cache['singularize'][$word] = $word;
if (!isset(static::$_cache['uninflected'])) {
static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
}
if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
static::$_cache['pluralize'][$word] = $word;
return $word;
}
@@ -161,7 +161,7 @@ public function testInflectingSingulars() {
$this->assertEquals(Inflector::singularize('thieves'), 'thief');
$this->assertEquals(Inflector::singularize('potatoes'), 'potato');
$this->assertEquals(Inflector::singularize('heroes'), 'hero');
$this->assertEquals(Inflector::singularize('buffalos'), 'buffalo');
$this->assertEquals(Inflector::singularize('buffaloes'), 'buffalo');
$this->assertEquals(Inflector::singularize('babies'), 'baby');
$this->assertEquals(Inflector::singularize('teeth'), 'tooth');
$this->assertEquals(Inflector::singularize('geese'), 'goose');
@@ -229,7 +229,7 @@ public function testInflectingPlurals() {
$this->assertEquals(Inflector::pluralize('thief'), 'thieves');
$this->assertEquals(Inflector::pluralize('potato'), 'potatoes');
$this->assertEquals(Inflector::pluralize('hero'), 'heroes');
$this->assertEquals(Inflector::pluralize('buffalo'), 'buffalo');
$this->assertEquals(Inflector::pluralize('buffalo'), 'buffaloes');
$this->assertEquals(Inflector::pluralize('tooth'), 'teeth');
$this->assertEquals(Inflector::pluralize('goose'), 'geese');
$this->assertEquals(Inflector::pluralize('foot'), 'feet');
@@ -429,16 +429,17 @@ public function testHumanization() {
*/
public function testCustomPluralRule() {
Inflector::rules('plural', array('/^(custom)$/i' => '\1izables'));
$this->assertEquals(Inflector::pluralize('custom'), 'customizables');
Inflector::rules('uninflected', array('uninflectable'));
Inflector::rules('plural', array('uninflected' => array('uninflectable')));
$this->assertEquals(Inflector::pluralize('custom'), 'customizables');
$this->assertEquals(Inflector::pluralize('uninflectable'), 'uninflectable');
Inflector::reset();
Inflector::rules('plural', array(
'rules' => array('/^(alert)$/i' => '\1ables'),
'uninflected' => array('noflect', 'abtuse'),
'irregular' => array('amaze' => 'amazable', 'phone' => 'phonezes')
));
Inflector::rules('uninflected', array('noflect', 'abtuse'));
$this->assertEquals(Inflector::pluralize('noflect'), 'noflect');
$this->assertEquals(Inflector::pluralize('abtuse'), 'abtuse');
$this->assertEquals(Inflector::pluralize('alert'), 'alertables');
@@ -452,14 +453,14 @@ public function testCustomPluralRule() {
* @return void
*/
public function testCustomSingularRule() {
Inflector::rules('uninflected', array('singulars'));
Inflector::rules('singular', array('/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1'));
$this->assertEquals(Inflector::singularize('epler'), 'eple');
$this->assertEquals(Inflector::singularize('jenter'), 'jente');
Inflector::rules('singular', array(
'rules' => array('/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta'),
'uninflected' => array('singulars'),
'irregular' => array('spins' => 'spinor')
));
@@ -517,18 +518,16 @@ public function testCustomRuleWithReset() {
$pluralIrregular = array('as' => 'ases');
Inflector::rules('singular', array(
'rules' => array('/^(.*)(a|e|o|u)is$/i' => '\1\2l'),
'uninflected' => $uninflected,
'rules' => array('/^(.*)(a|e|o|u)is$/i' => '\1\2l')
), true);
Inflector::rules('plural', array(
'rules' => array(
'/^(.*)(a|e|o|u)l$/i' => '\1\2is',
),
'uninflected' => $uninflected,
'rules' => array('/^(.*)(a|e|o|u)l$/i' => '\1\2is'),
'irregular' => $pluralIrregular
), true);
Inflector::rules('uninflected', $uninflected, true);
$this->assertEquals(Inflector::pluralize('Alcool'), 'Alcoois');
$this->assertEquals(Inflector::pluralize('Atlas'), 'Atlas');
$this->assertEquals(Inflector::singularize('Alcoois'), 'Alcool');

0 comments on commit aedc92d

Please sign in to comment.
You can’t perform that action at this time.