Skip to content

Commit 5794794

Browse files
committed
add get tree word feature.
1 parent 6e15862 commit 5794794

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/TrieTree.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,48 @@ public function getTree() {
185185
return $this->nodeTree;
186186
}
187187

188+
public function getTreeWord($word, $count = 0) {
189+
$search = trim($word);
190+
if (empty($search)) {
191+
return false;
192+
}
193+
if($count===0){
194+
$count = 9999;
195+
}
196+
197+
$word_keys = $this->convertStrToH($search);
198+
$tree = &$this->nodeTree;
199+
$key_count = count($word_keys);
200+
$words = [];
201+
foreach ($word_keys as $key => $val) {
202+
if (isset($tree[$val])) {
203+
//检测当前词语是否已命中
204+
if ($key == $key_count - 1 && $tree[$val]['end'] == true) {
205+
$words[] = ["word" => $tree[$val]['full'], "data" => $tree[$val]['data']];
206+
}
207+
$tree = &$tree[$val]["child"];
208+
}else{
209+
//第一个字符都没有命中
210+
if($key == 0){
211+
return [];
212+
}
213+
}
214+
}
215+
$this->_getTreeWord($tree, $count, $words);
216+
return $words;
217+
}
218+
219+
private function _getTreeWord(&$child, $count, &$words = array()) {
220+
foreach ($child as $node) {
221+
if ($node['end'] == true) {
222+
$words[] = ["word" => $node['full'], "data" => $node['data']];
223+
}
224+
if (!empty($node['child']) && $count >= count($words)) {
225+
$this->_getTreeWord($node['child'], $count, $words);
226+
}
227+
}
228+
}
229+
188230
/**
189231
* overwrite tostring.
190232
* @return string

test/console.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
据负责宣传的骨干成员刘某表示,“张大连p2p理财公司/平台天明特别会说,张口就能来一大段,跟人打交道也永远是一副笑脸。”但是,他认为张天明不踏实,喜欢吹嘘,比如要做一百万元的事情,就跟别人说做五百万元。经常宣传自己有39项专利,其实很多都是虚假的。有个什么想法,自己不去做,而是画一张大饼,召集别人去做。
5555
在“善心汇”内部,张天明每周周一到周五,晚上八点半都会在会员微信群里进行语音直播,大讲要实现人生价值,要互助共生。“通过不断的演讲,强化会员们对他的印象,认定了他是慈善家,相信\"善心汇\"是慈善事业,发展更多人进来。”刘某说。
5656
人前跟会员宣传要做好事,以慈善家包装自己的张天明,背后却在大肆敛财。综合张天明自己供述联国和公安部门调查,张天明把非法所得的十余亿元,用于给自己和家人购置大量资产,如在昆明以自己控股公司的名义花费2.2亿元购买了一座大厦。办案民警介绍说,张天明的妻子在逃跑时,随身携带了140多万元现金、29张银行卡,仅抽查的8张卡中的资金就有1100多万元。
57-
“我愿意现身说法,\"善心汇\"成立之初就是为了取得个人利益,我个人深深地感到悔恨和愧疚,\"善心汇\"如今变成了\"恶心汇\"。”在接受记者采访时,张天明说道,“不劳而获是走不通的。希望大家不要在错误道路上越走越远,能够真正意识到,这种模式的危害。”"
57+
“我愿意现身说法,\"善心汇\"成立之初就是为了取得个人利益,我个人深深地感到悔恨和愧疚,\"善心汇\"如今变成了\"恶心汇\"。”在接受记者采访时,张天明说道,“不劳而获是走不通的。希望大家不要在错误道路上越走越远,能够真正意识到,这种模式的危害。”"
5858
EOF;
5959
$t1 = microtime(true);
6060
var_dump($tree->search($str));
@@ -65,3 +65,7 @@
6565
$t3 = microtime(true);
6666
echo 'DELETE RES:'.$del_res.PHP_EOL;
6767
var_dump($tree->search($str));
68+
$t1 = microtime(true);
69+
var_dump($tree->getTreeWord(""));
70+
$t2 = microtime(true);
71+
echo 'SearchTime{' . ($t2 - $t1) . '}s'.PHP_EOL;

0 commit comments

Comments
 (0)