Esempio n. 1
0
function clear_word($contents)
{
    // 载入词典,成功返回一个 Trie_Filter 资源句柄,失败返回 NULL
    return $contents;
    if ($contents == '') {
        return '';
    }
    $file = trie_filter_load('./word-filter.dic');
    // 检测文本中是否含有词典中定义的敏感词(假设敏感词设定为:‘敏感词’)
    $res = trie_filter_search($file, $contents);
    var_dump($res);
    if (empty($res) == false) {
        trie_filter_free($file);
        //最后别忘记调用free
        return '';
    }
    //$res1 = trie_filter_search_all($file, $str1);  // 一次把所有的敏感词都检测出来
    //$res2 = trie_filter_search($file, $str2);// 每次只检测一个敏感词
    trie_filter_free($file);
    //最后别忘记调用free
    return $contents;
}
foreach ($arrWord as $k => $v) {
    trie_filter_store($resTrie, $v);
}
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$str = 'hello word2 haha word1 word4 word2';
$arrRet = trie_filter_search($resTrie, $str);
print_all($str, array($arrRet));
//Array(0 => 6, 1 => 5)
echo "\ntest1///////////////////\n";
$str = 'hello word2 haha word1 word4 word2';
$arrRet = trie_filter_search_all($resTrie, $str);
print_all($str, $arrRet);
echo "\ntest2///////////////////\n";
$str = 'hello word';
$arrRet = trie_filter_search($resTrie, $str);
print_all($str, array($arrRet));
//Array()
$arrRet = trie_filter_search_all($resTrie, 'hello word');
print_all($str, $arrRet);
echo "\ntest3///////////////////\n";
echo "start memory=" . memory_get_usage(true) . "\n";
date_default_timezone_set('Asia/Chongqing');
$test = array('a', 'abd', 'dad', 'pab', 'dda', 'word1f', 'cword1', 'cword1t');
foreach ($test as $v) {
    //    echo "per start memory=".memory_get_usage(true)."\n";
    $arrRet = trie_filter_search_all($resTrie, $v);
    //  echo "per end memory=".memory_get_usage(true)."\n";
    //print_all($v, $arrRet);
}
echo "end memory=" . memory_get_usage(true) . "\n";