使用Discuz關(guān)鍵詞服務器實現(xiàn)PHP中文分詞
更新時間:2014年03月11日 10:31:16 作者:
這篇文章主要介紹了使用Discuz關(guān)鍵詞服務器實現(xiàn)PHP中文分詞的方法以及代碼實例,需要的朋友可以參考下
不同于使用自己的服務器進行分詞,Discuz!在線中文分詞服務是基于API返回分詞結(jié)果的。在項目中,我們只需要一個函數(shù)即可方便地進行分詞、關(guān)鍵詞提取。
以下是根據(jù)Discuz!在線分詞服務API寫的函數(shù),測試可正常運行:
復制代碼 代碼如下:
/**
* DZ在線中文分詞
* @param $title string 進行分詞的標題
* @param $content string 進行分詞的內(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){ //在線分詞服務有長度限制
$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訪問:
復制代碼 代碼如下:
http://keyword.discuz.com/related_kw.html?title=高三歷史全程復習精品教程&content=&ics=utf-8&ocs=utf-8
返回的XML數(shù)據(jù):
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<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>
<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 對應的經(jīng)緯度
本篇文章主要給大家介紹百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對應的經(jīng)緯度.需要的朋友可以參考下2015-08-08Zend Framework教程之Zend_Db_Table用法詳解
這篇文章主要介紹了Zend Framework教程之Zend_Db_Table用法,結(jié)合實例形式詳細分析了Zend_Db_Table的功能,使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-03-03PhpStorm的使用教程(本地運行PHP+遠程開發(fā)+快捷鍵)
這篇文章主要介紹了PhpStorm的使用教程(本地運行PHP+遠程開發(fā)+快捷鍵),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用
本篇文章介紹了,基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用。需要的朋友參考下2013-04-04