18 #include "start-ask.h"
19 #include <yarp/os/Log.h>
20 #include <yarp/os/LogStream.h>
24 bool STARTModule::configure(yarp::os::ResourceFinder &rf)
26 moduleName = rf.check(
"name", yarp::os::Value(
"start-ask"),
"module name (string)").asString();
28 setName(moduleName.c_str());
30 handlerPortName =
"/";
31 handlerPortName += getName();
32 handlerPortName +=
"/rpc:i";
34 if (!rpcPort.open(handlerPortName.c_str()))
36 yError(
"%s : Unable to open port %s\n", getName().c_str(), handlerPortName.c_str());
43 startManager =
new STARTManager( moduleName );
52 bool STARTModule::interruptModule()
59 bool STARTModule::close()
62 yInfo(
"starting the shutdown procedure\n");
63 startManager->interrupt();
64 startManager->close();
65 yInfo(
"deleting thread\n");
67 yInfo(
"done deleting thread\n");
72 bool STARTModule::updateModule()
78 double STARTModule::getPeriod()
84 STARTManager::~STARTManager()
90 STARTManager::STARTManager(
const std::string &moduleName )
92 yInfo(
"initialising Variables\n");
93 this->moduleName = moduleName;
97 bool STARTManager::open()
102 inSpeechPortName =
"/" + moduleName +
"/speech:i";
103 BufferedPort<yarp::os::Bottle >::open( inSpeechPortName.c_str() );
105 outSTARTUrlName =
"/" + moduleName +
"/url";
106 portURL.openFake(outSTARTUrlName);
108 outSTARTPortName =
"/" + moduleName +
"/start:o";
109 startOutPort.open( outSTARTPortName.c_str() );
111 inFacePortName =
"/" + moduleName +
"/faces:i";
112 faceInPort.open( inFacePortName.c_str() );
118 void STARTManager::close()
120 yInfo(
"now closing ports...\n");
121 startOutPort.close();
124 yarp::os::BufferedPort<yarp::os::Bottle >::close();
125 yInfo(
"finished closing the read port...\n");
129 void STARTManager::interrupt()
131 yInfo(
"cleaning up...\n");
132 yInfo(
"attempting to interrupt ports\n");
133 yarp::os::BufferedPort<yarp::os::Bottle >::interrupt();
135 faceInPort.interrupt();
136 yInfo(
"finished interrupt ports\n");
140 void STARTManager::onRead(yarp::os::Bottle &bot)
142 yarp::os::Bottle &
final = startOutPort.prepare();
147 std::string question = bot.toString();
149 if (isalpha(question[0]))
150 yInfo(
"avoiding erase\n");
152 question.erase(std::remove_if(question.begin(), question.end(), aZCheck), question.end());
154 yInfo(
"cmd is %s", question.c_str());
156 yarp::os::Bottle *faceList = faceInPort.read(
false);
161 yarp::os::Bottle *item=faceList->get(0).asList();
162 name = item->get(4).asString().c_str();
163 yInfo() <<
"NAME IS " << name;
166 std::stringstream s(question);
169 std::string speech =
"";
171 bool greeting =
false;
173 if (strcmp (question.c_str(),
"how are you") == 0 || strcmp (question.c_str(),
"how are you robot") == 0 || strcmp (question.c_str(),
"how are you doing") == 0)
177 for (
int i = 0; s >> word; i++){
178 speech += word.c_str();
180 if (strcmp (speech.c_str(),
"hello" ) == 0 || strcmp (speech.c_str(),
"hi" ) == 0)
187 speech.erase(speech.size()-1,1);
190 std::string port =
"http://start.csail.mit.edu:80/justanswer.php?te=formatted-text&query=";
192 std::string query = port + speech;
194 portURL.addOutput(query.c_str());
195 yarp::os::Bottle cmd, reply;
197 portURL.write(cmd,reply);
200 std::vector<std::string> testtags;
201 std::vector<std::string> testtext;
203 yInfo(
"query is: %s\n", query.c_str());
205 yInfo() <<
"\n--------------------------------Start cleaning up session-------------------------------\n";
209 html = reply.toString().c_str();
212 std::string tags =
"<P><p>";
213 std::string::size_type initial = html.find(tags);
214 if (initial!=std::string::npos)
216 yInfo() <<
"Response found "<< tags <<
" @ " << initial <<
" with length " << tags.length();
217 html.erase (0,initial+tags.length());
223 std::string::size_type initial = html.find(tags);
224 if (initial!=std::string::npos)
226 yInfo() <<
"Response found " << tags <<
" @ " << initial <<
" with length " << tags.length();
227 html.erase (0,initial+tags.length());
233 std::string::size_type initial = html.find(tags);
234 if (initial!=std::string::npos)
236 yInfo() <<
"Response found " << tags <<
" @ " << initial <<
" with length " << tags.length();
237 html.erase (0,initial+tags.length());
240 yError() <<
"Cannot seem to find any info in the initial part of the response";
247 std::string::size_type
final = html.find(tags);
248 if (
final!=std::string::npos)
250 yInfo() <<
"Complex response found "<< tags <<
" @ " <<
final <<
" with length " << tags.length();
251 html.erase (html.begin()+
final, html.end());
257 std::string::size_type
final = html.find(tags);
258 if (
final!=std::string::npos)
260 yInfo() <<
"Normal response found "<< tags <<
" @ " <<
final <<
" with length " << tags.length();
261 html.erase (html.begin()+
final, html.end());
267 std::string::size_type
final = html.find(tags);
268 if (
final!=std::string::npos)
270 yInfo() <<
"Response found " << tags <<
" @ " <<
final <<
" with length " << tags.length();
271 html.erase (html.begin()+
final, html.end());
274 yError() <<
"Cannot seem to find any info in the final part of the response";
280 std::string::size_type n;
282 while ( (n = html.find(tags)) != html.npos)
284 yInfo() <<
"Cleaning up " << tags <<
" @ " << n;
285 html.replace(n,tags.length(),
" ");
292 std::string::size_type iknow = html.find(tags);
293 if (iknow!=std::string::npos)
295 yInfo() <<
"Response found " << tags <<
" @ " << iknow <<
" with length " << tags.length();
296 html.erase (html.begin()+iknow, html.end());
300 std::string::size_type numericEntry;
302 while ( (numericEntry = html.find(tags)) != html.npos)
304 yInfo() <<
"Cleaning up " << tags <<
" @ " << numericEntry;
305 html.replace(numericEntry,tags.length(),
" ");
309 std::string::size_type enDash;
311 while ( (enDash = html.find(tags)) != html.npos)
313 yInfo() <<
"Cleaning up " << tags <<
" @ " << enDash;
314 html.replace(enDash,tags.length(),
",");
318 std::string::size_type enDash2;
320 while ( (enDash2 = html.find(tags)) != html.npos)
322 yInfo() <<
"Cleaning up " << tags <<
" @ " << enDash2;
323 html.replace(enDash2,tags.length(),
",");
330 std::string::size_type nonSpace;
332 while ( (nonSpace = html.find(tags)) != html.npos)
334 yInfo() <<
"Cleaning up " << tags <<
" @ " << nonSpace;
335 html.replace(nonSpace,tags.length(),
",");
339 std::string::size_type quote;
341 while ( (quote = html.find(tags)) != html.npos)
343 yInfo() <<
"Cleaning up " << tags <<
" @ " << quote;
344 html.replace(quote,tags.length(),
" ");
348 std::string::size_type fslash;
350 while ( (fslash = html.find(tags)) != html.npos)
352 yInfo() <<
"Cleaning up " << tags <<
" @ " << fslash;
353 html.replace(fslash,tags.length(),
" ");
357 std::string::size_type bslash;
359 while ( (bslash = html.find(tags)) != html.npos)
361 yInfo() <<
"Cleaning up " << tags <<
" @ " << bslash;
362 html.replace(bslash,tags.length(),
" ");
366 std::string::size_type dash;
368 while ( (dash = html.find(tags)) != html.npos)
370 yInfo() <<
"Cleaning up " << tags <<
" @ " << dash;
371 html.replace(dash,tags.length(),
"");
375 std::string::size_type amp;
377 while ( (amp = html.find(tags)) != html.npos)
379 yInfo() <<
"Cleaning up " << tags <<
" @ " << amp;
380 html.replace(amp,tags.length(),
"&");
383 yInfo() <<
"---------------------------Done the first cleaning up session---------------------------";
385 for(
int i=0; i<html.length(); i++)
387 std::string::size_type startpos;
389 startpos = html.find(
'<');
390 if(startpos == std::string::npos)
393 testtext.push_back(html);
400 testtext.push_back(html.substr(0, startpos));
401 html = html.substr(startpos, html.size() - startpos);
406 std::string::size_type endpos;
407 for(endpos = startpos; endpos < html.size() && html[endpos] !=
'>'; ++endpos)
411 if(html[endpos] ==
'"')
414 while(endpos < html.size() && html[endpos] !=
'"')
420 if(endpos == html.size())
422 html = html.substr(endpos, html.size() - endpos);
429 testtags.push_back(html.substr(startpos, endpos - startpos));
430 html = html.substr(endpos, html.size() - endpos);
439 for(
size_t i = 0; i < testtext.size(); i++)
440 html = html + testtext[i];
442 yInfo() <<
"\n-------------------------------Starting specific cleaning-------------------------------";
443 yInfo() <<
"--------------------------------- check for [ ] removal ----------------------------------";
445 std::string first =
"[";
446 std::string second =
"]";
448 removetags(first, second, html);
450 yInfo() <<
"--------------------------------- check for ( ) removal ----------------------------------";
456 removetags(first, second, html);
459 yError(
"Something is wrong with the reply from START");
460 if (greeting && name.size()>0)
461 html = name +
", " + html;
463 final.addString(html.c_str());
465 yInfo() <<
"The original answer was:\n " << reply.toString().c_str();
467 yInfo() <<
"The answer is:\n " <<
final.toString().c_str();
468 startOutPort.write();
470 yError(
"Something is wrong with the query");
474 void STARTManager::removetags(std::string &first, std::string &second, std::string &text)
476 std::string tags = first;
478 std::vector<size_t> startPos;
479 std::vector<size_t> endPos;
481 size_t pos = text.find(tags);
482 while(pos != std::string::npos)
484 yInfo() <<
"found " << tags <<
" @ " << pos;
485 startPos.push_back(pos);
486 pos = text.find(tags, pos+1);
492 pos = text.find(tags);
493 while(pos != std::string::npos)
495 yInfo() <<
"found " << tags <<
" @ " << pos;
496 endPos.push_back(pos);
497 pos = text.find(tags, pos+1);
502 std::vector<size_t> tmpstartPos = startPos;
503 std::vector<size_t> tmpendPos = endPos;
505 if (tmpstartPos.size() == tmpendPos.size())
507 for (
int i =0; i<tmpstartPos.size(); i++)
511 if ( i != tmpstartPos.size()-1 && tmpendPos[i] > tmpstartPos[i+1] )
513 startPos.erase(startPos.begin()+1);
514 endPos.erase(endPos.begin());
518 for (
int i =0; i<startPos.size(); i++)
521 shift = shift + (endPos[i-1] - startPos[i-1] + 1 );
523 yInfo() <<
"deleting pos " << startPos[i] <<
" until " << endPos[i] <<
" with shift " << shift;
525 text.erase (text.begin()+(startPos[i]-shift), text.begin()+(endPos[i] + 1 - shift));