PHP 生成 Trie 树
发表于:2024-11-24 作者:热门IT资讯网编辑
编辑最后更新 2024年11月24日,将所有敏感词生成 Trie 树结构,便于做敏感词检测,生成代码如下class TrieNode{ private static $TrieTree; public function __c
将所有敏感词生成 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);