對於中文搜尋引擎來說, 中文分詞是整個系統最基礎的部分之一, 因為目前基於單字的中文搜尋演算法並不是太好。 當然, 本文不是要對中文搜尋引擎做研究, 而是分享如果用 PHP 做一個站內搜尋引擎。 本文是這個系統中的一篇。
我使用的分詞工具是中科院計算所的開源版本的 ICTCLAS。 另外還有開源的 Bamboo, 我隨後也會對該工具進行調研。
從 ICTCLAS 出發是個不錯的選擇, 因為其演算法傳播比較廣泛, 有公開的學術文檔, 並且編譯簡單, 庫依賴少。 但目前只提供了 C/C++, Java 和 C# 版本的代碼, 並沒有 PHP 版本的代碼。 怎麼辦呢? 也許可以學習它的 C/C++ 源碼和學術文檔中, 然後再開發一個 PHP 版本出來。 不過, 我要使用處理序間通訊, 在 PHP 代碼裡調用 C/C++ 版本的可執行檔。
下載源碼解壓後, 在有 C++ 開發庫和編譯環境的機器上直接 make ictclas 即可。 它的 Makefile 指令碼有個錯誤, 執行測試的代碼沒有加上'。/', 當然不能像 Windows 下執行成功了。 但也不影響編譯結果。
進行中文分詞的 PHP 類就在下面了, 用 proc_open() 函數來執行分詞程式, 並通過管道和其互動, 輸入要進行分詞的文本, 讀取分詞結果。
array("pipe", "r"), 1 => array("pipe", "w"), ); $cmd = self::$cmd_path . "/ictclas"; $process = proc_open($cmd, $descriptorspec, $pipes); if (is_resource($process)) { $str = iconv('utf-8', 'gbk', $str); fwrite($pipes[0], $str); $output = stream_get_contents($pipes[1]); fclose($pipes[0]); fclose($pipes[1]); $return_value = proc_close($process); } /* $cmd = "printf '$input' | " . self::$cmd_path . "/ictclas"; exec($cmd, $output, $ret); $output = join("\n", $output); */ $output = trim($output); $output = iconv('gbk', 'utf-8', $output); return $output; } /** * 進行分詞, 返回詞語列表. */ function tokenize($str){ $tokens = array(); $output = self::cmd($input); if($output){ $ps = preg_split('/\s+/', $output); foreach($ps as $p){ list($seg, $tag) = explode('/', $p); $item = array( 'seg' => $seg, 'tag' => $tag, ); $tokens[] = $item; } } return $tokens; }}NLP::set_cmd_path(dirname(__FILE__));?>
使用起來很簡單(確保 ICTCLAS 編譯後的可執行檔和詞典在目前的目錄):
http://www.bkjia.com/PHPjc/752514.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752514.htmlTechArticle對於中文搜尋引擎來說, 中文分詞是整個系統最基礎的部分之一, 因為目前基於單字的中文搜尋演算法並不是太好。 當然, 本文不是要對中...