-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDfa.Class.php
More file actions
188 lines (158 loc) · 5.83 KB
/
Dfa.Class.php
File metadata and controls
188 lines (158 loc) · 5.83 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
<?php
class DFA
{
public $arrHashMap = [];
public $filePath = 'sensive_word_demo_1.xml';
// public $filePath = 'chinese_word_list.txt';
public $fileType = '2'; // 可选 1.txt 2.xml ( Txt空格分隔,Xml 可用Excel转换, Xml敏感词存放于第一张表第一列中 )
public $keyWord = [];
public $content = '';
public function __construct($content) {
$this->content = $content;
$this->keyWord = $this->getKeyWord();
// echo "关键字总数量:".count($this->keyWord)."<br>".PHP_EOL;
foreach ($this->keyWord as $k => $v) {
$this->addKeyWord($v);
}
}
public function getKeyWord() {
$data = [];
//txt文件读取
if (1 == $this->fileType) {
$str = file_get_contents($this->filePath);
$eol = array("\r\n", "\n", "\r");
$str = str_replace($eol, '', $str);
$data = explode(' ', $str);
$this->utf8_encoding($data);
} else if (2 == $this->fileType) {
//xml文件读取
require_once 'PHPExcel/Classes/PHPExcel.php';
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2003XML');
$objPHPExcel = $objReader->load($this->filePath);
$sheet = $objPHPExcel->getSheet(0);
$highestRowNum = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn);
$filed = array();
for ($i = 0; $i < $highestColumnNum; $i++) {
$cellName = PHPExcel_Cell::stringFromColumnIndex($i) . '1';
$cellVal = $sheet->getCell($cellName)->getValue();//取得列内容
$filed [] = $cellVal;
}
for ($i = 1; $i <= $highestRowNum; $i++) {
for ($j = 0; $j < $highestColumnNum; $j++) {
$cellName = PHPExcel_Cell::stringFromColumnIndex($j) . $i;
$cellVal = $sheet->getCell($cellName)->getValue();
$data[] = $cellVal;
}
}
}
$this->utf8_encoding($data);
return $data;
}
public function utf8_encoding(&$arr) {
foreach ($arr as $k => &$v) {
if (!mb_detect_encoding($v, 'utf-8', true)) {
$v = iconv('gbk', 'utf-8', $v);
} else {
//TODO if the encoding is utf-8
}
}
}
public function getHashMap() {
echo json_encode($this->arrHashMap);
}
// 分隔字的索引存在 end = 1 不存在 end = 0
public function addKeyWord($strWord) {
$len = mb_strlen($strWord, 'UTF-8');
$arrHashMap = &$this->arrHashMap;
for ($i = 0; $i < $len; $i++) {
$word = mb_substr($strWord, $i, 1, 'UTF-8');
if (isset($arrHashMap[$word])) {
if ($i == ($len - 1)) {
$arrHashMap[$word]['end'] = 1;
}
} else {
if ($i == ($len - 1)) {
$arrHashMap[$word] = [];
$arrHashMap[$word]['end'] = 1;
} else {
$arrHashMap[$word] = [];
$arrHashMap[$word]['end'] = 0;
}
}
$arrHashMap = &$arrHashMap[$word]; //origin
}
}
public function splitWithKeyWord($strWord, $isAllowDuplicate = 1) {
$len = mb_strlen($strWord, 'UTF-8');
$arrHashMap = $this->arrHashMap;
$allBadWord = [];
$badWord = '';
for ($i = 0; $i < $len; $i++) {
$word = mb_substr($strWord, $i, 1, 'UTF-8');
if (!isset($arrHashMap[$word])) {
// reset hashmap
$arrHashMap = $this->arrHashMap;
if (!empty($badWord)) $i--;
$badWord = '';
continue;
} else {
$badWord .= $word;
}
if ($arrHashMap[$word]['end']) {
if ($isAllowDuplicate || !in_array($badWord, $allBadWord)) {
$allBadWord[] = $badWord;
}
$result = true;
}
$arrHashMap = $arrHashMap[$word];
}
return $allBadWord;
}
public function replaceKeyWord($strWord, $replace = '*') {
$len = mb_strlen($strWord, 'UTF-8');
$arrHashMap = $this->arrHashMap;
$content = '';
$badWord = '';
$repeatCount = [];
for ($i = 0; $i < $len; $i++) {
$word = mb_substr($strWord, $i, 1, 'UTF-8');
if (!in_array($i, $repeatCount)) {
$content .= $word;
}
$repeatCount[] = $i;
if (!isset($arrHashMap[$word])) {
// reset hashmap
$arrHashMap = $this->arrHashMap;
if (!empty($badWord)) $i--;
$badWord = '';
continue;
} else {
$badWord .= $word;
}
if ($arrHashMap[$word]['end']) {
$badWordLength = mb_strlen($badWord);
$contentLentgh = mb_strlen($content);
$content = mb_substr($content, 0, $contentLentgh - $badWordLength, 'UTF-8');
$replaceContent = '';
for ($b = 0; $b < $badWordLength; $b++) {
$replaceContent .= $replace;
}
$content .= $replaceContent;
}
$arrHashMap = $arrHashMap[$word];
}
return $content;
}
public function origin($content) {
$count = [];
foreach ($this->keyWord as $k => $v) {
if (strpos($content, $v) > 0) {
$count[] = $v;
}
}
return $count;
}
}