将所有敏感词生成 Trie 树结构,便于做敏感词检测,生成代码如下
class TrieNode
{
private static $TrieTree;
public function __construct()
{
static::$TrieTree = [];
}
public function insert($sensWords): TrieNode
{
$words = preg_split('//u', $sensWords, -1, PREG_SPLIT_NO_EMPTY);
$_tree = &static::$TrieTree;
foreach ($words as $key => $_word) {
if (!isset($_tree[$_word])) {
$_tree[$_word] = [
'isEnd' => !isset($words[$key + 1]),
'child' => []
];
}
$_tree = &$_tree[$_word]['child'];
}
return $this;
}
public function getTree()
{
return static::$TrieTree;
}
}
$treeNode = (new TrieNode)->insert('CNM')->insert('MLGB')->insert('WRNM')->getTree();
echo json_encode($treeNode,JSON_UNESCAPED_UNICODE);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。