加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用Discuz关键词服务器实现PHP中文分词

发布时间:2020-12-12 20:06:07 所属栏目:PHP教程 来源:网络整理
导读:不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的。在项目中,我们只需要一个函数即可方便地进行分词、关键词提

不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的。在项目中,我们只需要一个函数即可方便地进行分词、关键词提取。
以下是根据Discuz!在线分词服务API写的函数,测试可正常运行:

代码如下:
/**
* DZ在线中文分词
* @param $title string 进行分词的标题
* @param $content string 进行分词的内容
* @param $encode string API返回的数据编码
* @return array 得到的关键词数组
*/
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,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中的数据,读取到数组对象中
$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数据:


代码如下:


36000


1
0
1291287160
0







(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读