Skip to content

Commit

Permalink
Ability to pluralise and singularise words
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega committed Feb 12, 2018
1 parent 13bf74d commit 2b9dcd3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"php": "^7.0",
"rapidwebltd/rw-file-cache": "^1.2",
"divineomega/is_offensive": "^1.0",
"davechild/textstatistics": "^1.0"
"davechild/textstatistics": "^1.0",
"doctrine/inflector": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down
61 changes: 61 additions & 0 deletions src/Pluralizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace DivineOmega\WordInfo;

use Doctrine\Common\Inflector\Inflector;

class Pluralizer
{
private $irregular = [
'goose' => 'geese',
];

private $uncountable = [
'audio',
'education',
'love',
'pokemon',
'mathematics'
];

private $word;

public function __construct(string $word)
{
$this->word = $word;
}

public function pluralize()
{
if ($this->isUncountable()) {
return $this->word;
}

foreach($this->irregular as $singular => $plural) {
if ($singular==$this->word) {
return $plural;
}
}

return Inflector::pluralize($this->word);
}

public function singularize()
{
if ($this->isUncountable()) {
return $this->word;
}

foreach($this->irregular as $singular => $plural) {
if ($plural==$this->word) {
return $singular;
}
}

return Inflector::singularize($this->word);
}

private function isUncountable()
{
return (in_array($this->word, $this->uncountable));
}
}
10 changes: 10 additions & 0 deletions src/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function syllables()
return Syllables::syllableCount($this->word);
}

public function plural()
{
return (new Pluralizer($this->word))->pluralize();
}

public function singular()
{
return (new Pluralizer($this->word))->singularize();
}

public function offensive()
{
return is_offensive($this->word);
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/WordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,49 @@ public function testPortmanteaus2()
$this->assertEquals($expected, $portmanteaus);
}

private function getSingularToPluralData()
{
return [
'cat' => 'cats',
'mitten' => 'mittens',
'sausage' => 'sausages',
'child' => 'children',
'goose' => 'geese',
'person' => 'people',
'woman' => 'women',
'man' => 'men',
'audio' => 'audio',
'education' => 'education',
'rice' => 'rice',
'love' => 'love',
'pokemon' => 'pokemon',
'sheep' => 'sheep',
'sex' => 'sexes',
'mouse' => 'mice',
'mathematics' => 'mathematics',
'information' => 'information',
'tooth' => 'teeth'
];
}

public function testPluralise()
{
$data = $this->getSingularToPluralData();

foreach($data as $singular => $plural) {
$word = new Word($singular);
$this->assertEquals($plural, $word->plural());
}
}

public function testSingularise()
{
$data = $this->getSingularToPluralData();

foreach($data as $singular => $plural) {
$word = new Word($plural);
$this->assertEquals($singular, $word->singular());
}
}

}

0 comments on commit 2b9dcd3

Please sign in to comment.