/
FilterSensitiveWord.php
160 lines (136 loc) · 3.46 KB
/
FilterSensitiveWord.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
<?php
header("Content-type:text/html; charset=utf-8");
class MyMap
{
public $isEnd = 0;
private $keySpace = [];
public function get($key)
{
return isset($this->keySpace[$key]) ? $this->keySpace[$key] : null;
}
public function put($key, $value)
{
$this->keySpace[$key] = $value;
}
public function hasNext()
{
return !empty($this->keySpace);
}
}
class MyFilter
{
public $map = null;
public function addWordToMap($word)
{
$len = mb_strlen($word);
if (is_null($this->map)) {
$map = new MyMap();
} else {
$map = $this->map;
}
$tmp = $map;
for ($i = 0; $i < $len; $i++) {
$nowWord = mb_substr($word, $i, 1);
$nowMap = $map->get($nowWord);
if (!is_null($nowMap)) {
$map = $nowMap;
} else {
$newMap = new MyMap();
$map->put($nowWord, $newMap);
$map = $newMap;
}
if ($i == ($len - 1)) {
$map->isEnd = 1;
}
}
$this->map = $tmp;
}
//最大匹配(有问题)
public function searchFromMap($string)
{
$len = mb_strlen($string);
$tmp = $this->map;
$map = $this->map;
$str = '';
$result = [];
for ($i = 0; $i < $len; $i++) {
$nowWord = mb_substr($string, $i, 1);
$nowMap = $map->get($nowWord);
if (!is_null($nowMap)) {
$str .= $nowWord;
if ($nowMap->isEnd) {
if (!in_array($str, $result)) {
array_push($result, $str);
}
$str = '';
$map = $tmp;
} else {
$map = $nowMap;
}
} else {
if (!empty($str)) {
$i--;
}
$str = '';
$map = $tmp;
}
}
return $result;
}
//左侧全量匹配
public function leftFullSearch($string)
{
$len = mb_strlen($string);
$tmp = $this->map;
$map = $this->map;
$str = '';
$result = [];
for ($i = 0; $i < $len; $i++) {
$nowWord = mb_substr($string, $i, 1);
$nowMap = $map->get($nowWord);
if (!is_null($nowMap)) {
$str .= $nowWord;
if ($nowMap->isEnd) {
if (!in_array($str, $result)) {
array_push($result, $str);
}
if ($nowMap->hasNext()) {
$map = $nowMap;
} else {
$str = '';
$map = $tmp;
}
} else {
$map = $nowMap;
}
} else {
if (!empty($str)) {
$i--;
}
$str = '';
$map = $tmp;
}
}
return $result;
}
public function matchSearch($string)
{
}
}
$example = new MyFilter();
$example->addWordToMap('中国人');
$example->addWordToMap('中国男人');
$example->addWordToMap('中国女人');
$example->addWordToMap('中国男');
$example->addWordToMap('中国女');
$example->addWordToMap('中国');
$example->addWordToMap('男人');
$example->addWordToMap('女人');
$example->addWordToMap('男');
$example->addWordToMap('女');
$example->addWordToMap('人');
$str = '我是中国人,我爱中国,中国男人发送中国女人男到中国男人发送';
var_dump($example->searchFromMap($str));
//var_dump($example->map);
//var_dump($result);
var_dump($example->leftFullSearch($str));