コード例 #1
0
ファイル: Badword.php プロジェクト: wsyandy/Badword
 public function __construct($path)
 {
     $this->path = $path;
     $pathinfo = pathinfo($path);
     $this->dirname = $pathinfo['dirname'];
     if (is_file($path)) {
         $this->trie = trie_filter_load($this->path);
     }
 }
コード例 #2
0
ファイル: getNote.php プロジェクト: MungBeanSoup/Delete-So
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;
}
コード例 #3
0
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
$arrWord = array('Key', 'w ord', '12345678');
$resTrie = trie_filter_new();
echo "\nsave key\n";
foreach ($arrWord as $k => $v) {
    echo "id:", $k, ' -> ', $v, "\n";
    trie_filter_store($resTrie, $k, $v, TRIE_FILTER_UP | TRIE_FILTER_SP | TRIE_FILTER_NUM);
}
//去掉关键字中的空格 小写转为大写 数字转为0
//实际相当于 array('WORD', 'KEY', '00000000');
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$strContent = 'hello wo rd WORD kEy 0123456789';
$arrRet = trie_filter_search_all($resTrie, $strContent, TRIE_FILTER_UP | TRIE_FILTER_SP | TRIE_FILTER_NUM);
//小写转为大写 数字转为0 忽略文本中的空格
//实际相当于 'HELLOWORDWORDKEY0000000000'
print_all($strContent, $arrRet);
function print_all($str, $res)
{
    echo "\ntext:{$str}\n", "\nmatch ", count($res) / 3, "\n";
    for ($i = 0, $c = count($res); $i < $c; $i += 3) {
        echo 'id:', $res[$i + 2], ' -> ', substr($str, $res[$i], $res[$i + 1]), "\n";
    }
}
trie_filter_free($resTrie);
/*输出为

save key