18 #include "start-speech.h"
19 #include <yarp/os/Log.h>
20 #include <yarp/os/LogStream.h>
23 bool STARTModule::configure(yarp::os::ResourceFinder &rf)
25 moduleName = rf.check(
"name", yarp::os::Value(
"start-speech"),
"module name (string)").asString();
27 setName(moduleName.c_str());
29 handlerPortName =
"/";
30 handlerPortName += getName();
31 handlerPortName +=
"/rpc:i";
33 if (!rpcPort.open(handlerPortName.c_str()))
35 yError(
"%s : Unable to open port %s\n", getName().c_str(), handlerPortName.c_str());
42 startManager =
new STARTManager( moduleName );
51 bool STARTModule::interruptModule()
58 bool STARTModule::close()
61 yInfo(
"starting the shutdown procedure\n");
62 startManager->interrupt();
63 startManager->close();
64 yInfo(
"deleting thread\n");
66 yInfo(
"done deleting thread\n");
71 bool STARTModule::updateModule()
77 double STARTModule::getPeriod()
83 STARTManager::~STARTManager()
89 STARTManager::STARTManager(
const std::string &moduleName )
91 yInfo(
"initialising Variables\n");
92 this->moduleName = moduleName;
96 bool STARTManager::open()
101 inSpeechPortName =
"/" + moduleName +
"/speech:i";
102 yarp::os::BufferedPort<yarp::os::Bottle >::open( inSpeechPortName.c_str() );
104 outSTARTUrlName =
"/" + moduleName +
"/url";
105 portURL.openFake(outSTARTUrlName);
107 outSTARTPortName =
"/" + moduleName +
"/start:o";
108 startOutPort.open( outSTARTPortName.c_str() );
110 yarp::os::Network::connect(
"/yarpIOS/speechPort:o",
"/speechSTART/speech:i");
116 void STARTManager::close()
118 yInfo(
"now closing ports...\n");
119 startOutPort.close();
121 yarp::os::BufferedPort<yarp::os::Bottle >::close();
122 yInfo(
"finished closing the read port...\n");
126 void STARTManager::interrupt()
128 yInfo(
"cleaning up...\n");
129 yInfo(
"attempting to interrupt ports\n");
130 yarp::os::BufferedPort<yarp::os::Bottle >::interrupt();
132 yInfo(
"finished interrupt ports\n");
136 void STARTManager::onRead(yarp::os::Bottle &bot)
138 yarp::os::Bottle &
final = startOutPort.prepare();
144 std::string question = bot.toString();
146 if (isalpha(question[0]))
147 yInfo(
"avoiding erase\n");
150 question.erase( remove_if(question.begin(), question.end(), aZCheck), question.end());
153 yInfo(
"cmd is %s", question.c_str());
155 yarp::os::Bottle &request =
final.addList();
157 std::stringstream s(question);
160 std::string speech =
"";
165 for (
int i = 0; s >> word; i++){
167 if (i==0 && strcmp (word.c_str(),
"no")==0)
169 else if (i==0 && strcmp (word.c_str(),
"yes")==0)
171 else if (i==0 && strcmp (word.c_str(),
"Skip")==0)
174 speech += word.c_str();
175 request.addString(word);
180 speech.erase(speech.size()-1,1);
184 yInfo(
"Special cmd %s, not processing ", speech.c_str());
190 std::string port =
"http://start.csail.mit.edu:80/api.php?server=guest&action=parse&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);
201 std::string tmp = reply.toString().c_str();
205 answer = tmp.substr(tmp.find_first_of(
"["), tmp.find_last_of(
"]"));
207 catch (
const std::out_of_range& oor) {
208 yError(
"START cannot process the command %s", tmp.c_str());
212 size_t n = std::count(answer.begin(), answer.end(),
']');
214 yInfo(
"with %lu occurences \n", n);
216 yarp::os::Bottle &tmpList =
final.addList();
218 for (
size_t i = 0; i<n; i++)
220 std::string various = answer.substr(answer.find_first_of(
"["), answer.find_first_of(
"]"));
225 std::stringstream str(various);
228 yarp::os::Bottle &finalTmp = tmpList.addList();
230 for (
int i = 0; str >> words; i++)
232 if (words.size() > 1 )
233 words.erase( remove_if(words.begin(), words.end(), aZCheck), words.end());
235 if (isalpha(*words.c_str()))
236 finalTmp.addString( words.c_str() );
239 std::string::size_type sz;
240 int i_dec = std::stoi (words,&sz);
241 finalTmp.addInt32(i_dec);
244 answer.erase(answer.find_first_of(
"["), answer.find_first_of(
"]"));
246 answer.erase(0, answer.find_first_of(
"["));
250 yError(
"Something is wrong with the reply from START");
253 yInfo(
" LIST is:\n %s \n",
final.toString().c_str());
256 startOutPort.write();
260 yError(
"Something is wrong with the query");