linux版本中间件

nlp.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #ifndef __AIP_NLP_H__
  16. #define __AIP_NLP_H__
  17. #include "base/base.h"
  18. namespace aip {
  19. class Nlp: public AipBase
  20. {
  21. public:
  22. std::string _lexer =
  23. "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer";
  24. std::string _wordembedding =
  25. "https://aip.baidubce.com/rpc/2.0/nlp/v2/word_emb_vec";
  26. std::string _depparser =
  27. "https://aip.baidubce.com/rpc/2.0/nlp/v1/depparser";
  28. std::string _dnnlm_cn =
  29. "https://aip.baidubce.com/rpc/2.0/nlp/v2/dnnlm_cn";
  30. std::string _word_sim_embedding =
  31. "https://aip.baidubce.com/rpc/2.0/nlp/v1/word_emb_sim";
  32. std::string _simnet =
  33. "https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet";
  34. std::string _comment_tag =
  35. "https://aip.baidubce.com/rpc/2.0/nlp/v2/comment_tag";
  36. std::string _sentiment_classify =
  37. "https://aip.baidubce.com/rpc/2.0/nlp/v2/sentiment_classify";
  38. Nlp(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
  39. {
  40. }
  41. /**
  42. * lexer
  43. * 词法分析接口向用户提供分词、词性标注、专名识别三大功能;能够识别出文本串中的基本词汇(分词),对这些词汇进行重组、标注组合后词汇的词性,并进一步识别出命名实体。
  44. * @param text 待分析文本(目前仅支持UTF8编码),长度不超过65536字节
  45. * options 可选参数:
  46. */
  47. Json::Value lexer(
  48. std::string const & text,
  49. const std::map<std::string, std::string> & options)
  50. {
  51. Json::Value data;
  52. data["text"] = text;
  53. std::map<std::string, std::string>::const_iterator it;
  54. for(it=options.begin(); it!=options.end(); it++)
  55. {
  56. data[it->first] = it->second;
  57. }
  58. Json::Value result =
  59. this->request(_lexer, null, data.toStyledString(), null);
  60. return result;
  61. }
  62. /**
  63. * wordembedding
  64. * 词向量表示接口提供中文词向量的查询功能。
  65. * @param word 文本内容(UTF8编码),最大64字节
  66. * options 可选参数:
  67. */
  68. Json::Value wordembedding(
  69. std::string const & word,
  70. const std::map<std::string, std::string> & options)
  71. {
  72. Json::Value data;
  73. data["word"] = word;
  74. std::map<std::string, std::string>::const_iterator it;
  75. for(it=options.begin(); it!=options.end(); it++)
  76. {
  77. data[it->first] = it->second;
  78. }
  79. Json::Value result =
  80. this->request(_wordembedding, null, data.toStyledString(), null);
  81. return result;
  82. }
  83. /**
  84. * depparser
  85. * 词向量表示接口提供中文词向量的查询功能。
  86. * @param text 待分析文本(目前仅支持UTF8编码),长度不超过256字节
  87. * options 可选参数:
  88. * mode 模型选择。默认值为0,可选值mode=0(对应web模型);mode=1(对应query模型)
  89. */
  90. Json::Value depparser(
  91. std::string const & text,
  92. const std::map<std::string, std::string> & options)
  93. {
  94. Json::Value data;
  95. data["text"] = text;
  96. std::map<std::string, std::string>::const_iterator it;
  97. for(it=options.begin(); it!=options.end(); it++)
  98. {
  99. data[it->first] = it->second;
  100. }
  101. Json::Value result =
  102. this->request(_depparser, null, data.toStyledString(), null);
  103. return result;
  104. }
  105. /**
  106. * dnnlm_cn
  107. * 中文DNN语言模型接口用于输出切词结果并给出每个词在句子中的概率值,判断一句话是否符合语言表达习惯。
  108. * @param text 文本内容(UTF8编码),最大10240字节,不需要切词
  109. * options 可选参数:
  110. */
  111. Json::Value dnnlm_cn(
  112. std::string const & text,
  113. const std::map<std::string, std::string> & options)
  114. {
  115. Json::Value data;
  116. data["text"] = text;
  117. std::map<std::string, std::string>::const_iterator it;
  118. for(it=options.begin(); it!=options.end(); it++)
  119. {
  120. data[it->first] = it->second;
  121. }
  122. Json::Value result =
  123. this->request(_dnnlm_cn, null, data.toStyledString(), null);
  124. return result;
  125. }
  126. /**
  127. * word_sim_embedding
  128. * 输入两个词,得到两个词的相似度结果。
  129. * @param word_1 词1(UTF8编码),最大64字节
  130. * @param word_2 词1(UTF8编码),最大64字节
  131. * options 可选参数:
  132. * mode 预留字段,可选择不同的词义相似度模型。默认值为0,目前仅支持mode=0
  133. */
  134. Json::Value word_sim_embedding(
  135. std::string const & word_1,
  136. std::string const & word_2,
  137. const std::map<std::string, std::string> & options)
  138. {
  139. Json::Value data;
  140. data["word_1"] = word_1;
  141. data["word_2"] = word_2;
  142. std::map<std::string, std::string>::const_iterator it;
  143. for(it=options.begin(); it!=options.end(); it++)
  144. {
  145. data[it->first] = it->second;
  146. }
  147. Json::Value result =
  148. this->request(_word_sim_embedding, null, data.toStyledString(), null);
  149. return result;
  150. }
  151. /**
  152. * simnet
  153. * 短文本相似度接口用来判断两个文本的相似度得分。
  154. * @param text_1 待比较文本1(UTF8编码),最大512字节
  155. * @param text_2 待比较文本2(UTF8编码),最大512字节
  156. * options 可选参数:
  157. * model 默认为"BOW",可选"BOW"、"CNN"与"GRNN"
  158. */
  159. Json::Value simnet(
  160. std::string const & text_1,
  161. std::string const & text_2,
  162. const std::map<std::string, std::string> & options)
  163. {
  164. Json::Value data;
  165. data["text_1"] = text_1;
  166. data["text_2"] = text_2;
  167. std::map<std::string, std::string>::const_iterator it;
  168. for(it=options.begin(); it!=options.end(); it++)
  169. {
  170. data[it->first] = it->second;
  171. }
  172. Json::Value result =
  173. this->request(_simnet, null, data.toStyledString(), null);
  174. return result;
  175. }
  176. /**
  177. * comment_tag
  178. * 评论观点抽取接口用来提取一条评论句子的关注点和评论观点,并输出评论观点标签及评论观点极性。
  179. * @param text 评论内容(UTF8编码),最大10240字节
  180. * options 可选参数:
  181. * type 评论行业类型,默认为4(餐饮美食)
  182. */
  183. Json::Value comment_tag(
  184. std::string const & text,
  185. const std::map<std::string, std::string> & options)
  186. {
  187. Json::Value data;
  188. data["text"] = text;
  189. std::map<std::string, std::string>::const_iterator it;
  190. for(it=options.begin(); it!=options.end(); it++)
  191. {
  192. data[it->first] = it->second;
  193. }
  194. Json::Value result =
  195. this->request(_comment_tag, null, data.toStyledString(), null);
  196. return result;
  197. }
  198. /**
  199. * sentiment_classify
  200. * 对包含主观观点信息的文本进行情感极性类别(积极、消极、中性)的判断,并给出相应的置信度。
  201. * @param text 文本内容(UTF8编码),最大102400字节
  202. * options 可选参数:
  203. */
  204. Json::Value sentiment_classify(
  205. std::string const & text,
  206. const std::map<std::string, std::string> & options)
  207. {
  208. Json::Value data;
  209. data["text"] = text;
  210. std::map<std::string, std::string>::const_iterator it;
  211. for(it=options.begin(); it!=options.end(); it++)
  212. {
  213. data[it->first] = it->second;
  214. }
  215. Json::Value result =
  216. this->request(_sentiment_classify, null, data.toStyledString(), null);
  217. return result;
  218. }
  219. };
  220. }
  221. #endif