Completed
Push — master ( 747f1c...419887 )
by Martijn
15s queued 12s
created
src/Sentence.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
         $merges = [];
122 122
         $merge = '';
123 123
 
124
-        $filtered = array_filter($punctuations, function ($p) {
124
+        $filtered = array_filter($punctuations, function($p) {
125 125
             return $p !== '';
126 126
         });
127 127
 
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
      */
367 367
     private static function trimSentences($sentences)
368 368
     {
369
-        return array_map(function ($sentence) {
369
+        return array_map(function($sentence) {
370 370
             return Multibyte::trim($sentence);
371 371
         }, $sentences);
372 372
     }
Please login to merge, or discard this patch.
Indentation   +409 added lines, -409 removed lines patch added patch discarded remove patch
@@ -17,415 +17,415 @@
 block discarded – undo
17 17
 class Sentence
18 18
 {
19 19
 
20
-    /**
21
-     * Specify this flag with the split method to trim whitespace.
22
-     */
23
-    const SPLIT_TRIM = 0x1;
24
-
25
-    /**
26
-     * List of characters used to terminate sentences.
27
-     *
28
-     * @var string[]
29
-     */
30
-    private $terminals = ['.', '!', '?'];
31
-
32
-    /**
33
-     * List of characters used for abbreviations.
34
-     *
35
-     * @var string[]
36
-     */
37
-    private $abbreviators = ['.'];
38
-
39
-    /**
40
-     * List of float numbers in the text
41
-     *
42
-     * @var string[]
43
-     */
44
-    private $floatNumbers = [];
45
-
46
-    /**
47
-     * Clean floating point numbers by replace them with their md5 hash
48
-     *
49
-     * @param string $text
50
-     *
51
-     * @return string
52
-     */
53
-    private function floatNumberClean(string $text)
54
-    {
55
-        preg_match_all('!\d+(?:\.\d+)?!', $text, $matches);
56
-
57
-        foreach ($matches[0] as $floatNumber) {
58
-            $this->floatNumbers[$floatNumber] = md5($floatNumber);
59
-
60
-            $text = str_replace($floatNumber, md5($floatNumber), $text);
61
-        }
62
-
63
-        return $text;
64
-    }
65
-
66
-    /**
67
-     * Revert the hashed floating number back
68
-     *
69
-     * @param string[] $text
70
-     *
71
-     * @return string[]
72
-     */
73
-    private function floatNumberRevert($text)
74
-    {
20
+	/**
21
+	 * Specify this flag with the split method to trim whitespace.
22
+	 */
23
+	const SPLIT_TRIM = 0x1;
24
+
25
+	/**
26
+	 * List of characters used to terminate sentences.
27
+	 *
28
+	 * @var string[]
29
+	 */
30
+	private $terminals = ['.', '!', '?'];
31
+
32
+	/**
33
+	 * List of characters used for abbreviations.
34
+	 *
35
+	 * @var string[]
36
+	 */
37
+	private $abbreviators = ['.'];
38
+
39
+	/**
40
+	 * List of float numbers in the text
41
+	 *
42
+	 * @var string[]
43
+	 */
44
+	private $floatNumbers = [];
45
+
46
+	/**
47
+	 * Clean floating point numbers by replace them with their md5 hash
48
+	 *
49
+	 * @param string $text
50
+	 *
51
+	 * @return string
52
+	 */
53
+	private function floatNumberClean(string $text)
54
+	{
55
+		preg_match_all('!\d+(?:\.\d+)?!', $text, $matches);
56
+
57
+		foreach ($matches[0] as $floatNumber) {
58
+			$this->floatNumbers[$floatNumber] = md5($floatNumber);
59
+
60
+			$text = str_replace($floatNumber, md5($floatNumber), $text);
61
+		}
62
+
63
+		return $text;
64
+	}
65
+
66
+	/**
67
+	 * Revert the hashed floating number back
68
+	 *
69
+	 * @param string[] $text
70
+	 *
71
+	 * @return string[]
72
+	 */
73
+	private function floatNumberRevert($text)
74
+	{
75 75
         
76
-        return array_map(function($value) {
77
-            foreach ($this->floatNumbers as $number => $hash) {
78
-                $value = str_replace($hash, $number, $value);
79
-            }
80
-            return $value;
81
-        }, $text);
82
-    }
83
-
84
-    /**
85
-     * Breaks a piece of text into lines by linebreak.
86
-     * Eats up any linebreak characters as if one.
87
-     *
88
-     * Multibyte.php safe
89
-     *
90
-     * @param string $text
91
-     * @return string[]
92
-     */
93
-    private static function linebreakSplit($text)
94
-    {
95
-        $lines = [];
96
-        $line = '';
97
-
98
-        foreach (Multibyte::split('([\r\n]+)', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) {
99
-            $line .= $part;
100
-            if (Multibyte::trim($part) === '') {
101
-                $lines[] = $line;
102
-                $line = '';
103
-            }
104
-        }
105
-        $lines[] = $line;
106
-
107
-        return $lines;
108
-    }
109
-
110
-    /**
111
-     * Splits an array of lines by (consecutive sequences of)
112
-     * terminals, keeping terminals.
113
-     *
114
-     * Multibyte.php safe (atleast for UTF-8)
115
-     *
116
-     * For example:
117
-     *    "There ... is. More!"
118
-     *        ... becomes ...
119
-     *    [ "There ", "...", " is", ".", " More", "!" ]
120
-     *
121
-     * @param string $line
122
-     * @return string[]
123
-     */
124
-    private function punctuationSplit($line)
125
-    {
126
-        $parts = [];
127
-
128
-        $chars = preg_split('//u', $line, -1, PREG_SPLIT_NO_EMPTY); // This is UTF8 multibyte safe!
129
-        $is_terminal = in_array($chars[0], $this->terminals);
130
-
131
-        $part = '';
132
-        foreach ($chars as $index => $char) {
133
-            if (in_array($char, $this->terminals) !== $is_terminal) {
134
-                $parts[] = $part;
135
-                $part = '';
136
-                $is_terminal = !$is_terminal;
137
-            }
138
-            $part .= $char;
139
-        }
140
-
141
-        if (!empty($part)) {
142
-            $parts[] = $part;
143
-        }
144
-
145
-        return $parts;
146
-    }
147
-
148
-    /**
149
-     * Appends each terminal item after it's preceding
150
-     * non-terminals.
151
-     *
152
-     * Multibyte.php safe (atleast for UTF-8)
153
-     *
154
-     * For example:
155
-     *    [ "There ", "...", " is", ".", " More", "!" ]
156
-     *        ... becomes ...
157
-     *    [ "There ... is.", "More!" ]
158
-     *
159
-     * @param string[] $punctuations
160
-     * @return string[]
161
-     */
162
-    private function punctuationMerge($punctuations)
163
-    {
164
-        $definite_terminals = array_diff($this->terminals, $this->abbreviators);
165
-
166
-        $merges = [];
167
-        $merge = '';
168
-
169
-        $filtered = array_filter($punctuations, function ($p) {
170
-            return $p !== '';
171
-        });
172
-
173
-        foreach ($filtered as $punctuation) {
174
-            $merge .= $punctuation;
175
-            if (mb_strlen($punctuation) === 1
176
-                && in_array($punctuation, $this->terminals)) {
177
-                $merges[] = $merge;
178
-                $merge = '';
179
-            } else {
180
-                foreach ($definite_terminals as $terminal) {
181
-                    if (mb_strpos($punctuation, $terminal) !== false) {
182
-                        $merges[] = $merge;
183
-                        $merge = '';
184
-                        break;
185
-                    }
186
-                }
187
-            }
188
-        }
189
-        if (!empty($merge)) {
190
-            $merges[] = $merge;
191
-        }
192
-
193
-        return $merges;
194
-    }
195
-
196
-    /**
197
-     * Looks for capitalized abbreviations & includes them with the following fragment.
198
-     *
199
-     * For example:
200
-     *    [ "Last week, former director of the F.B.I. James B. Comey was fired. Mr. Comey was not available for comment." ]
201
-     *        ... becomes ...
202
-     *    [ "Last week, former director of the F.B.I. James B. Comey was fired." ]
203
-     *  [ "Mr. Comey was not available for comment." ]
204
-     *
205
-     * @param string[] $fragments
206
-     * @return string[]
207
-     */
208
-    private function abbreviationMerge($fragments)
209
-    {
210
-        $return_fragment = [];
211
-
212
-        $previous_fragment = '';
213
-        $previous_is_abbreviation = false;
214
-        $i = 0;
215
-        foreach ($fragments as $fragment) {
216
-            $is_abbreviation = self::isAbreviation($fragment);
217
-
218
-            // merge previous fragment with this
219
-            if ($previous_is_abbreviation) {
220
-                $fragment = $previous_fragment . $fragment;
221
-            }
222
-            $return_fragment[$i] = $fragment;
223
-
224
-            $previous_is_abbreviation = $is_abbreviation;
225
-            $previous_fragment = $fragment;
226
-
227
-            // only increment if this isn't an abbreviation
228
-            if (!$is_abbreviation) {
229
-                $i++;
230
-            }
231
-        }
232
-        return $return_fragment;
233
-    }
234
-
235
-    /**
236
-     * Check if the last word of fragment starts with a Capital, ends in "." & has less than 3 characters.
237
-     *
238
-     * @param $fragment
239
-     * @return bool
240
-     */
241
-    private static function isAbreviation($fragment)
242
-    {
243
-        $words = mb_split('\s+', Multibyte::trim($fragment));
244
-
245
-        $word_count = count($words);
246
-
247
-        $last_word = Multibyte::trim($words[$word_count - 1]);
248
-        $last_is_capital = preg_match('#^\p{Lu}#u', $last_word);
249
-        $last_is_abbreviation = mb_substr(Multibyte::trim($fragment), -1) === '.';
250
-
251
-        return $last_is_capital > 0
252
-            && $last_is_abbreviation > 0
253
-            && mb_strlen($last_word) <= 3;
254
-    }
255
-
256
-    /**
257
-     * Merges any part starting with a closing parenthesis ')' to the previous
258
-     * part.
259
-     *
260
-     * @param string[] $parts
261
-     * @return string[]
262
-     */
263
-    private function parenthesesMerge($parts)
264
-    {
265
-        $subsentences = [];
266
-
267
-        foreach ($parts as $part) {
268
-            if ($part[0] === ')') {
269
-                $subsentences[count($subsentences) - 1] .= $part;
270
-            } else {
271
-                $subsentences[] = $part;
272
-            }
273
-        }
274
-
275
-        return $subsentences;
276
-    }
277
-
278
-    /**
279
-     * Looks for closing quotes to include them with the previous statement.
280
-     * "That was very interesting," he said.
281
-     * "That was very interesting."
282
-     *
283
-     * @param string[] $statements
284
-     * @return string[]
285
-     */
286
-    private function closeQuotesMerge($statements)
287
-    {
288
-        $i = 0;
289
-        $previous_statement = '';
290
-        $return = [];
291
-        foreach ($statements as $statement) {
292
-            if (self::isEndQuote($statement)) {
293
-                $statement = $previous_statement . $statement;
294
-            } else {
295
-                $i++;
296
-            }
297
-
298
-            $return[$i] = $statement;
299
-            $previous_statement = $statement;
300
-        }
301
-
302
-        return $return;
303
-    }
304
-
305
-    /**
306
-     * Check if the entire string is a quotation mark or quote, then space, then lowercase.
307
-     *
308
-     * @param $statement
309
-     * @return bool
310
-     */
311
-    private static function isEndQuote($statement)
312
-    {
313
-        $trimmed = Multibyte::trim($statement);
314
-        $first = mb_substr($statement, 0, 1);
315
-
316
-        return in_array($trimmed, ['"', '\''])
317
-            || (
318
-                in_array($first, ['"', '\''])
319
-                && mb_substr($statement, 1, 1) === ' '
320
-                && ctype_lower(mb_substr($statement, 2, 1)) === true
321
-            );
322
-    }
323
-
324
-    /**
325
-     * Merges items into larger sentences.
326
-     * Multibyte.php safe
327
-     *
328
-     * @param string[] $shorts
329
-     * @return string[]
330
-     */
331
-    private function sentenceMerge($shorts)
332
-    {
333
-        $non_abbreviating_terminals = array_diff($this->terminals, $this->abbreviators);
334
-
335
-        $sentences = [];
336
-
337
-        $sentence = '';
338
-        $has_words = false;
339
-        $previous_word_ending = null;
340
-        foreach ($shorts as $short) {
341
-            $word_count = count(mb_split('\s+', Multibyte::trim($short)));
342
-            $after_non_abbreviating_terminal = in_array($previous_word_ending, $non_abbreviating_terminals);
343
-
344
-            if ($after_non_abbreviating_terminal
345
-                || ($has_words && $word_count > 1)) {
346
-
347
-                $sentences[] = $sentence;
348
-
349
-                $sentence = '';
350
-                $has_words = false;
351
-            }
352
-
353
-            $has_words = $has_words
354
-                || $word_count > 1;
355
-
356
-            $sentence .= $short;
357
-            $previous_word_ending = mb_substr($short, -1);
358
-        }
359
-
360
-        if (!empty($sentence)) {
361
-            $sentences[] = $sentence;
362
-        }
363
-
364
-        return $sentences;
365
-    }
366
-
367
-    /**
368
-     * Return the sentences sentences detected in the provided text.
369
-     * Set the Sentence::SPLIT_TRIM flag to trim whitespace.
370
-     * @param string $text
371
-     * @param integer $flags
372
-     * @return string[]
373
-     */
374
-    public function split($text, $flags = 0)
375
-    {
376
-        static $pipeline = [
377
-            'floatNumberClean',
378
-            'punctuationSplit',
379
-            'parenthesesMerge', // also works after punctuationMerge or abbreviationMerge
380
-            'punctuationMerge',
381
-            'abbreviationMerge',
382
-            'closeQuotesMerge',
383
-            'sentenceMerge',
384
-            'floatNumberRevert'
385
-        ];
386
-
387
-        // clean funny quotes
388
-        $text = Multibyte::cleanUnicode($text);
389
-
390
-        // Split
391
-        $sentences = [];
392
-        foreach (self::linebreakSplit($text) as $input) {
393
-            if (Multibyte::trim($input) !== '') {
394
-                foreach ($pipeline as $method) {
395
-                    $input = $this->$method($input);
396
-                }
397
-                $sentences = array_merge($sentences, $input);
398
-            }
399
-        }
400
-
401
-        // Post process
402
-        if ($flags & self::SPLIT_TRIM) {
403
-            return self::trimSentences($sentences);
404
-        }
405
-
406
-        return $sentences;
407
-    }
408
-
409
-    /**
410
-     * Multibyte.php trim each string in an array.
411
-     * @param string[] $sentences
412
-     * @return string[]
413
-     */
414
-    private static function trimSentences($sentences)
415
-    {
416
-        return array_map(function ($sentence) {
417
-            return Multibyte::trim($sentence);
418
-        }, $sentences);
419
-    }
420
-
421
-    /**
422
-     * Return the number of sentences detected in the provided text.
423
-     * @param string $text
424
-     * @return integer
425
-     */
426
-    public function count($text)
427
-    {
428
-        return count($this->split($text));
429
-    }
76
+		return array_map(function($value) {
77
+			foreach ($this->floatNumbers as $number => $hash) {
78
+				$value = str_replace($hash, $number, $value);
79
+			}
80
+			return $value;
81
+		}, $text);
82
+	}
83
+
84
+	/**
85
+	 * Breaks a piece of text into lines by linebreak.
86
+	 * Eats up any linebreak characters as if one.
87
+	 *
88
+	 * Multibyte.php safe
89
+	 *
90
+	 * @param string $text
91
+	 * @return string[]
92
+	 */
93
+	private static function linebreakSplit($text)
94
+	{
95
+		$lines = [];
96
+		$line = '';
97
+
98
+		foreach (Multibyte::split('([\r\n]+)', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) {
99
+			$line .= $part;
100
+			if (Multibyte::trim($part) === '') {
101
+				$lines[] = $line;
102
+				$line = '';
103
+			}
104
+		}
105
+		$lines[] = $line;
106
+
107
+		return $lines;
108
+	}
109
+
110
+	/**
111
+	 * Splits an array of lines by (consecutive sequences of)
112
+	 * terminals, keeping terminals.
113
+	 *
114
+	 * Multibyte.php safe (atleast for UTF-8)
115
+	 *
116
+	 * For example:
117
+	 *    "There ... is. More!"
118
+	 *        ... becomes ...
119
+	 *    [ "There ", "...", " is", ".", " More", "!" ]
120
+	 *
121
+	 * @param string $line
122
+	 * @return string[]
123
+	 */
124
+	private function punctuationSplit($line)
125
+	{
126
+		$parts = [];
127
+
128
+		$chars = preg_split('//u', $line, -1, PREG_SPLIT_NO_EMPTY); // This is UTF8 multibyte safe!
129
+		$is_terminal = in_array($chars[0], $this->terminals);
130
+
131
+		$part = '';
132
+		foreach ($chars as $index => $char) {
133
+			if (in_array($char, $this->terminals) !== $is_terminal) {
134
+				$parts[] = $part;
135
+				$part = '';
136
+				$is_terminal = !$is_terminal;
137
+			}
138
+			$part .= $char;
139
+		}
140
+
141
+		if (!empty($part)) {
142
+			$parts[] = $part;
143
+		}
144
+
145
+		return $parts;
146
+	}
147
+
148
+	/**
149
+	 * Appends each terminal item after it's preceding
150
+	 * non-terminals.
151
+	 *
152
+	 * Multibyte.php safe (atleast for UTF-8)
153
+	 *
154
+	 * For example:
155
+	 *    [ "There ", "...", " is", ".", " More", "!" ]
156
+	 *        ... becomes ...
157
+	 *    [ "There ... is.", "More!" ]
158
+	 *
159
+	 * @param string[] $punctuations
160
+	 * @return string[]
161
+	 */
162
+	private function punctuationMerge($punctuations)
163
+	{
164
+		$definite_terminals = array_diff($this->terminals, $this->abbreviators);
165
+
166
+		$merges = [];
167
+		$merge = '';
168
+
169
+		$filtered = array_filter($punctuations, function ($p) {
170
+			return $p !== '';
171
+		});
172
+
173
+		foreach ($filtered as $punctuation) {
174
+			$merge .= $punctuation;
175
+			if (mb_strlen($punctuation) === 1
176
+				&& in_array($punctuation, $this->terminals)) {
177
+				$merges[] = $merge;
178
+				$merge = '';
179
+			} else {
180
+				foreach ($definite_terminals as $terminal) {
181
+					if (mb_strpos($punctuation, $terminal) !== false) {
182
+						$merges[] = $merge;
183
+						$merge = '';
184
+						break;
185
+					}
186
+				}
187
+			}
188
+		}
189
+		if (!empty($merge)) {
190
+			$merges[] = $merge;
191
+		}
192
+
193
+		return $merges;
194
+	}
195
+
196
+	/**
197
+	 * Looks for capitalized abbreviations & includes them with the following fragment.
198
+	 *
199
+	 * For example:
200
+	 *    [ "Last week, former director of the F.B.I. James B. Comey was fired. Mr. Comey was not available for comment." ]
201
+	 *        ... becomes ...
202
+	 *    [ "Last week, former director of the F.B.I. James B. Comey was fired." ]
203
+	 *  [ "Mr. Comey was not available for comment." ]
204
+	 *
205
+	 * @param string[] $fragments
206
+	 * @return string[]
207
+	 */
208
+	private function abbreviationMerge($fragments)
209
+	{
210
+		$return_fragment = [];
211
+
212
+		$previous_fragment = '';
213
+		$previous_is_abbreviation = false;
214
+		$i = 0;
215
+		foreach ($fragments as $fragment) {
216
+			$is_abbreviation = self::isAbreviation($fragment);
217
+
218
+			// merge previous fragment with this
219
+			if ($previous_is_abbreviation) {
220
+				$fragment = $previous_fragment . $fragment;
221
+			}
222
+			$return_fragment[$i] = $fragment;
223
+
224
+			$previous_is_abbreviation = $is_abbreviation;
225
+			$previous_fragment = $fragment;
226
+
227
+			// only increment if this isn't an abbreviation
228
+			if (!$is_abbreviation) {
229
+				$i++;
230
+			}
231
+		}
232
+		return $return_fragment;
233
+	}
234
+
235
+	/**
236
+	 * Check if the last word of fragment starts with a Capital, ends in "." & has less than 3 characters.
237
+	 *
238
+	 * @param $fragment
239
+	 * @return bool
240
+	 */
241
+	private static function isAbreviation($fragment)
242
+	{
243
+		$words = mb_split('\s+', Multibyte::trim($fragment));
244
+
245
+		$word_count = count($words);
246
+
247
+		$last_word = Multibyte::trim($words[$word_count - 1]);
248
+		$last_is_capital = preg_match('#^\p{Lu}#u', $last_word);
249
+		$last_is_abbreviation = mb_substr(Multibyte::trim($fragment), -1) === '.';
250
+
251
+		return $last_is_capital > 0
252
+			&& $last_is_abbreviation > 0
253
+			&& mb_strlen($last_word) <= 3;
254
+	}
255
+
256
+	/**
257
+	 * Merges any part starting with a closing parenthesis ')' to the previous
258
+	 * part.
259
+	 *
260
+	 * @param string[] $parts
261
+	 * @return string[]
262
+	 */
263
+	private function parenthesesMerge($parts)
264
+	{
265
+		$subsentences = [];
266
+
267
+		foreach ($parts as $part) {
268
+			if ($part[0] === ')') {
269
+				$subsentences[count($subsentences) - 1] .= $part;
270
+			} else {
271
+				$subsentences[] = $part;
272
+			}
273
+		}
274
+
275
+		return $subsentences;
276
+	}
277
+
278
+	/**
279
+	 * Looks for closing quotes to include them with the previous statement.
280
+	 * "That was very interesting," he said.
281
+	 * "That was very interesting."
282
+	 *
283
+	 * @param string[] $statements
284
+	 * @return string[]
285
+	 */
286
+	private function closeQuotesMerge($statements)
287
+	{
288
+		$i = 0;
289
+		$previous_statement = '';
290
+		$return = [];
291
+		foreach ($statements as $statement) {
292
+			if (self::isEndQuote($statement)) {
293
+				$statement = $previous_statement . $statement;
294
+			} else {
295
+				$i++;
296
+			}
297
+
298
+			$return[$i] = $statement;
299
+			$previous_statement = $statement;
300
+		}
301
+
302
+		return $return;
303
+	}
304
+
305
+	/**
306
+	 * Check if the entire string is a quotation mark or quote, then space, then lowercase.
307
+	 *
308
+	 * @param $statement
309
+	 * @return bool
310
+	 */
311
+	private static function isEndQuote($statement)
312
+	{
313
+		$trimmed = Multibyte::trim($statement);
314
+		$first = mb_substr($statement, 0, 1);
315
+
316
+		return in_array($trimmed, ['"', '\''])
317
+			|| (
318
+				in_array($first, ['"', '\''])
319
+				&& mb_substr($statement, 1, 1) === ' '
320
+				&& ctype_lower(mb_substr($statement, 2, 1)) === true
321
+			);
322
+	}
323
+
324
+	/**
325
+	 * Merges items into larger sentences.
326
+	 * Multibyte.php safe
327
+	 *
328
+	 * @param string[] $shorts
329
+	 * @return string[]
330
+	 */
331
+	private function sentenceMerge($shorts)
332
+	{
333
+		$non_abbreviating_terminals = array_diff($this->terminals, $this->abbreviators);
334
+
335
+		$sentences = [];
336
+
337
+		$sentence = '';
338
+		$has_words = false;
339
+		$previous_word_ending = null;
340
+		foreach ($shorts as $short) {
341
+			$word_count = count(mb_split('\s+', Multibyte::trim($short)));
342
+			$after_non_abbreviating_terminal = in_array($previous_word_ending, $non_abbreviating_terminals);
343
+
344
+			if ($after_non_abbreviating_terminal
345
+				|| ($has_words && $word_count > 1)) {
346
+
347
+				$sentences[] = $sentence;
348
+
349
+				$sentence = '';
350
+				$has_words = false;
351
+			}
352
+
353
+			$has_words = $has_words
354
+				|| $word_count > 1;
355
+
356
+			$sentence .= $short;
357
+			$previous_word_ending = mb_substr($short, -1);
358
+		}
359
+
360
+		if (!empty($sentence)) {
361
+			$sentences[] = $sentence;
362
+		}
363
+
364
+		return $sentences;
365
+	}
366
+
367
+	/**
368
+	 * Return the sentences sentences detected in the provided text.
369
+	 * Set the Sentence::SPLIT_TRIM flag to trim whitespace.
370
+	 * @param string $text
371
+	 * @param integer $flags
372
+	 * @return string[]
373
+	 */
374
+	public function split($text, $flags = 0)
375
+	{
376
+		static $pipeline = [
377
+			'floatNumberClean',
378
+			'punctuationSplit',
379
+			'parenthesesMerge', // also works after punctuationMerge or abbreviationMerge
380
+			'punctuationMerge',
381
+			'abbreviationMerge',
382
+			'closeQuotesMerge',
383
+			'sentenceMerge',
384
+			'floatNumberRevert'
385
+		];
386
+
387
+		// clean funny quotes
388
+		$text = Multibyte::cleanUnicode($text);
389
+
390
+		// Split
391
+		$sentences = [];
392
+		foreach (self::linebreakSplit($text) as $input) {
393
+			if (Multibyte::trim($input) !== '') {
394
+				foreach ($pipeline as $method) {
395
+					$input = $this->$method($input);
396
+				}
397
+				$sentences = array_merge($sentences, $input);
398
+			}
399
+		}
400
+
401
+		// Post process
402
+		if ($flags & self::SPLIT_TRIM) {
403
+			return self::trimSentences($sentences);
404
+		}
405
+
406
+		return $sentences;
407
+	}
408
+
409
+	/**
410
+	 * Multibyte.php trim each string in an array.
411
+	 * @param string[] $sentences
412
+	 * @return string[]
413
+	 */
414
+	private static function trimSentences($sentences)
415
+	{
416
+		return array_map(function ($sentence) {
417
+			return Multibyte::trim($sentence);
418
+		}, $sentences);
419
+	}
420
+
421
+	/**
422
+	 * Return the number of sentences detected in the provided text.
423
+	 * @param string $text
424
+	 * @return integer
425
+	 */
426
+	public function count($text)
427
+	{
428
+		return count($this->split($text));
429
+	}
430 430
 
431 431
 }
Please login to merge, or discard this patch.
src/Multibyte.php 2 patches
Indentation   +173 added lines, -173 removed lines patch added patch discarded remove patch
@@ -7,177 +7,177 @@
 block discarded – undo
7 7
  */
8 8
 class Multibyte
9 9
 {
10
-    //https://stackoverflow.com/questions/20025030/convert-all-types-of-smart-quotes-with-php
11
-    private static $unicodeCharacterMap = [
12
-        // Windows codepage 1252
13
-        "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark
14
-        "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark
15
-        "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark
16
-        "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark
17
-        "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark
18
-        "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark
19
-        "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark
20
-        "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark
21
-        // Regular Unicode     // U+0022 quotation mark (")
22
-        // U+0027 apostrophe     (')
23
-        "\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark
24
-        "\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark
25
-        "\xE2\x80\x98" => "'", // U+2018 left single quotation mark
26
-        "\xE2\x80\x99" => "'", // U+2019 right single quotation mark
27
-        "\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
28
-        "\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
29
-        "\xE2\x80\x9C" => '"', // U+201C left double quotation mark
30
-        "\xE2\x80\x9D" => '"', // U+201D right double quotation mark
31
-        "\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
32
-        "\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
33
-        "\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
34
-        "\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
35
-    ];
36
-
37
-    /**
38
-     * Replace
39
-     *
40
-     * @staticvar array $chr_map
41
-     * @param string $string
42
-     * @return string
43
-     */
44
-    public static function cleanUnicode($string)
45
-    {
46
-        $character = array_keys(self::$unicodeCharacterMap); // but: for efficiency you should
47
-        $replace = array_values(self::$unicodeCharacterMap); // pre-calculate these two arrays
48
-        return str_replace($character, $replace, html_entity_decode($string, ENT_QUOTES, "UTF-8"));
49
-    }
50
-
51
-    /**
52
-     * Multibyte.php safe version of standard trim() function.
53
-     *
54
-     * @param string $string
55
-     * @return string
56
-     */
57
-    public static function trim($string)
58
-    {
59
-        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
60
-    }
61
-
62
-    /**
63
-     * A cross between mb_split and preg_split, adding the preg_split flags
64
-     * to mb_split.
65
-     *
66
-     * @param string $pattern
67
-     * @param string $string
68
-     * @param int $limit
69
-     * @param int $flags
70
-     * @return array
71
-     */
72
-    public static function split($pattern, $string, $limit = -1, $flags = 0)
73
-    {
74
-        $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
75
-
76
-        $lengths = self::getSplitLengths($pattern, $string);
77
-
78
-        // Substrings
79
-        $parts = [];
80
-        $position = 0;
81
-        $count = 1;
82
-        foreach ($lengths as $length) {
83
-            if (self::isLastPart($length, $flags, $limit, $count)) {
84
-                $parts[] = self::makePart($string, $position, null, $offset_capture);
85
-                return $parts;
86
-            }
87
-
88
-            if (self::isPart($length, $flags)) {
89
-                $parts[] = self::makePart($string, $position, $length[0], $offset_capture);
90
-            }
91
-
92
-            $position += $length[0];
93
-        }
94
-
95
-        return $parts;
96
-    }
97
-
98
-    /**
99
-     * @param $length
100
-     * @param $flags
101
-     * @param $limit
102
-     * @param $count
103
-     * @return bool
104
-     */
105
-    private static function isLastPart($length, $flags, $limit, &$count)
106
-    {
107
-        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
108
-        $is_delimiter = $length[1];
109
-
110
-        return $limit > 0
111
-            && !$is_delimiter
112
-            && $split_empty
113
-            && ++$count > $limit;
114
-    }
115
-
116
-    /**
117
-     * @param $length
118
-     * @param $flags
119
-     * @return bool
120
-     */
121
-    private static function isPart($length, $flags)
122
-    {
123
-        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
124
-        $is_delimiter = $length[1];
125
-        $is_captured = ($flags & PREG_SPLIT_DELIM_CAPTURE) && $length[2];
126
-
127
-        return (!$is_delimiter
128
-                || $is_captured)
129
-            && $split_empty;
130
-    }
131
-
132
-    /**
133
-     * Make part
134
-     * @param string $string
135
-     * @param integer $position
136
-     * @param integer|null $length
137
-     * @param bool $offset_capture
138
-     * @return array|string
139
-     */
140
-    private static function makePart($string, $position, $length = null, $offset_capture = false)
141
-    {
142
-        $cut = mb_strcut($string, $position, $length);
143
-
144
-        return $offset_capture
145
-            ? [$cut, $position]
146
-            : $cut;
147
-    }
148
-
149
-    /**
150
-     * Splits the string by pattern and for each element (part or split) returns:
151
-     *  [ 0 => length, 1 => is_delimiter?, 2 =>
152
-     *
153
-     * @param $pattern
154
-     * @param $string
155
-     * @return array
156
-     */
157
-    private static function getSplitLengths($pattern, $string)
158
-    {
159
-        $strlen = strlen($string); // bytes!
160
-        $lengths = [];
161
-
162
-        mb_ereg_search_init($string);
163
-
164
-        $position = 0;
165
-        while ($position < $strlen
166
-            && ($array = mb_ereg_search_pos($pattern, '')) !== false) {
167
-            // capture split
168
-            $lengths[] = [$array[0] - $position, false, null];
169
-
170
-            // move position
171
-            $position = $array[0] + $array[1];
172
-
173
-            // capture delimiter
174
-            $regs = mb_ereg_search_getregs();
175
-            $lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
176
-        }
177
-
178
-        // Add last bit, if not ending with split
179
-        $lengths[] = [$strlen - $position, false, null];
180
-
181
-        return $lengths;
182
-    }
10
+	//https://stackoverflow.com/questions/20025030/convert-all-types-of-smart-quotes-with-php
11
+	private static $unicodeCharacterMap = [
12
+		// Windows codepage 1252
13
+		"\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark
14
+		"\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark
15
+		"\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark
16
+		"\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark
17
+		"\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark
18
+		"\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark
19
+		"\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark
20
+		"\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark
21
+		// Regular Unicode     // U+0022 quotation mark (")
22
+		// U+0027 apostrophe     (')
23
+		"\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark
24
+		"\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark
25
+		"\xE2\x80\x98" => "'", // U+2018 left single quotation mark
26
+		"\xE2\x80\x99" => "'", // U+2019 right single quotation mark
27
+		"\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
28
+		"\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
29
+		"\xE2\x80\x9C" => '"', // U+201C left double quotation mark
30
+		"\xE2\x80\x9D" => '"', // U+201D right double quotation mark
31
+		"\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
32
+		"\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
33
+		"\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
34
+		"\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
35
+	];
36
+
37
+	/**
38
+	 * Replace
39
+	 *
40
+	 * @staticvar array $chr_map
41
+	 * @param string $string
42
+	 * @return string
43
+	 */
44
+	public static function cleanUnicode($string)
45
+	{
46
+		$character = array_keys(self::$unicodeCharacterMap); // but: for efficiency you should
47
+		$replace = array_values(self::$unicodeCharacterMap); // pre-calculate these two arrays
48
+		return str_replace($character, $replace, html_entity_decode($string, ENT_QUOTES, "UTF-8"));
49
+	}
50
+
51
+	/**
52
+	 * Multibyte.php safe version of standard trim() function.
53
+	 *
54
+	 * @param string $string
55
+	 * @return string
56
+	 */
57
+	public static function trim($string)
58
+	{
59
+		return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
60
+	}
61
+
62
+	/**
63
+	 * A cross between mb_split and preg_split, adding the preg_split flags
64
+	 * to mb_split.
65
+	 *
66
+	 * @param string $pattern
67
+	 * @param string $string
68
+	 * @param int $limit
69
+	 * @param int $flags
70
+	 * @return array
71
+	 */
72
+	public static function split($pattern, $string, $limit = -1, $flags = 0)
73
+	{
74
+		$offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
75
+
76
+		$lengths = self::getSplitLengths($pattern, $string);
77
+
78
+		// Substrings
79
+		$parts = [];
80
+		$position = 0;
81
+		$count = 1;
82
+		foreach ($lengths as $length) {
83
+			if (self::isLastPart($length, $flags, $limit, $count)) {
84
+				$parts[] = self::makePart($string, $position, null, $offset_capture);
85
+				return $parts;
86
+			}
87
+
88
+			if (self::isPart($length, $flags)) {
89
+				$parts[] = self::makePart($string, $position, $length[0], $offset_capture);
90
+			}
91
+
92
+			$position += $length[0];
93
+		}
94
+
95
+		return $parts;
96
+	}
97
+
98
+	/**
99
+	 * @param $length
100
+	 * @param $flags
101
+	 * @param $limit
102
+	 * @param $count
103
+	 * @return bool
104
+	 */
105
+	private static function isLastPart($length, $flags, $limit, &$count)
106
+	{
107
+		$split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
108
+		$is_delimiter = $length[1];
109
+
110
+		return $limit > 0
111
+			&& !$is_delimiter
112
+			&& $split_empty
113
+			&& ++$count > $limit;
114
+	}
115
+
116
+	/**
117
+	 * @param $length
118
+	 * @param $flags
119
+	 * @return bool
120
+	 */
121
+	private static function isPart($length, $flags)
122
+	{
123
+		$split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
124
+		$is_delimiter = $length[1];
125
+		$is_captured = ($flags & PREG_SPLIT_DELIM_CAPTURE) && $length[2];
126
+
127
+		return (!$is_delimiter
128
+				|| $is_captured)
129
+			&& $split_empty;
130
+	}
131
+
132
+	/**
133
+	 * Make part
134
+	 * @param string $string
135
+	 * @param integer $position
136
+	 * @param integer|null $length
137
+	 * @param bool $offset_capture
138
+	 * @return array|string
139
+	 */
140
+	private static function makePart($string, $position, $length = null, $offset_capture = false)
141
+	{
142
+		$cut = mb_strcut($string, $position, $length);
143
+
144
+		return $offset_capture
145
+			? [$cut, $position]
146
+			: $cut;
147
+	}
148
+
149
+	/**
150
+	 * Splits the string by pattern and for each element (part or split) returns:
151
+	 *  [ 0 => length, 1 => is_delimiter?, 2 =>
152
+	 *
153
+	 * @param $pattern
154
+	 * @param $string
155
+	 * @return array
156
+	 */
157
+	private static function getSplitLengths($pattern, $string)
158
+	{
159
+		$strlen = strlen($string); // bytes!
160
+		$lengths = [];
161
+
162
+		mb_ereg_search_init($string);
163
+
164
+		$position = 0;
165
+		while ($position < $strlen
166
+			&& ($array = mb_ereg_search_pos($pattern, '')) !== false) {
167
+			// capture split
168
+			$lengths[] = [$array[0] - $position, false, null];
169
+
170
+			// move position
171
+			$position = $array[0] + $array[1];
172
+
173
+			// capture delimiter
174
+			$regs = mb_ereg_search_getregs();
175
+			$lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
176
+		}
177
+
178
+		// Add last bit, if not ending with split
179
+		$lengths[] = [$strlen - $position, false, null];
180
+
181
+		return $lengths;
182
+	}
183 183
 }
184 184
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@
 block discarded – undo
71 71
      */
72 72
     public static function split($pattern, $string, $limit = -1, $flags = 0)
73 73
     {
74
-        $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
74
+        $offset_capture = (bool) ($flags & PREG_SPLIT_OFFSET_CAPTURE);
75 75
 
76 76
         $lengths = self::getSplitLengths($pattern, $string);
77 77
 
Please login to merge, or discard this patch.