Skip to content

Commit 6bfd47d

Browse files
committed
add search string count feature.
1 parent 02ae0d0 commit 6bfd47d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/TrieTree.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct() {
2929
* 从树种摘除一个文本
3030
* @param $str
3131
*/
32-
public function delete($str,$deltree=false) {
32+
public function delete($str, $deltree = false) {
3333
$str = trim($str);
3434
$delstr_arr = $this->_convertStrToH($str);
3535
$len = count($delstr_arr);
@@ -58,9 +58,9 @@ public function delete($str,$deltree=false) {
5858
}
5959
$idx = $len - 1;
6060
//删除整棵树
61-
if($deltree){
61+
if ($deltree) {
6262
//清空子集
63-
$del_index[$idx]['index']['child']=array();
63+
$del_index[$idx]['index']['child'] = array();
6464
}
6565
//只有一个字 直接删除
6666
if ($idx == 0) {
@@ -216,12 +216,17 @@ public function search($search) {
216216
if (isset($tree[$search_keys[$i]])) {
217217
$node = $tree[$search_keys[$i]];
218218
if ($node['end']) {
219-
// print_r(PHP_EOL.$i . '+' . PHP_EOL);
220-
//发现结尾 将原词以及自定义属性纳入返回结果集中
221-
$hit_arr[md5($node['full'])] = array(
222-
'words' => $node['full'],
223-
'data' => $node['data']
224-
);
219+
//发现结尾 将原词以及自定义属性纳入返回结果集中 3.1 增加词频统计
220+
$key = md5($node['full']);
221+
if (isset($hit_arr[$key])) {
222+
$hit_arr[$key]['count'] += 1;
223+
} else {
224+
$hit_arr[$key] = array(
225+
'words' => $node['full'],
226+
'data' => $node['data'],
227+
'count' => 1
228+
);
229+
}
225230
if (empty($node['child'])) {
226231
//若不存在子集,检索游标还原
227232
$i = $current_index;

0 commit comments

Comments
 (0)