PHP中文分詞的簡(jiǎn)單實(shí)現(xiàn)代碼分享
更新時(shí)間:2011年07月17日 12:46:38 作者:
對(duì)于中文搜索引擎來(lái)說(shuō), 中文分詞是整個(gè)系統(tǒng)最基礎(chǔ)的部分之一, 因?yàn)槟壳盎趩巫值闹形乃阉魉惴ú⒉皇翘谩?/div>
當(dāng)然, 本文不是要對(duì)中文搜索引擎做研究, 而是分享如果用 PHP 做一個(gè)站內(nèi)搜索引擎。 本文是這個(gè)系統(tǒng)中的一篇。
我使用的分詞工具是中科院計(jì)算所的開(kāi)源版本的 ICTCLAS。 另外還有開(kāi)源的 Bamboo, 我隨后也會(huì)對(duì)該工具進(jìn)行調(diào)研。
從 ICTCLAS 出發(fā)是個(gè)不錯(cuò)的選擇, 因?yàn)槠渌惴▊鞑ケ容^廣泛, 有公開(kāi)的學(xué)術(shù)文檔, 并且編譯簡(jiǎn)單, 庫(kù)依賴少。 但目前只提供了 C/C++, Java 和 C# 版本的代碼, 并沒(méi)有 PHP 版本的代碼。 怎么辦呢? 也許可以學(xué)習(xí)它的 C/C++ 源碼和學(xué)術(shù)文檔中, 然后再開(kāi)發(fā)一個(gè) PHP 版本出來(lái)。 不過(guò), 我要使用進(jìn)程間通信, 在 PHP 代碼里調(diào)用 C/C++ 版本的可執(zhí)行文件。
下載源碼解壓后, 在有 C++ 開(kāi)發(fā)庫(kù)和編譯環(huán)境的機(jī)器上直接 make ictclas 即可。 它的 Makefile 腳本有個(gè)錯(cuò)誤, 執(zhí)行測(cè)試的代碼沒(méi)有加上'。/', 當(dāng)然不能像 Windows 下執(zhí)行成功了。 但也不影響編譯結(jié)果。
進(jìn)行中文分詞的 PHP 類就在下面了, 用 proc_open() 函數(shù)來(lái)執(zhí)行分詞程序, 并通過(guò)管道和其交互, 輸入要進(jìn)行分詞的文本, 讀取分詞結(jié)果。
<?php
class NLP{
private static $cmd_path;
// 不以'/'結(jié)尾
static function set_cmd_path($path){
self::$cmd_path = $path;
}
private function cmd($str){
$descriptorspec = array(
0 => 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;
}
/**
* 進(jìn)行分詞, 返回詞語(yǔ)列表.
*/
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__));
?>
使用起來(lái)很簡(jiǎn)單(確保 ICTCLAS 編譯后的可執(zhí)行文件和詞典在當(dāng)前目錄):
<?php
require_once('NLP.php');
var_dump(NLP::tokenize('Hello, World!'));
?>
我使用的分詞工具是中科院計(jì)算所的開(kāi)源版本的 ICTCLAS。 另外還有開(kāi)源的 Bamboo, 我隨后也會(huì)對(duì)該工具進(jìn)行調(diào)研。
從 ICTCLAS 出發(fā)是個(gè)不錯(cuò)的選擇, 因?yàn)槠渌惴▊鞑ケ容^廣泛, 有公開(kāi)的學(xué)術(shù)文檔, 并且編譯簡(jiǎn)單, 庫(kù)依賴少。 但目前只提供了 C/C++, Java 和 C# 版本的代碼, 并沒(méi)有 PHP 版本的代碼。 怎么辦呢? 也許可以學(xué)習(xí)它的 C/C++ 源碼和學(xué)術(shù)文檔中, 然后再開(kāi)發(fā)一個(gè) PHP 版本出來(lái)。 不過(guò), 我要使用進(jìn)程間通信, 在 PHP 代碼里調(diào)用 C/C++ 版本的可執(zhí)行文件。
下載源碼解壓后, 在有 C++ 開(kāi)發(fā)庫(kù)和編譯環(huán)境的機(jī)器上直接 make ictclas 即可。 它的 Makefile 腳本有個(gè)錯(cuò)誤, 執(zhí)行測(cè)試的代碼沒(méi)有加上'。/', 當(dāng)然不能像 Windows 下執(zhí)行成功了。 但也不影響編譯結(jié)果。
進(jìn)行中文分詞的 PHP 類就在下面了, 用 proc_open() 函數(shù)來(lái)執(zhí)行分詞程序, 并通過(guò)管道和其交互, 輸入要進(jìn)行分詞的文本, 讀取分詞結(jié)果。
復(fù)制代碼 代碼如下:
<?php
class NLP{
private static $cmd_path;
// 不以'/'結(jié)尾
static function set_cmd_path($path){
self::$cmd_path = $path;
}
private function cmd($str){
$descriptorspec = array(
0 => 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;
}
/**
* 進(jìn)行分詞, 返回詞語(yǔ)列表.
*/
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__));
?>
使用起來(lái)很簡(jiǎn)單(確保 ICTCLAS 編譯后的可執(zhí)行文件和詞典在當(dāng)前目錄):
復(fù)制代碼 代碼如下:
<?php
require_once('NLP.php');
var_dump(NLP::tokenize('Hello, World!'));
?>
您可能感興趣的文章:
- PHP中文分詞 自動(dòng)獲取關(guān)鍵詞介紹
- 開(kāi)源php中文分詞系統(tǒng)SCWS安裝和使用實(shí)例
- PHPAnalysis中文分詞類詳解
- php實(shí)現(xiàn)的中文分詞類完整實(shí)例
- php實(shí)現(xiàn)scws中文分詞搜索的方法
- 使用Discuz關(guān)鍵詞服務(wù)器實(shí)現(xiàn)PHP中文分詞
- 支持漢轉(zhuǎn)拼和拼音分詞的PHP中文工具類ChineseUtil
- php+正則將字符串中的字母數(shù)字和中文分割
- php中文語(yǔ)義分析實(shí)現(xiàn)方法示例
- 多訊php中文分詞擴(kuò)展 v0.1
- HTTPCWS PHP中文分詞擴(kuò)展 1.0.0
相關(guān)文章
通過(guò)PHP設(shè)置BugFree獲取郵箱通知
在本篇文章里小編給大家分享了關(guān)于通過(guò)PHP設(shè)置BugFree獲取郵箱通知的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們跟著學(xué)習(xí)下。2019-04-04php中字符查找函數(shù)strpos、strrchr與strpbrk用法
這篇文章主要介紹了php中字符查找函數(shù)strpos、strrchr與strpbrk用法,以實(shí)例形式較為詳細(xì)的分析了php中字符查找函數(shù)strpos、strrchr與strpbrk的具體用法及相關(guān)注意事項(xiàng),非常實(shí)用,需要的朋友可以參考下2014-11-11php mailer類調(diào)用遠(yuǎn)程SMTP服務(wù)器發(fā)送郵件實(shí)現(xiàn)方法
這篇文章主要介紹了php mailer類調(diào)用遠(yuǎn)程SMTP服務(wù)器發(fā)送郵件實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了php mailer類的調(diào)用及郵件發(fā)送相關(guān)技巧,需要的朋友可以參考下2016-03-03php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法
這篇文章主要介紹了php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法,詳細(xì)分析了微信支付api.mch.weixin.qq.com域名解析慢原因與使用curl_easy_setopt指定ipv4解決ipv6解析問(wèn)題的相關(guān)技巧,需要的朋友可以參考下2016-10-10如何批量替換相對(duì)地址為絕對(duì)地址(利用bat批處理實(shí)現(xiàn))
你的url鏈接是相對(duì)路徑你想把他批量替換成絕對(duì)路徑該怎么做呢?下面與大家分享下具體的實(shí)現(xiàn)思路及代碼,只需點(diǎn)擊bat文件,全部頁(yè)面里的相對(duì)地址就會(huì)變成絕對(duì)地址了2013-05-05