1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TextUtils; |
4
|
|
|
|
5
|
|
|
use TextUtils\Exceptions\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class NGram |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The length of the n-gram. |
11
|
|
|
* |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $n; |
15
|
|
|
|
16
|
|
|
protected $string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* NGram constructor. |
20
|
|
|
* |
21
|
|
|
* @param int $n |
22
|
|
|
* @param string $string |
23
|
|
|
* |
24
|
|
|
* @throws \TextUtils\Exceptions\InvalidArgumentException |
25
|
|
|
*/ |
26
|
|
|
public function __construct(int $n, string $string) |
27
|
|
|
{ |
28
|
|
|
$this->setN($n); |
29
|
|
|
$this->setString($string); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Static wrapper for n-gram generator. |
34
|
|
|
* |
35
|
|
|
* @param string $text |
36
|
|
|
* @param int $n |
37
|
|
|
* |
38
|
|
|
* @throws \TextUtils\Exceptions\InvalidArgumentException |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public static function for(string $text, int $n = 3) |
43
|
|
|
{ |
44
|
|
|
return (new static($n, $text))->get(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Static wrapper to generate a bigram. |
49
|
|
|
* |
50
|
|
|
* @param string $text |
51
|
|
|
* |
52
|
|
|
* @throws \TextUtils\Exceptions\InvalidArgumentException |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public static function bigram(string $text) : array |
57
|
|
|
{ |
58
|
|
|
return self::for($text, 2); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Static wrapper to generate a trigram. |
63
|
|
|
* |
64
|
|
|
* @param string $text |
65
|
|
|
* |
66
|
|
|
* @throws \TextUtils\Exceptions\InvalidArgumentException |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public static function trigram(string $text) : array |
71
|
|
|
{ |
72
|
|
|
return self::for($text, 3); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generate the N-gram for the provided string. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function get() : array |
81
|
|
|
{ |
82
|
|
|
$nGrams = []; |
83
|
|
|
|
84
|
|
|
$text = $this->getString(); |
85
|
|
|
$max = \mb_strlen($text); |
86
|
|
|
$n = $this->getN(); |
87
|
|
|
for ($i = 0; $i + $n <= $max; $i++) { |
88
|
|
|
$partial = ''; |
89
|
|
|
for ($j = 0; $j < $n; $j++) { |
90
|
|
|
$partial .= $text[$j + $i]; |
91
|
|
|
} |
92
|
|
|
$nGrams[] = $partial; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $nGrams; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getN() : int |
102
|
|
|
{ |
103
|
|
|
return $this->n; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the length of the n-gram. |
108
|
|
|
* |
109
|
|
|
* @param int $n |
110
|
|
|
* |
111
|
|
|
* @throws \TextUtils\Exceptions\InvalidArgumentException |
112
|
|
|
* |
113
|
|
|
* @return \TextUtils\NGram |
114
|
|
|
*/ |
115
|
|
|
public function setN(int $n) : NGram |
116
|
|
|
{ |
117
|
|
|
if ($n < 1) { |
118
|
|
|
throw new InvalidArgumentException('Provided number cannot be smaller than 1'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->n = $n; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the string to create the n-gram for. |
128
|
|
|
* |
129
|
|
|
* @param string $string |
130
|
|
|
* |
131
|
|
|
* @return \TextUtils\NGram |
132
|
|
|
*/ |
133
|
|
|
public function setString(string $string) : NGram |
134
|
|
|
{ |
135
|
|
|
$this->string = $string; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the string used for the n-gram. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getString() : string |
146
|
|
|
{ |
147
|
|
|
return $this->string; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|