Nlp.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |ZBPHP[基于ThinkPHP5.1开发]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016-2019 http://www.benbenwangluo.com
  6. // +----------------------------------------------------------------------
  7. // | Author 似水星辰 [ 2630481389@qq.com ]
  8. // +----------------------------------------------------------------------
  9. // | 中犇软件 技术六部 出品
  10. // +----------------------------------------------------------------------
  11. namespace addons\BaiduAi\controller;
  12. use app\common\controller\Common;
  13. require_once(dirname(dirname(__FILE__))."/sdk/AipNlp.php");
  14. /**
  15. * 自然语言处理
  16. * @author 晓风<215628355@qq.com>
  17. * @package plugins\Sms\controller
  18. */
  19. class Nlp extends Common
  20. {
  21. public $client;
  22. public function __construct(){
  23. $config = addons_config('BaiduAi');
  24. $this->client = new \AipOcr($config['app_id'], $config['api_key'], $config['secret_key']);
  25. }
  26. public function setConnectionTimeoutInMillis($ms){
  27. $this->client->setConnectionTimeoutInMillis($ms);
  28. }
  29. public function setSocketTimeoutInMillis($ms){
  30. $this->client->setSocketTimeoutInMillis($ms);
  31. }
  32. public function __call($method, $args){
  33. $ret = call_user_func_array([$this->client, $method], $args);
  34. if(isset($ret['error_code']) && $ret['error_code'] != 0){
  35. $error_msg = $this->getErrorMsg($ret['error_code'])?:$ret['error_msg'];
  36. exception($error_msg);
  37. }else{
  38. return $ret;
  39. }
  40. }
  41. public function getErrorMsg($errorCode){
  42. $errors = [
  43. '4' => '集群超限额',
  44. '14' => 'IAM鉴权失败,建议用户参照文档自查生成sign的方式是否正确,或换用控制台中ak sk的方式调用',
  45. '17' => '每天流量超限额',
  46. '18' => 'QPS超限额',
  47. '19' => '请求总量超限额',
  48. '100' => '无效参数',
  49. '110' => 'Access Token失效',
  50. '111' => 'Access token过期',
  51. '282000' => '内部错误',
  52. '282002' => '编码错误,请使用GBK编码',
  53. '282004' => '请求中包含非法参数,请检查后重新尝试',
  54. '282130' => '当前查询无结果返回,出现此问题的原因一般为:参数配置存在问题,请检查后重新尝试',
  55. '282131' => '输入长度超限,请查看文档说明',
  56. '282133' => '接口参数缺失',
  57. '282300' => 'word不在算法词典中',
  58. '282301' => 'word_1提交的词汇暂未收录,无法比对相似度',
  59. '282302' => 'word_2提交的词汇暂未收录,无法比对相似度',
  60. '282303' => 'word_1和word_2暂未收录,无法比对相似度',
  61. ];
  62. return $errors[$errorCode]?: '';
  63. }
  64. }