-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFilter.class.php
More file actions
237 lines (173 loc) · 4.96 KB
/
TextFilter.class.php
File metadata and controls
237 lines (173 loc) · 4.96 KB
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
<?php
/**
*
* +------------------------------------------------
* 敏感詞過濾類
* +------------------------------------------------
* @author gaosongwang <songwanggao@gmail.com>
* +-------------------------------------------------
* @version 2016/6/3
* +-------------------------------------------------
*/
namespace Addcn\Model\Tools\Filter;
use Addcn\Core\Library\Cache;
class TextFilter{
public $path;
private $sensitive = array();
private $sensitiveDic = null;
// private $readType = 'file';
public function __construct($configPath){
$this->path = $configPath;
}
public function read(){
if(is_readable($this->path)){
$this->sensitive = file_get_contents(
$this->path,
false,
stream_context_create(array(
'http' => array(
'header' => array('timeout'=>20)
),
'https' => array(
'header' => array('timeout' => 20)
),
))
);
}else{
die('Dic file access deny!');
}
}
/**
*
* 敏感詞字典創建
*/
public function sensitiveWordDic(){
$key = __CLASS__.":".__METHOD__.'filter';
if( $sesitive = \cache::get($key) ) return $sesitive;
$sesitive = $this->createSensitiveWordDic();
//set cache
\cache::set($key, $sesitive, 3600*3);
return $sesitive;
}
public static function clearCache(){
$key = __CLASS__.":".__METHOD__.'filter';
return \cache::delete($key);
}
/**
*
* 文本搜索
* @param unknown_type $content
* @param unknown_type $encoding
*/
public function sensitiveWordSearch($content, $encoding = 'utf-8'){
$conlen = mb_strlen($content, $encoding);
$sessitiveWord = new \ArrayObject(array());
//get dic config;
$hashMap = $this->sensitiveWordDic();
for($j=0; $j < $conlen; $j++){
$word = strtolower(mb_substr($content, $j, 1, $encoding));
//escape word;
if(empty($word) || $this->isEscapeWord($word, $encoding)){
continue;
}
$nowNode = $hashMap->offsetGet($word);
if( !$nowNode instanceof \ArrayObject ){
continue;
}
$end = $j+1;
while ($end < $conlen){
$newword = strtolower(mb_substr($content, $end, 1, $encoding));
//escape new word;
if(empty($newword) || $this->isEscapeWord($newword, $encoding)){
$end++;
continue;
}
if( $nowNode->offsetGet('isEnd') == 1){
$sword = mb_substr($content,$j,$end-$j, $encoding);
$swordNum = $sessitiveWord->offsetGet($sword);
$sessitiveWord->offsetSet($sword, ($swordNum ? $swordNum : 0)+1);
}
$nextNode = $nowNode->offsetGet($newword);
if(!$nextNode instanceof \ArrayObject) break;
$nowNode = $nextNode;
$end++;
}
}
return $sessitiveWord->getArrayCopy();
}
/**
*
* 字典創建
* @param unknown_type $encoding
* @throws \RuntimeException
*/
public function createSensitiveWordDic($encoding = 'utf-8'){
$this->read();
if( empty($this->sensitive) ) throw new \RuntimeException('敏感詞表為空!');
$hashMap = new \ArrayObject(array());
$strArr = explode(',', $this->sensitive);
foreach ($strArr as $k=>$v){
$v = trim($v);
$len = mb_strlen($v,$encoding);
$nowMap = $hashMap;
for ($i=0; $i<$len; $i++){
$word = mb_substr(trim($v), $i, 1, $encoding);
if( empty($word) || $this->isEscapeWord($word) ) continue;
if( !$nowMap instanceof \ArrayObject) continue;
$wordMap = $nowMap->offsetGet($word);
if($wordMap){
$nowMap = $wordMap;
}else{
$newMap = new \ArrayObject(array());
$newMap -> offsetSet('isEnd',0);
$nowMap->offsetSet(strtolower($word), $newMap);
$nowMap = $newMap;
}
if($i == $len-1){
$nowMap->offsetSet('isEnd', 1);
}
}
}
return $hashMap;
}
public function escapeWord($content , $encoding = 'utf-8'){
$conlen = mb_strlen($content, $encoding);
$n = '';
for($j=0; $j < $conlen; $j++){
$word = mb_substr($content, $j, 1, $encoding);
if( empty($word) || $this->isEscapeWord($word, $encoding)) continue;
$n .= $word;
}
return $n;
}
/**
*
* 特殊文本過濾
* @param unknown_type $word
* @param unknown_type $encoding
* return boolean
* a-z ={97,122},
* A-Z ={65,90}
* 1-9 ={49,57}
* @ = {64}
* word ={19968,40869}
*/
public function isEscapeWord($word, $encoding = 'utf-8'){
$unicode = Unicode::charCodeAt($word, $encoding);
switch (true){
case $unicode >= 97 && $unicode <= 122 :
return false;
break;
case $unicode >=65 && $unicode <= 90 :
return false;
break;
case $unicode >=19968 && $unicode <=40869 :
return false;
break;
case $unicode == 64:
return false;
break;
}
return true;
}
}