Skip to content

Commit 2b9dcd3

Browse files
committed
Ability to pluralise and singularise words
1 parent 13bf74d commit 2b9dcd3

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"php": "^7.0",
77
"rapidwebltd/rw-file-cache": "^1.2",
88
"divineomega/is_offensive": "^1.0",
9-
"davechild/textstatistics": "^1.0"
9+
"davechild/textstatistics": "^1.0",
10+
"doctrine/inflector": "^1.2"
1011
},
1112
"require-dev": {
1213
"phpunit/phpunit": "^5.7",

src/Pluralizer.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace DivineOmega\WordInfo;
3+
4+
use Doctrine\Common\Inflector\Inflector;
5+
6+
class Pluralizer
7+
{
8+
private $irregular = [
9+
'goose' => 'geese',
10+
];
11+
12+
private $uncountable = [
13+
'audio',
14+
'education',
15+
'love',
16+
'pokemon',
17+
'mathematics'
18+
];
19+
20+
private $word;
21+
22+
public function __construct(string $word)
23+
{
24+
$this->word = $word;
25+
}
26+
27+
public function pluralize()
28+
{
29+
if ($this->isUncountable()) {
30+
return $this->word;
31+
}
32+
33+
foreach($this->irregular as $singular => $plural) {
34+
if ($singular==$this->word) {
35+
return $plural;
36+
}
37+
}
38+
39+
return Inflector::pluralize($this->word);
40+
}
41+
42+
public function singularize()
43+
{
44+
if ($this->isUncountable()) {
45+
return $this->word;
46+
}
47+
48+
foreach($this->irregular as $singular => $plural) {
49+
if ($plural==$this->word) {
50+
return $singular;
51+
}
52+
}
53+
54+
return Inflector::singularize($this->word);
55+
}
56+
57+
private function isUncountable()
58+
{
59+
return (in_array($this->word, $this->uncountable));
60+
}
61+
}

src/Word.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function syllables()
7575
return Syllables::syllableCount($this->word);
7676
}
7777

78+
public function plural()
79+
{
80+
return (new Pluralizer($this->word))->pluralize();
81+
}
82+
83+
public function singular()
84+
{
85+
return (new Pluralizer($this->word))->singularize();
86+
}
87+
7888
public function offensive()
7989
{
8090
return is_offensive($this->word);

tests/Unit/WordTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,49 @@ public function testPortmanteaus2()
123123
$this->assertEquals($expected, $portmanteaus);
124124
}
125125

126+
private function getSingularToPluralData()
127+
{
128+
return [
129+
'cat' => 'cats',
130+
'mitten' => 'mittens',
131+
'sausage' => 'sausages',
132+
'child' => 'children',
133+
'goose' => 'geese',
134+
'person' => 'people',
135+
'woman' => 'women',
136+
'man' => 'men',
137+
'audio' => 'audio',
138+
'education' => 'education',
139+
'rice' => 'rice',
140+
'love' => 'love',
141+
'pokemon' => 'pokemon',
142+
'sheep' => 'sheep',
143+
'sex' => 'sexes',
144+
'mouse' => 'mice',
145+
'mathematics' => 'mathematics',
146+
'information' => 'information',
147+
'tooth' => 'teeth'
148+
];
149+
}
150+
151+
public function testPluralise()
152+
{
153+
$data = $this->getSingularToPluralData();
154+
155+
foreach($data as $singular => $plural) {
156+
$word = new Word($singular);
157+
$this->assertEquals($plural, $word->plural());
158+
}
159+
}
160+
161+
public function testSingularise()
162+
{
163+
$data = $this->getSingularToPluralData();
164+
165+
foreach($data as $singular => $plural) {
166+
$word = new Word($plural);
167+
$this->assertEquals($singular, $word->singular());
168+
}
169+
}
170+
126171
}

0 commit comments

Comments
 (0)