-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdfaFilter.php
294 lines (257 loc) · 7.34 KB
/
dfaFilter.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* DFA有穷状态自动机
* Created by PhpStorm.
* User: Wxz
* Date: 2020/7/8
* Time: 11:13
*/
class DfaFilter
{
/**
* 哈希表变量
* @var array|null
*/
private $wordTree = [];
/**
* 待检测语句长度
* @var int
*/
private $contentLength = 0;
/**
* 符号不进行查找
* @var array
*/
private $InvalidWords = [' ', ',', '~', '!', '@', '#', '$', '%', '^', '*', '_', '=', '?', '<', '>', ',', '。', '/', '\\', '|', '《', '》', '?', ';', ':', ':', '\'', '‘', ';', '“'];
private static $_instance = null;
private function __construct()
{
}
private function __clone()
{
// TODO: Implement __clone() method.
}
/**
* @Notes:
* @Function 单例
* @author: Wxz
* @Time: 2020/7/8 11:24
* @return DfaFilter
*/
public static function init()
{
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* @Notes:
* @Function getHashMap
* @author: Wxz
* @Time: 2020/7/8 11:24
* @return array|null
*/
public function getWordTree()
{
return $this->wordTree;
}
/**
* @Notes:
* @Function setHashMap
* @author: Wxz
* @Time: 2020/7/8 11:24
* @param array $wordTree
* @return bool
*/
public function setWordTree($wordTree = [])
{
$this->wordTree = $wordTree;
return true;
}
/**
* @Notes:
* @Function 构建铭感词树【文件模式】
* @author: Wxz
* @Time: 2020/7/8 11:21
* @param string $filepath
* @return $this
* @throws \Exception
*/
public function setTreeByFile($file_path = '')
{
if (!file_exists($file_path) || !is_readable($file_path)) {
throw new \Exception('词库文件不存在');
}
// 词库树初始化
$this->wordTree = $this->wordTree ?: [];
foreach ($this->yieldToReadFile($file_path) as $word) {
$this->buildWordToTree(trim($word));
}
return $this;
}
/**
* @Notes:
* @Function yieldToReadFile
* @author: Wxz
* @Time: 2020/7/8 11:22
* @param $filepath
* @return \Generator
*/
protected function yieldToReadFile($file_path = '')
{
$fp = fopen($file_path, 'r');
while (!feof($fp)) {
yield fgets($fp);
}
fclose($fp);
}
/**
* @Notes:
* @Function 数组形式设置敏感词树
* @author: Wxz
* @Time: 2020/7/8 11:28
* @param array $sensitiveWords
* @return $this
* @throws \Exception
*/
public function setTree($sensitiveWords = [])
{
if (empty($sensitiveWords)) {
throw new \Exception('词库不能为空');
}
$this->wordTree = [];
foreach ($sensitiveWords as $word) {
$word = Forum::TermBlankSpace($word);
$this->buildWordToTree($word);
}
return $this;
}
/**
* @Notes:
* @Function 构建敏感词库
* @author: Wxz
* @Time: 2020/7/8 11:19
* @param $strWord
*/
public function buildWordToTree($strWord)
{
if (empty($strWord)) return;
$length = mb_strlen($strWord, 'UTF-8');
// 传址递归添加子树
$arrHashMap = &$this->wordTree;
for ($i = 0; $i < $length; $i++) {
$word = mb_substr($strWord, $i, 1, 'UTF-8');
if (!isset($arrHashMap[$word])) {
// 不存在
$arrHashMap[$word] = [];
$arrHashMap[$word]['end'] = false;
}
//判断是否为最后一个字
if ($i == ($length - 1)) {
$arrHashMap[$word]['end'] = true;
}
// 传址
$arrHashMap = &$arrHashMap[$word];
}
}
/**
* 检测文字中的敏感词
* @author: Wxz
* @param string $content 待检测内容
* @param int $matchType 匹配类型 [默认为最小匹配规则]
* @return array
*/
public function getBadWord($content = '', $matchType = 1)
{
if (empty($content)) {
return [];
}
$this->contentLength = mb_strlen($content, 'utf-8');
$badWordList = [];
for ($length = 0; $length < $this->contentLength; $length++) {
$matchFlag = 0;
//存放结束的词结束位置
$flag = [];
$tempMap = $this->wordTree;
for ($i = $length; $i < $this->contentLength; $i++) {
$keyChar = mb_substr($content, $i, 1, 'utf-8');
//标点符号跳出
if (in_array($keyChar, $this->InvalidWords)) {
break;
}
// 获取指定节点树
$nowMap = $tempMap[$keyChar];
// 不存在节点树,直接返回
if (empty($nowMap)) {
break;
}
// 存在,则判断是否为最后一个
$tempMap = $nowMap;
// 找到相应key,偏移量+1
$matchFlag++;
// 如果为最后一个匹配规则,结束循环,返回匹配标识数
if (false === $nowMap['end']) {
continue;
}
//记录铭感次的结束位置
$flag[] = $matchFlag;
// 最小规则,直接退出
if (1 === $matchType) {
break;
}
}
//判断该是否有敏感词
if (empty($flag)) {
continue;
}
//敏感词
foreach ($flag as $value) {
$badWordList[] = mb_substr($content, $length, $value, 'utf-8');
}
// 需匹配内容标志位往后移
$length = $length + end($flag) - 1;
}
return $badWordList;
}
/**
* @Notes:
* @Function 被检测内容是否合法
* @author: Wxz
* @Time: 2020/7/8 11:29
* @param $content
* @return bool
*/
public function isLegal($content)
{
$this->contentLength = mb_strlen($content, 'utf-8');
for ($length = 0; $length < $this->contentLength; $length++) {
$matchFlag = 0;
$tempMap = $this->wordTree;
for ($i = $length; $i < $this->contentLength; $i++) {
$keyChar = mb_substr($content, $i, 1, 'utf-8');
// 获取指定节点树
$nowMap = $tempMap[$keyChar];
// 不存在节点树,直接返回
if (empty($nowMap)) {
break;
}
// 找到相应key,偏移量+1
$tempMap = $nowMap;
$matchFlag++;
// 如果为最后一个匹配规则,结束循环,返回匹配标识数
if (false === $nowMap['end']) {
continue;
}
return true;
}
// 找到相应key
if ($matchFlag <= 0) {
continue;
}
// 需匹配内容标志位往后移
$length = $length + $matchFlag - 1;
}
return false;
}
}