PHP 生成 Trie 树
发布时间:2020-12-13 21:30:07 所属栏目:PHP教程 来源:网络整理
导读:将所有敏感词生成 Trie 树结构,便于做敏感词检测,生成代码如下 class TrieNode{ private static $TrieTree; public function __construct() { static::$TrieTree = []; } public function insert($sensWords): TrieNode { $words = preg_split(‘//u‘,$se
将所有敏感词生成 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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |