speech
All Data Structures Functions Modules Pages
start-speech.cpp
1 /*
2  * Copyright (C) 2017 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Vadim Tikhanoff
4  * email: vadim.tikhanoff@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16  */
17 
18 #include "start-speech.h"
19 #include <yarp/os/Log.h>
20 #include <yarp/os/LogStream.h>
21 
22 /**********************************************************/
23 bool STARTModule::configure(yarp::os::ResourceFinder &rf)
24 {
25  moduleName = rf.check("name", yarp::os::Value("start-speech"), "module name (string)").asString();
26 
27  setName(moduleName.c_str());
28 
29  handlerPortName = "/";
30  handlerPortName += getName();
31  handlerPortName += "/rpc:i";
32 
33  if (!rpcPort.open(handlerPortName.c_str()))
34  {
35  yError( "%s : Unable to open port %s\n", getName().c_str(), handlerPortName.c_str());
36  return false;
37  }
38 
39  attach(rpcPort);
40 
41  /* create the thread and pass pointers to the module parameters */
42  startManager = new STARTManager( moduleName );
43 
44  /* now start the thread to do the work */
45  startManager->open();
46 
47  return true ;
48 }
49 
50 /**********************************************************/
51 bool STARTModule::interruptModule()
52 {
53  rpcPort.interrupt();
54  return true;
55 }
56 
57 /**********************************************************/
58 bool STARTModule::close()
59 {
60  rpcPort.close();
61  yInfo( "starting the shutdown procedure\n");
62  startManager->interrupt();
63  startManager->close();
64  yInfo( "deleting thread\n");
65  delete startManager;
66  yInfo( "done deleting thread\n");
67  return true;
68 }
69 
70 /**********************************************************/
71 bool STARTModule::updateModule()
72 {
73  return !closing;
74 }
75 
76 /**********************************************************/
77 double STARTModule::getPeriod()
78 {
79  return 0.1;
80 }
81 
82 /**********************************************************/
83 STARTManager::~STARTManager()
84 {
85 
86 }
87 
88 /**********************************************************/
89 STARTManager::STARTManager( const std::string &moduleName )
90 {
91  yInfo("initialising Variables\n");
92  this->moduleName = moduleName;
93 }
94 
95 /**********************************************************/
96 bool STARTManager::open()
97 {
98  this->useCallback();
99 
100  //create all ports
101  inSpeechPortName = "/" + moduleName + "/speech:i";
102  yarp::os::BufferedPort<yarp::os::Bottle >::open( inSpeechPortName.c_str() );
103 
104  outSTARTUrlName = "/" + moduleName + "/url";
105  portURL.openFake(outSTARTUrlName); // don't use a full, real port
106 
107  outSTARTPortName = "/" + moduleName + "/start:o";
108  startOutPort.open( outSTARTPortName.c_str() );
109 
110  yarp::os::Network::connect("/yarpIOS/speechPort:o", "/speechSTART/speech:i");
111 
112  return true;
113 }
114 
115 /**********************************************************/
116 void STARTManager::close()
117 {
118  yInfo("now closing ports...\n");
119  startOutPort.close();
120  portURL.close();
121  yarp::os::BufferedPort<yarp::os::Bottle >::close();
122  yInfo("finished closing the read port...\n");
123 }
124 
125 /**********************************************************/
126 void STARTManager::interrupt()
127 {
128  yInfo("cleaning up...\n");
129  yInfo("attempting to interrupt ports\n");
130  yarp::os::BufferedPort<yarp::os::Bottle >::interrupt();
131  portURL.interrupt();
132  yInfo("finished interrupt ports\n");
133 }
134 
135 /**********************************************************/
136 void STARTManager::onRead(yarp::os::Bottle &bot)
137 {
138  yarp::os::Bottle &final = startOutPort.prepare();
139  final.clear();
140  //yInfo("input: %s \n", bot.toString().c_str());
141 
142  if (bot.size()>0){
143 
144  std::string question = bot.toString();
145 
146  if (isalpha(question[0]))
147  yInfo("avoiding erase\n");
148  else{
149 
150  question.erase( remove_if(question.begin(), question.end(), aZCheck), question.end());
151  }
152 
153  yInfo("cmd is %s", question.c_str());
154 
155  yarp::os::Bottle &request = final.addList();
156 
157  std::stringstream s(question);
158  std::string word;
159 
160  std::string speech = "";
161 
162  bool process = true;
163 
164  //format it correctly for START
165  for (int i = 0; s >> word; i++){
166 
167  if (i==0 && strcmp (word.c_str(), "no")==0)
168  process=false;
169  else if (i==0 && strcmp (word.c_str(), "yes")==0)
170  process=false;
171  else if (i==0 && strcmp (word.c_str(), "Skip")==0)
172  process=false;
173 
174  speech += word.c_str();
175  request.addString(word);
176  speech += "+";
177  }
178 
179  //clean it
180  speech.erase(speech.size()-1,1);
181 
182  if (!process)
183  {
184  yInfo("Special cmd %s, not processing ", speech.c_str());
185  }
186  else
187  {
188 
189  //string port = "http://start.csail.mit.edu:80/askstart.cgi?server=guest&action=parse&te=formatted-text&query=";
190  std::string port = "http://start.csail.mit.edu:80/api.php?server=guest&action=parse&te=formatted-text&query=";
191 
192  std::string query = port + speech;
193 
194  portURL.addOutput(query.c_str());
195  yarp::os::Bottle cmd, reply;
196  cmd.addString("1"); // send any message you like, it is ignored
197  portURL.write(cmd,reply);
198 
199  if (reply.size()>0){
200 
201  std::string tmp = reply.toString().c_str();
202 
203  std::string answer;
204  try {
205  answer = tmp.substr(tmp.find_first_of("["), tmp.find_last_of("]"));
206  }
207  catch (const std::out_of_range& oor) {
208  yError("START cannot process the command %s", tmp.c_str());
209  }
210 
211  //find how many occurences of "\n"
212  size_t n = std::count(answer.begin(), answer.end(), ']');
213 
214  yInfo("with %lu occurences \n", n);
215 
216  yarp::os::Bottle &tmpList = final.addList();
217 
218  for (size_t i = 0; i<n; i++)
219  {
220  std::string various = answer.substr(answer.find_first_of("["), answer.find_first_of("]"));
221  various.erase(0,1);
222 
223  //yInfo("\n tmp string is:\n %s \n", various.c_str());
224 
225  std::stringstream str(various);
226  std::string words;
227 
228  yarp::os::Bottle &finalTmp = tmpList.addList();
229 
230  for (int i = 0; str >> words; i++)
231  {
232  if (words.size() > 1 )
233  words.erase( remove_if(words.begin(), words.end(), aZCheck), words.end());
234 
235  if (isalpha(*words.c_str()))
236  finalTmp.addString( words.c_str() );
237  else
238  {
239  std::string::size_type sz; // alias of size_t
240  int i_dec = std::stoi (words,&sz);
241  finalTmp.addInt32(i_dec);
242  }
243  }
244  answer.erase(answer.find_first_of("["), answer.find_first_of("]"));
245 
246  answer.erase(0, answer.find_first_of("["));
247  }
248  }
249  else{
250  yError("Something is wrong with the reply from START");
251  }
252 
253  yInfo(" LIST is:\n %s \n", final.toString().c_str());
254  }
255 
256  startOutPort.write();
257  }else
258 
259  {
260  yError("Something is wrong with the query");
261  }
262 }
263 //empty line to make gcc happy