/**
* 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=Discuz!在线中文分词、关键词提取服务&content=不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的。在项目中,我们只需要一个函数即可方便地进行分词、关键词提取。&ics=utf-8&ocs=utf-8得到的分词结果为:ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。