当前位置:   article > 正文

ros+科大迅飞语音包+图灵机器人(三)通过图灵进行语义理解_tuling_nlu.cpp

tuling_nlu.cpp
  • 首先我们需要下载
  • sduo apt install libcurl3 libcurl4-openssl-dev
  • sudo apt install libjsoncpp0 libjsoncpp-dev
  • 然后到图灵的官网(http://www.tuling123.com/)申请一个帐号 ,申请完后,需要创建一个机器人,我这里的机器人叫”聊天机器人“,创建好后你会获得一个密匙,我们需要把密匙后的开关关掉,不然等下会出错

  • 在vioce_system 包中的src文件夹下创建一个tuling_nlu.cpp文件
  • 以下是tuling_nlu.cpp的内容(注意,需要把代码中的key改为自己创建机器人的key,把 curl_easy_setopt(pCurl, CURLOPT_URL, "http://openapi.tuling123.com/openapi/api"); 
    中的网址改为自己机器人的接口地址,这个地址可以在机器人的api文档中找到)
    1. #include<ros/ros.h>
    2. #include<std_msgs/String.h>
    3. #include<iostream>
    4. #include<sstream>
    5. #include<jsoncpp/json/json.h>
    6. #include<curl/curl.h>
    7. #include<string>
    8. #include<exception>
    9. using namespace std;
    10. int flag = 0;
    11. string result;
    12. int writer(char *data, size_t size, size_t nmemb, string *writerData)
    13. {
    14. unsigned long sizes = size * nmemb;
    15. if (writerData == NULL)
    16. return -1;
    17. writerData->append(data, sizes);
    18. return sizes;
    19. }
    20. int parseJsonResonse(string input)
    21. {
    22. Json::Value root;
    23. Json::Reader reader;
    24. bool parsingSuccessful = reader.parse(input, root);
    25. if(!parsingSuccessful)
    26. {
    27. std::cout<<"!!! Failed to parse the response data"<< std::endl;
    28. return -1;
    29. }
    30. const Json::Value code = root["code"];
    31. const Json::Value text = root["text"];
    32. result = text.asString();
    33. flag = 1;
    34. std::cout<< "response code:" << code << std::endl;
    35. std::cout<< "response text:" << result << std::endl;
    36. return 0;
    37. }
    38. int HttpPostRequest(string input)
    39. {
    40. string buffer;
    41. std::string strJson = "{";
    42. strJson += "\"key\" : \"093f05b003c643bbae919b353c3a12ff\","; //双引号前加/防转仪
    43. strJson += "\"info\" : ";
    44. strJson += "\"";
    45. strJson += input;
    46. strJson += "\"";
    47. strJson += "}";
    48. std::cout<<"post json string: " << strJson << std::endl;
    49. try
    50. {
    51. CURL *pCurl = NULL;
    52. CURLcode res;
    53. // In windows, this will init the winsock stuff
    54. curl_global_init(CURL_GLOBAL_ALL);
    55. // get a curl handle
    56. pCurl = curl_easy_init();
    57. if (NULL != pCurl)
    58. {
    59. // 设置超时时间为10秒
    60. curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 10);
    61. // First set the URL that is about to receive our POST.
    62. // This URL can just as well be a
    63. // https:// URL if that is what should receive the data.
    64. curl_easy_setopt(pCurl, CURLOPT_URL, "http://openapi.tuling123.com/openapi/api");
    65. //curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.cgi");
    66. // 设置http发送的内容类型为JSON
    67. curl_slist *plist = curl_slist_append(NULL,"Content-Type:application/json;charset=UTF-8");
    68. curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
    69. // 设置要POST的JSON数据
    70. curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, strJson.c_str());
    71. curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer);
    72. curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer);
    73. // Perform the request, res will get the return code
    74. res = curl_easy_perform(pCurl);
    75. // Check for errors
    76. if (res != CURLE_OK)
    77. {
    78. printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
    79. }
    80. // always cleanup
    81. curl_easy_cleanup(pCurl);
    82. }
    83. curl_global_cleanup();
    84. }
    85. catch (std::exception &ex)
    86. {
    87. printf("curl exception %s.\n", ex.what());
    88. }
    89. if(buffer.empty())
    90. {
    91. std::cout<< "!!! ERROR The Tuling sever response NULL" << std::endl;
    92. }
    93. else
    94. {
    95. parseJsonResonse(buffer);
    96. }
    97. return 0;
    98. }
    99. void arvCallBack(const std_msgs::String::ConstPtr &msg)
    100. {
    101. std::cout<<"your quesion is: " << msg->data << std::endl;
    102. HttpPostRequest(msg->data);
    103. }
    104. int main(int argc, char **argv)
    105. {
    106. ros::init(argc, argv,"tuling_nlu_node");
    107. ros::NodeHandle nd;
    108. ros::Subscriber sub = nd.subscribe("voice/tuling_nlu_topic", 10, arvCallBack);
    109. ros::Publisher pub = nd.advertise<std_msgs::String>("/voice/xf_tts_topic", 10);
    110. ros::Rate loop_rate(10);
    111. while(ros::ok())
    112. {
    113. if(flag)
    114. {
    115. std_msgs::String msg;
    116. msg.data = result;
    117. pub.publish(msg);
    118. flag = 0;
    119. }
    120. ros::spinOnce();
    121. loop_rate.sleep();
    122. }
    123. }
  • 在CMakeList文件中加入
    1. add_executable(tuling_nlu_node src/tuling_nlu.cpp)
    2. target_link_libraries(tuling_nlu_node ${catkin_LIBRARIES} -lcurl -ljsoncpp)
  • 修改好的CMakeList如下
    1. cmake_minimum_required(VERSION 2.8.3)
    2. project(voice_system)
    3. find_package(catkin REQUIRED COMPONENTS
    4. roscpp
    5. rospy
    6. std_msgs
    7. )
    8. include_directories(
    9. include
    10. ${catkin_INCLUDE_DIRS}
    11. )
    12. add_executable(xf_tts_node src/xf_tts.cpp)
    13. target_link_libraries(xf_tts_node ${catkin_LIBRARIES} -lmsc -lrt -ldl -lpthread)
    14. add_executable(tuling_nlu_node src/tuling_nlu.cpp)
    15. target_link_libraries(tuling_nlu_node ${catkin_LIBRARIES} -lcurl -ljsoncpp)
  • 在catkin_ws编译一下
  • $ cd catkin_ws
  • $ catkin_make
  • 现在就可以和机器人对话了
  • 首先roscore一下,在catkin_ws下运行xf_tts_node节点和tuling_nlu_node节点
  • $ rosrun voice_system xf_tts_node
  • $ rosrun voice_system tuling_nlu_node
  • $ rostopic pub -1 /voice/tuling_nlu_topic std_msgs/String "明天东莞天气"
  • 在此,你便能听到图灵机器人的回复了


该文章主要是对自己学习的一些总结,方便以后学习,也对学习该方面的人提供一些帮助,如有问题请指出。

同时该文章也借鉴了ros小课堂的一些内容。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/225474
推荐阅读
相关标签
  

闽ICP备14008679号