AipNlp.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /*
  3. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * Http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. require_once 'lib/AipBase.php';
  18. class AipNlp extends AipBase {
  19. /**
  20. * 词法分析 lexer api url
  21. * @var string
  22. */
  23. private $lexerUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer';
  24. /**
  25. * 词法分析(定制版) lexer_custom api url
  26. * @var string
  27. */
  28. private $lexerCustomUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer_custom';
  29. /**
  30. * 依存句法分析 dep_parser api url
  31. * @var string
  32. */
  33. private $depParserUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/depparser';
  34. /**
  35. * 词向量表示 word_embedding api url
  36. * @var string
  37. */
  38. private $wordEmbeddingUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/word_emb_vec';
  39. /**
  40. * DNN语言模型 dnnlm_cn api url
  41. * @var string
  42. */
  43. private $dnnlmCnUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/dnnlm_cn';
  44. /**
  45. * 词义相似度 word_sim_embedding api url
  46. * @var string
  47. */
  48. private $wordSimEmbeddingUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/word_emb_sim';
  49. /**
  50. * 短文本相似度 simnet api url
  51. * @var string
  52. */
  53. private $simnetUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet';
  54. /**
  55. * 评论观点抽取 comment_tag api url
  56. * @var string
  57. */
  58. private $commentTagUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/comment_tag';
  59. /**
  60. * 情感倾向分析 sentiment_classify api url
  61. * @var string
  62. */
  63. private $sentimentClassifyUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify';
  64. /**
  65. * 文本标签 keyword api url
  66. * @var string
  67. */
  68. private $keywordUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/keyword';
  69. /**
  70. * 格式化结果
  71. * @param $content string
  72. * @return mixed
  73. */
  74. protected function proccessResult($content){
  75. return json_decode(mb_convert_encoding($content, 'UTF8', 'GBK'), true, 512, JSON_BIGINT_AS_STRING);
  76. }
  77. /**
  78. * 词法分析接口
  79. *
  80. * @param string $text - 待分析文本(目前仅支持GBK编码),长度不超过65536字节
  81. * @param array $options - 可选参数对象,key: value都为string类型
  82. * @description options列表:
  83. * @return array
  84. */
  85. public function lexer($text, $options=array()){
  86. $data = array();
  87. $data['text'] = $text;
  88. $data = array_merge($data, $options);
  89. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  90. return $this->request($this->lexerUrl, $data);
  91. }
  92. /**
  93. * 词法分析(定制版)接口
  94. *
  95. * @param string $text - 待分析文本(目前仅支持GBK编码),长度不超过65536字节
  96. * @param array $options - 可选参数对象,key: value都为string类型
  97. * @description options列表:
  98. * @return array
  99. */
  100. public function lexerCustom($text, $options=array()){
  101. $data = array();
  102. $data['text'] = $text;
  103. $data = array_merge($data, $options);
  104. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  105. return $this->request($this->lexerCustomUrl, $data);
  106. }
  107. /**
  108. * 依存句法分析接口
  109. *
  110. * @param string $text - 待分析文本(目前仅支持GBK编码),长度不超过256字节
  111. * @param array $options - 可选参数对象,key: value都为string类型
  112. * @description options列表:
  113. * mode 模型选择。默认值为0,可选值mode=0(对应web模型);mode=1(对应query模型)
  114. * @return array
  115. */
  116. public function depParser($text, $options=array()){
  117. $data = array();
  118. $data['text'] = $text;
  119. $data = array_merge($data, $options);
  120. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  121. return $this->request($this->depParserUrl, $data);
  122. }
  123. /**
  124. * 词向量表示接口
  125. *
  126. * @param string $word - 文本内容(GBK编码),最大64字节
  127. * @param array $options - 可选参数对象,key: value都为string类型
  128. * @description options列表:
  129. * @return array
  130. */
  131. public function wordEmbedding($word, $options=array()){
  132. $data = array();
  133. $data['word'] = $word;
  134. $data = array_merge($data, $options);
  135. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  136. return $this->request($this->wordEmbeddingUrl, $data);
  137. }
  138. /**
  139. * DNN语言模型接口
  140. *
  141. * @param string $text - 文本内容(GBK编码),最大512字节,不需要切词
  142. * @param array $options - 可选参数对象,key: value都为string类型
  143. * @description options列表:
  144. * @return array
  145. */
  146. public function dnnlm($text, $options=array()){
  147. $data = array();
  148. $data['text'] = $text;
  149. $data = array_merge($data, $options);
  150. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  151. return $this->request($this->dnnlmCnUrl, $data);
  152. }
  153. /**
  154. * 词义相似度接口
  155. *
  156. * @param string $word1 - 词1(GBK编码),最大64字节
  157. * @param string $word2 - 词1(GBK编码),最大64字节
  158. * @param array $options - 可选参数对象,key: value都为string类型
  159. * @description options列表:
  160. * mode 预留字段,可选择不同的词义相似度模型。默认值为0,目前仅支持mode=0
  161. * @return array
  162. */
  163. public function wordSimEmbedding($word1, $word2, $options=array()){
  164. $data = array();
  165. $data['word_1'] = $word1;
  166. $data['word_2'] = $word2;
  167. $data = array_merge($data, $options);
  168. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  169. return $this->request($this->wordSimEmbeddingUrl, $data);
  170. }
  171. /**
  172. * 短文本相似度接口
  173. *
  174. * @param string $text1 - 待比较文本1(GBK编码),最大512字节
  175. * @param string $text2 - 待比较文本2(GBK编码),最大512字节
  176. * @param array $options - 可选参数对象,key: value都为string类型
  177. * @description options列表:
  178. * model 默认为"BOW",可选"BOW"、"CNN"与"GRNN"
  179. * @return array
  180. */
  181. public function simnet($text1, $text2, $options=array()){
  182. $data = array();
  183. $data['text_1'] = $text1;
  184. $data['text_2'] = $text2;
  185. $data = array_merge($data, $options);
  186. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  187. return $this->request($this->simnetUrl, $data);
  188. }
  189. /**
  190. * 评论观点抽取接口
  191. *
  192. * @param string $text - 评论内容(GBK编码),最大10240字节
  193. * @param array $options - 可选参数对象,key: value都为string类型
  194. * @description options列表:
  195. * type 评论行业类型,默认为4(餐饮美食)
  196. * @return array
  197. */
  198. public function commentTag($text, $options=array()){
  199. $data = array();
  200. $data['text'] = $text;
  201. $data = array_merge($data, $options);
  202. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  203. return $this->request($this->commentTagUrl, $data);
  204. }
  205. /**
  206. * 情感倾向分析接口
  207. *
  208. * @param string $text - 文本内容(GBK编码),最大102400字节
  209. * @param array $options - 可选参数对象,key: value都为string类型
  210. * @description options列表:
  211. * @return array
  212. */
  213. public function sentimentClassify($text, $options=array()){
  214. $data = array();
  215. $data['text'] = $text;
  216. $data = array_merge($data, $options);
  217. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  218. return $this->request($this->sentimentClassifyUrl, $data);
  219. }
  220. /**
  221. * 文本标签接口
  222. *
  223. * @param string $title - 篇章的标题,最大80字节
  224. * @param string $content - 篇章的正文,最大65535字节
  225. * @param array $options - 可选参数对象,key: value都为string类型
  226. * @description options列表:
  227. * @return array
  228. */
  229. public function keyword($title, $content, $options=array()){
  230. $data = array();
  231. $data['title'] = $title;
  232. $data['content'] = $content;
  233. $data = array_merge($data, $options);
  234. $data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');
  235. return $this->request($this->keywordUrl, $data);
  236. }
  237. }