thinkphp5的整合结巴分词
首先:通过 composer 安装结巴分词模块
composer require fukuball/jieba-php:dev-master
以下是核心代码根据个人业务自行调整
<?php
namespace app\api\controller;
// 必须有,且内存不小于1024M
ini_set('memory_limit', '1024M');
use app\admin\library\Auth;
use app\common\controller\Api;
use fast\Random;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\exception\PDOException;
// 结巴分词PHP模块
use Fukuball\Jieba\Jieba; //必须
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\JiebaAnalyse; //关键词提取
Jieba::init(); //必须
Finalseg::init();
JiebaAnalyse::init();
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 大数据获取
*/
public function get_big_data(){
$keyword = $this->request->request('seach');
$where=[];
if (!empty($keyword)) {
// 使用关键词提取,返回 5 个 TF/IDF 权重最大的关键词,默认为 20 个
$fenci = JiebaAnalyse::extractTags($keyword, 5);
$keywords_fenci = [];
foreach ($fenci as $key => $value) {
// 将关键词组合为 Like 所需格式的数组
$keywords_fenci[] = '%' . $key. '%';
}
// 搜索标题和简介字段
$where['name'] = array('like', $keywords_fenci);
}
$list = db('big_data')->where($where)->limit(0,100)->select();
$this->success(__('获取成功'),$list);
}
}