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