php-ext-trie-filterフィルタキーワード
2812 ワード
キーワードフィルタ拡張は、Double-Array Trieツリーに基づいて、テキストに敏感語が表示されているかどうかを確認するために使用されます.
libdatrieをインストールします.libdatrie-0.2.4または更新するバージョンが必要です.
libiconvに依存しています
インストール:
================
================
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make && make install
libdatrieダウンロードアドレス:
http://linux.thai.net/~thep/datrie/datrie.html#Download
インストール:
================
================
wget ftp://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz
tar -zxf libdatrie-0.2.4.tar.gz
cd libdatrie-0.2.4
./configure --prefix=/usr/local/libdatrie/
make ICONV_LIBS='/usr/local/lib/libiconv.so'
make install
PHP拡張のインストール
================
================
wget https://github.com/wulijun/php-ext-trie-filter/archive/master.zip
unzip master.zip
cd php-ext-trie-filter-master/
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-trie_filter=/usr/local/libdatrie/
make && make install
生成されるtrie_filter.soファイルはphp拡張ディレクトリにコピーし、php.iniにファイルを追加
参照先:http://blog.sina.com.cn/s/blog_5f54f0be0101dxqp.html
インストールが完了するとphpinfoにtrie_が表示されますろ過拡張
使用例
$arrWord = array('word1', 'word2', 'word3');
$resTrie = trie_filter_new(); //create an empty trie tree
foreach ($arrWord as $k => $v) {
trie_filter_store($resTrie, $v);
}
// .
trie_filter_save($resTrie, __DIR__ . '/blackword.tree');
//
$resTrie = trie_filter_load(__DIR__ . '/blackword.tree');
$strContent = 'hello word2 word1';
//
$arrRet = trie_filter_search($resTrie, $strContent);
print_r($arrRet); //Array(0 => 6, 1 => 5)
echo substr($strContent, $arrRet[0], $arrRet[1]); //word2
$arrRet = trie_filter_search_all($resTrie, $strContent);
print_r($arrRet); //Array(0 => Array(0 => 6, 1 => 5), 1 => Array(0 => 12, 1 => 5))
$arrRet = trie_filter_search($resTrie, 'hello word');
print_r($arrRet); //Array()
//
trie_filter_free($resTrie);