/
Sentence.php
47 lines (42 loc) · 989 Bytes
/
Sentence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Rubix\ML\Tokenizers;
use function preg_split;
/**
* Sentence
*
* This tokenizer matches sentences starting with a letter and ending with a punctuation mark.
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class Sentence implements Tokenizer
{
/**
* The regular expression to match sentences in a blob of text.
*
* @var string
*/
protected const SENTENCE_REGEX = '/(?<=[\.\?\⸮\؟\!\n])(\n+|\s+|\b(?=\D))(?=[\'\"\w])/iu';
/**
* Tokenize a blob of text.
*
* @param string $text
* @return list<string>
*/
public function tokenize(string $text) : array
{
return preg_split(self::SENTENCE_REGEX, $text, -1, PREG_SPLIT_NO_EMPTY) ?: [];
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Sentence';
}
}