这里需要用到PHPAnalysis,下载链接如下
https://github.com/feixuekeji/PHPAnalysis
把压缩包下载下来后解压放到vendor目录下(以tp5为例,其他目录也行),如下图:
//分词
function pscw($text){
Vendor('phpanalysis','.class.php'); //Vendor方法导入Vendor/PhpAnalysis目录下的phpanalysis.class.php类文件
\PhpAnalysis::$loadInit = false; //初始化类时不直接加载词典
$phpanalysis = new \PhpAnalysis('utf-8', 'utf-8', false);
$phpanalysis->LoadDict(); //载入词典
$phpanalysis->SetSource($text);
$phpanalysis->StartAnalysis(false);
$phpanalysis_tags = $phpanalysis->GetFinallyResult($spword=',');
$return = explode(',',$phpanalysis_tags);
$return=array_filter($return);
foreach ($return as $key=>$value){
$return[$key] = '%'.$value.'%';
}
return $return;
}
//分词查询
$condition = trim($info['condition']);
$pscwArr = $this->pscw($condition);
$where['name'] =array('like',$pscwArr,'AND');
$date = Db::name('information')
->where($where)
->select();
/**
* 关键字提取方法
*
* @param $title string
* 进行分词的标题
* @param $content string
* 进行分词的内容
* @return array 得到的关键词数组
*/
function getKeywords($title = "", $content = "") {
if (empty ( $title )) {
return array ();
}
if (empty ( $content )) {
return array ();
}
$data = $title . $title . $title . $content; // 为了增加title的权重,这里连接3次
//这个地方写上phpanalysis对应放置路径
Vendor('phpanalysis','.class.php');
\PhpAnalysis::$loadInit = false; //初始化类时是否直接加载词典,选是载入速度较慢,但解析较快;选否载入较快,但解析较慢
$pa = new PhpAnalysis ( 'utf-8', 'utf-8', false );
$pa->LoadDict (); //载入词典
$pa->SetSource ( $data ); //设置源字符串
$pa->StartAnalysis ( true ); //是否对结果进行优化
$tags = $pa->GetFinallyKeywords (4); // 获取文章中的五个关键字
$tagsArr = explode (",",$tags);
return $tagsArr;//返回关键字数组
}