xref: /plugin/aichat/vendor/vanderlee/php-sentence/tests/SentenceTest.php (revision 8817535b0c67f8b10e9b8c05dcdf58fc17827423)
1*8817535bSAndreas Gohr<?php
2*8817535bSAndreas Gohr
3*8817535bSAndreas Gohrnamespace Vanderlee\Sentence\Tests;
4*8817535bSAndreas Gohr
5*8817535bSAndreas Gohruse PHPUnit_Framework_TestCase;
6*8817535bSAndreas Gohruse Vanderlee\Sentence\Sentence;
7*8817535bSAndreas Gohr
8*8817535bSAndreas Gohr/**
9*8817535bSAndreas Gohr * @coversDefaultClass \Vanderlee\Sentence\Sentence
10*8817535bSAndreas Gohr */
11*8817535bSAndreas Gohrclass SentenceTest extends PHPUnit_Framework_TestCase
12*8817535bSAndreas Gohr{
13*8817535bSAndreas Gohr
14*8817535bSAndreas Gohr    /**
15*8817535bSAndreas Gohr     * @var Sentence
16*8817535bSAndreas Gohr     */
17*8817535bSAndreas Gohr    protected $object;
18*8817535bSAndreas Gohr
19*8817535bSAndreas Gohr    /**
20*8817535bSAndreas Gohr     * Sets up the fixture, for example, opens a network connection.
21*8817535bSAndreas Gohr     * This method is called before a test is executed.
22*8817535bSAndreas Gohr     */
23*8817535bSAndreas Gohr    protected function setUp()
24*8817535bSAndreas Gohr    {
25*8817535bSAndreas Gohr        $this->object = new \Vanderlee\Sentence\Sentence();
26*8817535bSAndreas Gohr    }
27*8817535bSAndreas Gohr
28*8817535bSAndreas Gohr    /**
29*8817535bSAndreas Gohr     * @covers ::count
30*8817535bSAndreas Gohr     */
31*8817535bSAndreas Gohr    public function testCountEmpty()
32*8817535bSAndreas Gohr    {
33*8817535bSAndreas Gohr        $this->assertSame(0, $this->object->count(''));
34*8817535bSAndreas Gohr        $this->assertSame(0, $this->object->count(' '));
35*8817535bSAndreas Gohr        $this->assertSame(0, $this->object->count("\n"));
36*8817535bSAndreas Gohr    }
37*8817535bSAndreas Gohr
38*8817535bSAndreas Gohr    /**
39*8817535bSAndreas Gohr     * @covers ::count
40*8817535bSAndreas Gohr     */
41*8817535bSAndreas Gohr    public function testCountWord()
42*8817535bSAndreas Gohr    {
43*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello'));
44*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello.'));
45*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello...'));
46*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello!'));
47*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello?'));
48*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello?!'));
49*8817535bSAndreas Gohr    }
50*8817535bSAndreas Gohr
51*8817535bSAndreas Gohr    /**
52*8817535bSAndreas Gohr     * @covers ::count
53*8817535bSAndreas Gohr     */
54*8817535bSAndreas Gohr    public function testCountTwoWords()
55*8817535bSAndreas Gohr    {
56*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world'));
57*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world.'));
58*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world...'));
59*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world!'));
60*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world?'));
61*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world?!'));
62*8817535bSAndreas Gohr    }
63*8817535bSAndreas Gohr
64*8817535bSAndreas Gohr    /**
65*8817535bSAndreas Gohr     * @covers ::count
66*8817535bSAndreas Gohr     */
67*8817535bSAndreas Gohr    public function testCountMultipleWords()
68*8817535bSAndreas Gohr    {
69*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count('Hello world. Are you there'));
70*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count('Hello world. Are you there?'));
71*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world, Are you there?'));
72*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world: Are you there?'));
73*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count('Hello world... Are you there?'));
74*8817535bSAndreas Gohr    }
75*8817535bSAndreas Gohr
76*8817535bSAndreas Gohr    /**
77*8817535bSAndreas Gohr     * @covers ::count
78*8817535bSAndreas Gohr     */
79*8817535bSAndreas Gohr    public function testCountLinebreaks()
80*8817535bSAndreas Gohr    {
81*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\rAre you there?"));
82*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\nAre you there?"));
83*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\r\nAre you there?"));
84*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\r\n\rAre you there?"));
85*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\n\r\nAre you there?"));
86*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\n\nAre you there?"));
87*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello world...\r\rAre you there?"));
88*8817535bSAndreas Gohr    }
89*8817535bSAndreas Gohr
90*8817535bSAndreas Gohr    /**
91*8817535bSAndreas Gohr     * @covers ::count
92*8817535bSAndreas Gohr     */
93*8817535bSAndreas Gohr    public function testCountAbreviations()
94*8817535bSAndreas Gohr    {
95*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Hello mr. Smith."));
96*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Hello, OMG Kittens!"));
97*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Hello, abbrev. Kittens!"));
98*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Hello, O.M.G. Kittens!"));
99*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Last week, former director of the A.B.C. John B. Smith was fired."));
100*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Mr. Smith was not available for comment.."));
101*8817535bSAndreas Gohr    }
102*8817535bSAndreas Gohr
103*8817535bSAndreas Gohr    /**
104*8817535bSAndreas Gohr     * @covers ::count
105*8817535bSAndreas Gohr     */
106*8817535bSAndreas Gohr    public function testCountMultiplePunctuation()
107*8817535bSAndreas Gohr    {
108*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello there. Brave new world."));
109*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("Hello there... Brave new world."));
110*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello there?... Brave new world."));
111*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello there!... Brave new world."));
112*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello there!!! Brave new world."));
113*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Hello there??? Brave new world."));
114*8817535bSAndreas Gohr    }
115*8817535bSAndreas Gohr
116*8817535bSAndreas Gohr    /**
117*8817535bSAndreas Gohr     * @covers ::count
118*8817535bSAndreas Gohr     */
119*8817535bSAndreas Gohr    public function testCountOneWordSentences()
120*8817535bSAndreas Gohr    {
121*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("You? Smith?"));
122*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("You there? Smith?"));
123*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("You mr. Smith?"));
124*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Are you there. Mister Smith?"));
125*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Are you there. Smith, sir?"));
126*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("Are you there. Mr. Smith?"));
127*8817535bSAndreas Gohr    }
128*8817535bSAndreas Gohr
129*8817535bSAndreas Gohr    /**
130*8817535bSAndreas Gohr     * @covers ::split
131*8817535bSAndreas Gohr     */
132*8817535bSAndreas Gohr    public function testSplitEmpty()
133*8817535bSAndreas Gohr    {
134*8817535bSAndreas Gohr        $this->assertSame([], $this->object->split(''));
135*8817535bSAndreas Gohr        $this->assertSame([], $this->object->split(' '));
136*8817535bSAndreas Gohr        $this->assertSame([], $this->object->split("\n"));
137*8817535bSAndreas Gohr    }
138*8817535bSAndreas Gohr
139*8817535bSAndreas Gohr    /**
140*8817535bSAndreas Gohr     * @covers ::cleanupUnicode
141*8817535bSAndreas Gohr     */
142*8817535bSAndreas Gohr    public function testCleanupUnicode()
143*8817535bSAndreas Gohr    {
144*8817535bSAndreas Gohr        $this->assertSame(['Fix "these" quotes'], $this->object->split('Fix "these" quotes'));
145*8817535bSAndreas Gohr        $this->assertSame(['Fix "these" quotes'], $this->object->split("Fix \xC2\xABthese\xC2\xAB quotes"));
146*8817535bSAndreas Gohr    }
147*8817535bSAndreas Gohr
148*8817535bSAndreas Gohr    /**
149*8817535bSAndreas Gohr     * @covers ::split
150*8817535bSAndreas Gohr     */
151*8817535bSAndreas Gohr    public function testSplitWord()
152*8817535bSAndreas Gohr    {
153*8817535bSAndreas Gohr        $this->assertSame(['Hello'], $this->object->split('Hello'));
154*8817535bSAndreas Gohr        $this->assertSame(['Hello.'], $this->object->split('Hello.'));
155*8817535bSAndreas Gohr        $this->assertSame(['Hello...'], $this->object->split('Hello...'));
156*8817535bSAndreas Gohr        $this->assertSame(['Hello!'], $this->object->split('Hello!'));
157*8817535bSAndreas Gohr        $this->assertSame(['Hello?'], $this->object->split('Hello?'));
158*8817535bSAndreas Gohr        $this->assertSame(['Hello?!'], $this->object->split('Hello?!'));
159*8817535bSAndreas Gohr    }
160*8817535bSAndreas Gohr
161*8817535bSAndreas Gohr    /**
162*8817535bSAndreas Gohr     * @covers ::split
163*8817535bSAndreas Gohr     */
164*8817535bSAndreas Gohr    public function testSplitMultipleWords()
165*8817535bSAndreas Gohr    {
166*8817535bSAndreas Gohr        $this->assertSame(['Hello world.', ' Are you there'], $this->object->split('Hello world. Are you there'));
167*8817535bSAndreas Gohr        $this->assertSame(['Hello world.', ' Are you there?'], $this->object->split('Hello world. Are you there?'));
168*8817535bSAndreas Gohr        $this->assertSame(['Hello world.', 'Are you there'], $this->object->split('Hello world. Are you there', Sentence::SPLIT_TRIM));
169*8817535bSAndreas Gohr        $this->assertSame(['Hello world.', 'Are you there?'], $this->object->split('Hello world. Are you there?', Sentence::SPLIT_TRIM));
170*8817535bSAndreas Gohr        $this->assertSame(['Hello world, Are you there?'], $this->object->split('Hello world, Are you there?'));
171*8817535bSAndreas Gohr        $this->assertSame(['Hello world: Are you there?'], $this->object->split('Hello world: Are you there?'));
172*8817535bSAndreas Gohr        $this->assertSame(['Hello world... Are you there?'], $this->object->split('Hello world... Are you there?'));
173*8817535bSAndreas Gohr    }
174*8817535bSAndreas Gohr
175*8817535bSAndreas Gohr    /**
176*8817535bSAndreas Gohr     * @covers ::split
177*8817535bSAndreas Gohr     */
178*8817535bSAndreas Gohr    public function testSplitLinebreaks()
179*8817535bSAndreas Gohr    {
180*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\r", "Are you there?"], $this->object->split("Hello world...\rAre you there?"));
181*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\n", " Are you there?"], $this->object->split("Hello world...\n Are you there?"));
182*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\n", "Are you there?"], $this->object->split("Hello world...\nAre you there?"));
183*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\r\n", "Are you there?"], $this->object->split("Hello world...\r\nAre you there?"));
184*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\r\n\r", "Are you there?"], $this->object->split("Hello world...\r\n\rAre you there?"));
185*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\n\r\n", "Are you there?"], $this->object->split("Hello world...\n\r\nAre you there?"));
186*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\n\n", "Are you there?"], $this->object->split("Hello world...\n\nAre you there?"));
187*8817535bSAndreas Gohr        $this->assertSame(["Hello world...\r\r", "Are you there?"], $this->object->split("Hello world...\r\rAre you there?"));
188*8817535bSAndreas Gohr    }
189*8817535bSAndreas Gohr
190*8817535bSAndreas Gohr    /**
191*8817535bSAndreas Gohr     * @covers ::split
192*8817535bSAndreas Gohr     */
193*8817535bSAndreas Gohr    public function testSplitAbreviations()
194*8817535bSAndreas Gohr    {
195*8817535bSAndreas Gohr//		$this->markTestIncomplete('This test has not been implemented yet.');
196*8817535bSAndreas Gohr        $this->assertSame(['Hello mr. Smith.'], $this->object->split("Hello mr. Smith."));
197*8817535bSAndreas Gohr        $this->assertSame(['Hello, OMG Kittens!'], $this->object->split("Hello, OMG Kittens!"));
198*8817535bSAndreas Gohr        $this->assertSame(['Hello, abbrev. Kittens!'], $this->object->split("Hello, abbrev. Kittens!"));
199*8817535bSAndreas Gohr        $this->assertSame(['Hello, O.M.G. Kittens!'], $this->object->split("Hello, O.M.G. Kittens!"));
200*8817535bSAndreas Gohr        $this->assertSame(['Last week, former director of the A.B.C. John B. Smith was fired.'], $this->object->split("Last week, former director of the A.B.C. John B. Smith was fired."));
201*8817535bSAndreas Gohr        $this->assertSame(['Mr. Smith was not available for comment..'], $this->object->split("Mr. Smith was not available for comment.."));
202*8817535bSAndreas Gohr        $this->assertSame(['Hello mr. Smith.', ' Are you there?'], $this->object->split("Hello mr. Smith. Are you there?"));
203*8817535bSAndreas Gohr    }
204*8817535bSAndreas Gohr
205*8817535bSAndreas Gohr    /**
206*8817535bSAndreas Gohr     * @covers ::split
207*8817535bSAndreas Gohr     */
208*8817535bSAndreas Gohr    public function testSplitOneWordSentences()
209*8817535bSAndreas Gohr    {
210*8817535bSAndreas Gohr        $this->assertSame(["You?", " Smith?"], $this->object->split("You? Smith?"));
211*8817535bSAndreas Gohr        $this->assertSame(["You there?", " Smith?"], $this->object->split("You there? Smith?"));
212*8817535bSAndreas Gohr        $this->assertSame(["You mr. Smith?"], $this->object->split("You mr. Smith?"));
213*8817535bSAndreas Gohr        $this->assertSame(["Are you there.", " Mister Smith?"], $this->object->split("Are you there. Mister Smith?"));
214*8817535bSAndreas Gohr        $this->assertSame(["Are you there.", " Smith, sir?"], $this->object->split("Are you there. Smith, sir?"));
215*8817535bSAndreas Gohr        $this->assertSame(["Are you there.", " Mr. Smith?"], $this->object->split("Are you there. Mr. Smith?"));
216*8817535bSAndreas Gohr    }
217*8817535bSAndreas Gohr
218*8817535bSAndreas Gohr    /**
219*8817535bSAndreas Gohr     * @covers ::split
220*8817535bSAndreas Gohr     */
221*8817535bSAndreas Gohr    public function testSplitParenthesis()
222*8817535bSAndreas Gohr    {
223*8817535bSAndreas Gohr        $this->assertSame(["You there (not here!).", " Mister Smith"], $this->object->split("You there (not here!). Mister Smith"));
224*8817535bSAndreas Gohr        $this->assertSame(["You (not him!) here.", " Mister Smith"], $this->object->split("You (not him!) here. Mister Smith"));
225*8817535bSAndreas Gohr        $this->assertSame(["(What!) you here.", " Mister Smith"], $this->object->split("(What!) you here. Mister Smith"));
226*8817535bSAndreas Gohr        $this->assertSame(["You there (not here).", " Mister Smith"], $this->object->split("You there (not here). Mister Smith"));
227*8817535bSAndreas Gohr        $this->assertSame(["You (not him) here.", " Mister Smith"], $this->object->split("You (not him) here. Mister Smith"));
228*8817535bSAndreas Gohr        $this->assertSame(["(What) you here.", " Mister Smith"], $this->object->split("(What) you here. Mister Smith"));
229*8817535bSAndreas Gohr    }
230*8817535bSAndreas Gohr
231*8817535bSAndreas Gohr    /**
232*8817535bSAndreas Gohr     * @covers ::split
233*8817535bSAndreas Gohr     */
234*8817535bSAndreas Gohr    public function testSentenceWithNumericValues()
235*8817535bSAndreas Gohr    {
236*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("The price is £25.50, including postage and packing."));
237*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("The price is 25.50, including postage and packing."));
238*8817535bSAndreas Gohr        $this->assertSame(1, $this->object->count("I went true to size at 10.5 cms."));
239*8817535bSAndreas Gohr        $this->assertSame(2, $this->object->count("The prices are £25.50 or £27.50, including postage and packing. I went true to size at 10.5 cms."));
240*8817535bSAndreas Gohr    }
241*8817535bSAndreas Gohr
242*8817535bSAndreas Gohr    /**
243*8817535bSAndreas Gohr     * @covers ::floatNumberClean
244*8817535bSAndreas Gohr     * @covers ::floatNumberRevert
245*8817535bSAndreas Gohr     *
246*8817535bSAndreas Gohr     * @dataProvider dataSplit
247*8817535bSAndreas Gohr     *
248*8817535bSAndreas Gohr     * @param string[] $expected
249*8817535bSAndreas Gohr     * @param string   $text
250*8817535bSAndreas Gohr     *
251*8817535bSAndreas Gohr     * @return void
252*8817535bSAndreas Gohr     */
253*8817535bSAndreas Gohr    public function testSplit(array $expected, string $text)
254*8817535bSAndreas Gohr    {
255*8817535bSAndreas Gohr        $this->assertSame($expected, $this->object->split($text));
256*8817535bSAndreas Gohr        $this->assertSame(count($expected), $this->object->count($text));
257*8817535bSAndreas Gohr    }
258*8817535bSAndreas Gohr
259*8817535bSAndreas Gohr    public function dataSplit()
260*8817535bSAndreas Gohr    {
261*8817535bSAndreas Gohr        return [
262*8817535bSAndreas Gohr            'repeat 2'                            => [
263*8817535bSAndreas Gohr                [
264*8817535bSAndreas Gohr                    'He got £2.',
265*8817535bSAndreas Gohr                    ' He lost £2.',
266*8817535bSAndreas Gohr                    ' He had £2.',
267*8817535bSAndreas Gohr                ],
268*8817535bSAndreas Gohr                'He got £2. He lost £2. He had £2.',
269*8817535bSAndreas Gohr            ],
270*8817535bSAndreas Gohr            'times'                               => [
271*8817535bSAndreas Gohr                [
272*8817535bSAndreas Gohr                    'If at 8:00 pm, do something, there is a good chance that by 8:45 pm we do something else.',
273*8817535bSAndreas Gohr                    ' This is another sentence',
274*8817535bSAndreas Gohr                ],
275*8817535bSAndreas Gohr                'If at 8:00 pm, do something, there is a good chance that by 8:45 pm we do something else. This is another sentence',
276*8817535bSAndreas Gohr            ],
277*8817535bSAndreas Gohr            'lead/trailing zeroes'                => [
278*8817535bSAndreas Gohr                [
279*8817535bSAndreas Gohr                    'Number 00.20 it is',
280*8817535bSAndreas Gohr                ],
281*8817535bSAndreas Gohr                'Number 00.20 it is',
282*8817535bSAndreas Gohr            ],
283*8817535bSAndreas Gohr            'Bug report #15; ))) -1 index offset' => [
284*8817535bSAndreas Gohr                [
285*8817535bSAndreas Gohr                    ')))',
286*8817535bSAndreas Gohr                ],
287*8817535bSAndreas Gohr                ')))',
288*8817535bSAndreas Gohr            ],
289*8817535bSAndreas Gohr            'Price'                               => [
290*8817535bSAndreas Gohr                [
291*8817535bSAndreas Gohr                    'The price is 25.50, including postage and packing.',
292*8817535bSAndreas Gohr                ],
293*8817535bSAndreas Gohr                'The price is 25.50, including postage and packing.',
294*8817535bSAndreas Gohr            ],
295*8817535bSAndreas Gohr            'Recursive replacement'               => [
296*8817535bSAndreas Gohr                [
297*8817535bSAndreas Gohr                    'From 11 to 12.',
298*8817535bSAndreas Gohr                    ' From 11 to 15.',
299*8817535bSAndreas Gohr                ],
300*8817535bSAndreas Gohr                'From 11 to 12. From 11 to 15.',
301*8817535bSAndreas Gohr            ],
302*8817535bSAndreas Gohr        ];
303*8817535bSAndreas Gohr    }
304*8817535bSAndreas Gohr}
305