Discuz!在线中文分词服务

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, 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中的数据,读取到数组对象中  
    $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数据:

<?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>

参考:http://www.jb51.net/article/47952.htm

转载于:https://www.cnblogs.com/DaBing0806/p/5377403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值