使用Discuz關(guān)鍵詞服務(wù)器實現(xiàn)PHP中文分詞
不同于使用自己的服務(wù)器進(jìn)行分詞,Discuz!在線中文分詞服務(wù)是基于API返回分詞結(jié)果的。在項目中,我們只需要一個函數(shù)即可方便地進(jìn)行分詞、關(guān)鍵詞提取。
以下是根據(jù)Discuz!在線分詞服務(wù)API寫的函數(shù),測試可正常運行:
/**
* DZ在線中文分詞
* @param $title string 進(jìn)行分詞的標(biāo)題
* @param $content string 進(jìn)行分詞的內(nèi)容
* @param $encode string API返回的數(shù)據(jù)編碼
* @return array 得到的關(guān)鍵詞數(shù)組
*/
function dz_segment($title = '', $content = '', $encode = 'utf-8'){
if($title == ''){
return false;
}
$title = rawurlencode(strip_tags($title));
$content = strip_tags($content);
if(strlen($content)>2400){ //在線分詞服務(wù)有長度限制
$content = mb_substr($content, 0, 800, $encode);
}
$content = rawurlencode($content);
$url = 'http://keyword.discuz.com/related_kw.html?title='.$title.'&content='.$content.'&ics='.$encode.'&ocs='.$encode;
$xml_array=simplexml_load_file($url); //將XML中的數(shù)據(jù),讀取到數(shù)組對象中
$result = $xml_array->keyword->result;
$data = array();
foreach ($result->item as $key => $value) {
array_push($data, (string)$value->kw);
}
if(count($data) > 0){
return $data;
}else{
return false;
}
}
分詞示例,通過url訪問:
返回的XML數(shù)據(jù):
<total_response>
<svalid>36000</svalid>
<keyword>
<info>
<count>1</count>
<errno>0</errno>
<nextuptime>1291287160</nextuptime>
<keep>0</keep>
</info>
<result>
<item>
<kw><![CDATA[高三歷史]]></kw>
</item>
</result>
</keyword>
</total_response>
相關(guān)文章
百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對應(yīng)的經(jīng)緯度
本篇文章主要給大家介紹百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對應(yīng)的經(jīng)緯度.需要的朋友可以參考下2015-08-08Zend Framework教程之Zend_Db_Table用法詳解
這篇文章主要介紹了Zend Framework教程之Zend_Db_Table用法,結(jié)合實例形式詳細(xì)分析了Zend_Db_Table的功能,使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-03-03PhpStorm的使用教程(本地運行PHP+遠(yuǎn)程開發(fā)+快捷鍵)
這篇文章主要介紹了PhpStorm的使用教程(本地運行PHP+遠(yuǎn)程開發(fā)+快捷鍵),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03PHP 對接美團(tuán)大眾點評團(tuán)購券(門票)的開發(fā)步驟
這篇文章主要介紹了PHP 對接美團(tuán)大眾點評團(tuán)購券(門票)的開發(fā)步驟,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用
本篇文章介紹了,基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用。需要的朋友參考下2013-04-04