42 lines
1021 B
PHP
42 lines
1021 B
PHP
<?php
|
|
|
|
|
|
namespace app;
|
|
|
|
|
|
use Fukuball\Jieba\Jieba;
|
|
use Workerman\Protocols\Http\Request;
|
|
use Workerman\Protocols\Http\Response;
|
|
|
|
class FenciArray extends Api
|
|
{
|
|
public function __construct()
|
|
{
|
|
self::initOnce();
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
$textArray = $request->post('text');
|
|
$textArray = empty($textArray) ? null : json_decode($textArray, true);
|
|
if (empty($textArray)) {
|
|
return $this->error('参数text不可为空且必须为json数组');
|
|
}
|
|
|
|
if(is_array($textArray)){
|
|
foreach ($textArray as $i => $text) {
|
|
try{
|
|
$textArray[$i] = Jieba::cut($text);
|
|
}catch (\Exception $e){
|
|
$this->log("cut {$text} error,reason:".$e->getMessage());
|
|
$textArray[$i] = [$text];
|
|
}
|
|
}
|
|
}
|
|
return $this->success($textArray);
|
|
}
|
|
} |