时间:2021-07-01 10:21:17 帮助过:22人阅读
$str="输入内容123";
坏蛋|1
火枪|1
超级大坏蛋|1
....
trie = new TTrie;
foreach(file('abc.txt') as $r) $trie->set(strtok($r, '
------解决方案--------------------
'));
$s =<<< TXT
输入内容坏蛋123
TXT;
//仅识别
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => 坏蛋
[2] => 123
)
*/
//去掉字典字
$trie->savematch = 0;
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => 123
)
*/
class mytrie extends ttrie {
function b() {
$this->buffer[] = '' . array_pop($this->buffer) . '';
}
}
$trie = new mytrie;
foreach(file('abc.txt') as $r) $trie->set(strtok($r, '
------解决方案--------------------
'), 'b');
$s =<<< TXT
输入内容坏蛋123
TXT;
print_r($trie->match($s));
/* 得
Array
(
[0] => 输入内容
[1] => 坏蛋 《== 这个就被突出显示了
[2] => 123
)
*/
$this->buffer = array();以重新初始化数据指针
$this->input = 0;
$this->backtracking = 0;