Skip to content

Commit 366a878

Browse files
committed
修改错误注释,支持获取规定数量的敏感感词
1 parent 31e3a23 commit 366a878

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/DfaFilter/SensitiveHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ public function setTree($sensitiveWords = null)
9898
* 检测文字中的敏感词
9999
*
100100
* @param string $content 待检测内容
101-
* @param int $matchType 匹配类型【1获取首个敏感词】
101+
* @param int $matchType 匹配类型 [默认为最小匹配规则]
102+
* @param int $wordNum 需要获取的敏感词数量 [默认获取全部]
102103
* @return array
103104
*/
104-
public function getBadWord($content, $matchType = 1)
105+
public function getBadWord($content, $matchType = 1, $wordNum = 0)
105106
{
106107
$this->contentLength = mb_strlen($content, 'utf-8');
107108
$badWordList = array();
@@ -147,8 +148,14 @@ public function getBadWord($content, $matchType = 1)
147148
if ($matchFlag <= 0) {
148149
continue;
149150
}
151+
150152
$badWordList[] = mb_substr($content, $length, $matchFlag, 'utf-8');
151153

154+
// 有返回数量限制
155+
if ($wordNum > 0 && count($badWordList) == $wordNum) {
156+
return $badWordList;
157+
}
158+
152159
// 需匹配内容标志位往后移
153160
$length = $length + $matchFlag - 1;
154161
}

tests/BaseTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ public function setUp()
2323

2424
public function testGetBadWord()
2525
{
26-
$content = '这是一段测试语句,请忽略赌球网';
26+
$content = '这是一段测试语句,请忽略赌球网, 第二个敏感词是三级片';
2727

2828
// 过滤,其中【赌球网】在词库中
2929
$filterContent = SensitiveHelper::init()
3030
->setTree($this->wordData)
3131
->getBadWord($content);
3232

33-
$this->assertEquals('赌球网',$filterContent[0]);
33+
// 返回规定数量的敏感词,其中【赌球网,三级片】在词库中
34+
$badWords = SensitiveHelper::init()
35+
->setTree($this->wordData)
36+
->getBadWord($content, 1, 2);
37+
38+
$this->assertEquals('赌球网', $filterContent[0]);
39+
$this->assertEquals('三级片', $badWords[1]);
3440
}
3541

3642
public function testFilterWord()

tests/ProTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ public function setUp()
2323

2424
public function testGetBadWord()
2525
{
26-
$content = '这是一段测试语句,请忽略赌球网';
26+
$content = '这是一段测试语句,请忽略赌球网, 第二个敏感词是三级片';
2727

2828
// 过滤,其中【赌球网】在词库中
2929
$filterContent = SensitiveHelper::init()
3030
->setTreeByFile($this->wordPoolPath)
3131
->getBadWord($content);
3232

33+
// 返回规定数量的敏感词,其中【赌球网,三级片】在词库中
34+
$badWords = SensitiveHelper::init()
35+
->setTreeByFile($this->wordPoolPath)
36+
->getBadWord($content, 1, 2);
37+
3338
$this->assertEquals('赌球网', $filterContent[0]);
39+
$this->assertEquals('三级片', $badWords[1]);
3440
}
3541

3642
public function testFilterWord()

0 commit comments

Comments
 (0)