speech
All Data Structures Functions Modules Pages
main.cpp
1 
2 
3 /*
4  * Copyright (C) 2018 iCub Facility - Istituto Italiano di Tecnologia
5  * Author: Vadim Tikhanoff Laura Cavaliere
6  * email: vadim.tikhanoff@iit.it laura.cavaliere@iit.it
7  * Permission is granted to copy, distribute, and/or modify this program
8  * under the terms of the GNU General Public License, version 2 or any
9  * later version published by the Free Software Foundation.
10  *
11  * A copy of the license can be found at
12  * http://www.robotcub.org/icub/license/gpl.txt
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details
18  */
19 
20 #include <vector>
21 #include <iostream>
22 #include <deque>
23 #include <cstdio>
24 #include <cmath>
25 
26 #include <fstream>
27 #include <iterator>
28 #include <string>
29 #include <map>
30 
31 #include <yarp/os/BufferedPort.h>
32 #include <yarp/os/ResourceFinder.h>
33 #include <yarp/os/RFModule.h>
34 #include <yarp/os/Network.h>
35 #include <yarp/os/Time.h>
36 #include <yarp/os/Log.h>
37 #include <yarp/os/LogStream.h>
38 #include <yarp/os/Semaphore.h>
39 #include <yarp/sig/SoundFile.h>
40 #include <yarp/dev/PolyDriver.h>
41 
42 #include <grpc++/grpc++.h>
43 #include "google/cloud/language/v1/language_service.grpc.pb.h"
44 
45 #include "googleSpeechProcess_IDL.h"
46 
47 using namespace google::cloud::language::v1;
48 bool is_changed;
49 
50 static const std::map<grpc::StatusCode, std::string> status_code_to_string {
51  {grpc::OK, "ok"},
52  {grpc::CANCELLED, "cancelled"},
53  {grpc::UNKNOWN, "unknown"},
54  {grpc::INVALID_ARGUMENT, "invalid_argument"},
55  {grpc::DEADLINE_EXCEEDED, "deadline_exceeded"},
56  {grpc::NOT_FOUND, "not_found"},
57  {grpc::ALREADY_EXISTS, "already_exists"},
58  {grpc::PERMISSION_DENIED, "permission_denied"},
59  {grpc::UNAUTHENTICATED, "unauthenticated"},
60  {grpc::RESOURCE_EXHAUSTED , "resource_exhausted"},
61  {grpc::FAILED_PRECONDITION, "failed_precondition"},
62  {grpc::ABORTED, "aborted"},
63  {grpc::OUT_OF_RANGE, "out_of_range"},
64  {grpc::UNIMPLEMENTED, "unimplemented"},
65  {grpc::INTERNAL, "internal"},
66  {grpc::UNAVAILABLE, "unavailable"},
67  {grpc::DATA_LOSS, "data_loss"},
68  {grpc::DO_NOT_USE, "do_not_use"}
69 };
70 /********************************************************/
71 class Processing : public yarp::os::BufferedPort<yarp::os::Bottle>
72 {
73  std::string moduleName;
74  std::string &state;
75  yarp::os::RpcServer handlerPort;
76  yarp::os::BufferedPort<yarp::os::Bottle> targetPort;
77 
78  yarp::os::Bottle wordList;
79 
80 public:
81  /********************************************************/
82 
83  Processing( const std::string &moduleName, std::string &state ): state(state)
84  {
85  this->moduleName = moduleName;
86  }
87 
88  /********************************************************/
89  ~Processing()
90  {
91 
92  };
93 
94  /********************************************************/
95  bool open()
96  {
97  this->useCallback();
98  yarp::os::BufferedPort<yarp::os::Bottle >::open( "/" + moduleName + "/text:i" );
99  targetPort.open("/"+ moduleName + "/result:o");
100 
101  //yarp::os::Network::connect("/googleSpeech/result:o", yarp::os::BufferedPort<yarp::os::Bottle >::getName().c_str());
102 
103  return true;
104  }
105 
106  /********************************************************/
107  void close()
108  {
109  yarp::os::BufferedPort<yarp::os::Bottle >::close();
110  targetPort.close();
111  }
112 
113  /********************************************************/
114  void onRead( yarp::os::Bottle &bot )
115  {
116  yarp::os::Bottle &outTargets = targetPort.prepare();
117 
118  wordList.clear();
119  outTargets.clear();
120  outTargets = queryGoogleSyntax(bot);
121 
122  targetPort.write();
123  yDebug() << "done querying google";
124 
125  }
126 
127  /********************************************************/
128  yarp::os::Bottle queryGoogleSyntax(yarp::os::Bottle& text)
129  {
130  yarp::os::Bottle b;
131 
132  yDebug() << "in queryGoogleSyntax";
133 
134  yDebug() << "Phrase is " << text.toString().c_str();
135 
136  std::string tmp = text.toString();
137 
138  std::map< std::string, std::string> dictionary;
139  dictionary.insert ( std::pair<std::string,std::string>("á","a") );
140  dictionary.insert ( std::pair<std::string,std::string>("à","a") );
141  dictionary.insert ( std::pair<std::string,std::string>("é","e") );
142  dictionary.insert ( std::pair<std::string,std::string>("è","e") );
143  dictionary.insert ( std::pair<std::string,std::string>("í","i") );
144  dictionary.insert ( std::pair<std::string,std::string>("ó","o") );
145  dictionary.insert ( std::pair<std::string,std::string>("ú","u") );
146  dictionary.insert ( std::pair<std::string,std::string>("ñ","n") );
147 
148  std::string tmp2 = tmp;
149  std::string strAux;
150  for (auto it= dictionary.begin(); it != dictionary.end(); it++)
151  {
152  tmp2=(it->first);
153  std::size_t found=tmp.find_first_of(tmp2);
154 
155  while (found!=std::string::npos)
156  {
157  yError() << "in found" << found;
158  strAux=(it->second);
159  tmp.erase(found,tmp2.length());
160  tmp.insert(found,strAux);
161  found=tmp.find_first_of(tmp2,found+1);
162  }
163  }
164 
165  yDebug() << "Phrase is now " << tmp.c_str();
166  tmp.erase(std::remove(tmp.begin(),tmp.end(),'\"'),tmp.end());
167 
168  yDebug() << tmp.size();
169  yDebug() << std::isalnum(tmp[1]);
170 
171  if (tmp.size() > 1 && std::isalnum(tmp[0])==0)
172  tmp = tmp.substr(1, tmp.size() - 2);
173 
174  yDebug() << "Phrase is now " << tmp.c_str();
175 
176  std::string content = tmp;
177 
178  if (content.size()>0){
179  AnalyzeSyntaxRequest request;
180  AnalyzeSyntaxResponse response;
181  grpc::Status status;
182  grpc::ClientContext context;
183 
184  AnalyzeSentimentRequest sentiment_request;
185  AnalyzeSentimentResponse sentiment_response;
186  grpc::Status sentiment_status;
187  grpc::ClientContext sentiment_context;
188 
189  setArguments( request.mutable_document(), content );
190  setArguments( sentiment_request.mutable_document(), content );
191 
192  // EncodingType //
193  request.set_encoding_type( EncodingType::UTF8 );
194 
195  b.clear();
196  auto creds = grpc::GoogleDefaultCredentials();
197  auto channel = grpc::CreateChannel("language.googleapis.com", creds);
198  std::unique_ptr< LanguageService::Stub> stub( LanguageService::NewStub( channel ) );
199 
200  checkState("Busy");
201  yarp::os::Time::delay(0.2);
202  status = stub->AnalyzeSyntax( &context, request, &response );
203  sentiment_status = stub->AnalyzeSentiment( &sentiment_context, sentiment_request, &sentiment_response );
204  std::string status_string = status_code_to_string.at(status.error_code());
205  yInfo() << "Status string:" << status_string;
206  checkState("Done");
207 
208  if ( status.ok() && sentiment_status.ok() ){
209  yInfo() << "Status returned OK";
210  yInfo() << "\n------Response------\n";
211 
212  read_language( &response.language() );
213  read_sentences( &response.sentences() );
214  read_sentiment( &sentiment_response.sentences() );
215  read_tokens( &response.tokens() );
216  b = read_analysis( &response.sentences(), &response.tokens(), &sentiment_response.sentences() );
217 
218  }
219  else {
220  yError() << "Status Returned Cancelled";
221  checkState("Failure_" + status_string);
222  yInfo() << status.error_message();
223  }
224  }
225  else if (content.size()==0) {
226  checkState("Empty_input");
227  }
228  return b;
229  }
230 
231  /********************************************************/
232  void setArguments(Document* doc, std::string& content)
233  {
234  doc->set_type( Document_Type::Document_Type_PLAIN_TEXT );
235  doc->set_content( content );
236  }
237 
238  /********************************************************/
239  void read_language( const std::string* lang )
240  {
241  std::cout << "\n----Language----" << std::endl;
242  std::cout << "Language: " << *lang << std::endl;
243  }
244 
245  /********************************************************/
246  void read_sentences( const google::protobuf::RepeatedPtrField< Sentence >* sentences ) {
247 
248  // SENTENCES //
249  yInfo() << "----Sentences----";
250  yInfo() << "Sentences Size: " << sentences->size();
251  for( int i = 0; i < sentences->size(); i++ ) {
252  yInfo() << "Sentence " << i << " has text: " << sentences->Get( i ).has_text();
253  if ( sentences->Get( i ).has_text() ) {
254  yInfo() << "Sentence text: " << sentences->Get( i ).text().content();
255  }
256  }
257  }
258 
259  /********************************************************/
260  std::pair<float,float> read_sentiment( const google::protobuf::RepeatedPtrField< Sentence >* sentences ) {
261 
262 
263  std::pair<float,float> result(9999,9999);
264 
265  // SENTIMENTS //
266  yInfo() << "----Sentiments----";
267  yInfo() << "Sentiments Size: " << sentences->size();
268  for( int i = 0; i < sentences->size(); i++ ) {
269  yInfo() << "Sentiments " << i << " has text: " << sentences->Get( i ).has_text();
270  if ( sentences->Get( i ).has_text() ) {
271  yInfo() << "Sentiments text: " << sentences->Get( i ).text().content();
272  }
273 
274  yInfo() << "Sentiments " << i << " has sentiment: " << sentences->Get( i ).has_sentiment();
275  if ( sentences->Get( i ).has_sentiment() ) {
276  yInfo() << "Sentiments " << i << " sentiment: "
277  << "\n\t\tMagnitude: "
278  << sentences->Get( i ).sentiment().magnitude() // float
279  << "\n\t\tScore: "
280  << sentences->Get( i ).sentiment().score() // float
281  << " ";
282  result.first = sentences->Get( i ).sentiment().magnitude();
283  result.second = sentences->Get( i ).sentiment().score();
284  }
285  }
286  return result;
287  }
288 
289  /********************************************************/
290  yarp::os::Bottle read_analysis( const google::protobuf::RepeatedPtrField< Sentence >* sentences , const google::protobuf::RepeatedPtrField< Token >* tokens, const google::protobuf::RepeatedPtrField< Sentence >* sentiment_sentences ) {
291 
292 
293  yarp::os::Bottle &words = wordList.addList();
294 
295  for( int i = 0; i < sentences->size(); i++ ) {
296  yInfo() << "Sentence " << i << " has text: " << sentences->Get( i ).has_text();
297  if ( sentences->Get( i ).has_text() ) {
298  yInfo() << "Sentence text: " << sentences->Get( i ).text().content();
299  words.addString(sentences->Get( i ).text().content());
300  }
301  }
302 
303  for ( int i = 0; i < tokens->size(); i++ ) {
304 
305  yarp::os::Bottle &word_analysis = words.addList();
306  word_analysis.addString("word");
307  word_analysis.addString(tokens->Get( i ).text().content());
308 
309  int j = tokens->Get( i ).dependency_edge().head_token_index();
310  if ( tokens->Get( j ).text().content() != tokens->Get( i ).text().content() ) {
311  yarp::os::Bottle &dependency = word_analysis.addList();
312  dependency.addString("Dependency");
313  dependency.addString(tokens->Get( j ).text().content());
314  }
315 
316  yarp::os::Bottle &parseLabel = word_analysis.addList();
317  parseLabel.addString("Parse_Label");
318  parseLabel.addString(DependencyEdge_Label_Name(tokens->Get( i ).dependency_edge().label()));
319 
320  yarp::os::Bottle &partOfSpeech = word_analysis.addList();
321  partOfSpeech.addString("Part_of_speech");
322  partOfSpeech.addString(PartOfSpeech_Tag_Name(tokens->Get( i ).part_of_speech().tag()));
323 
324  if ( tokens->Get( i ).lemma() != tokens->Get( i ).text().content() ) {
325  yarp::os::Bottle &lemma = word_analysis.addList();
326  lemma.addString("Lemma");
327  lemma.addString(tokens->Get( i ).lemma());
328  }
329 
330  yarp::os::Bottle &morphology = word_analysis.addList();
331  morphology.addString("Morphology");
332 
333  yarp::os::Bottle &number = morphology.addList();
334  number.addString("number");
335  number.addString(PartOfSpeech_Number_Name( tokens->Get( i ).part_of_speech().number() ));
336 
337  yarp::os::Bottle &person = morphology.addList();
338  person.addString("person");
339  person.addString( PartOfSpeech_Person_Name( tokens->Get( i ).part_of_speech().person() ));
340 
341  yarp::os::Bottle &gender = morphology.addList();
342  gender.addString("gender");
343  gender.addString( PartOfSpeech_Gender_Name( tokens->Get( i ).part_of_speech().gender() ));
344 
345  /*yarp::os::Bottle &instance = morphology.addList();
346  instance.addString("instance");
347  instance.addString( PartOfSpeech_Case_Name( tokens->Get( i ).part_of_speech().instance() ));*/
348 
349  yarp::os::Bottle &tense = morphology.addList();
350  tense.addString("tense");
351  tense.addString(PartOfSpeech_Tense_Name( tokens->Get( i ).part_of_speech().tense() ));
352 
353  yarp::os::Bottle &aspect = morphology.addList();
354  aspect.addString("aspect");
355  aspect.addString( PartOfSpeech_Aspect_Name( tokens->Get( i ).part_of_speech().aspect() ));
356 
357  yarp::os::Bottle &mood = morphology.addList();
358  mood.addString("mood");
359  mood.addString(PartOfSpeech_Mood_Name( tokens->Get( i ).part_of_speech().mood() ));
360 
361  yarp::os::Bottle &voice = morphology.addList();
362  voice.addString("voice");
363  voice.addString(PartOfSpeech_Voice_Name( tokens->Get( i ).part_of_speech().voice() ));
364 
365  yarp::os::Bottle &reciprocity = morphology.addList();
366  reciprocity.addString("reciprocity");
367  reciprocity.addString(PartOfSpeech_Reciprocity_Name( tokens->Get( i ).part_of_speech().reciprocity() ));
368 
369  yarp::os::Bottle &proper = morphology.addList();
370  proper.addString("proper");
371  proper.addString(PartOfSpeech_Proper_Name( tokens->Get( i ).part_of_speech().proper() ));
372 
373  yarp::os::Bottle &form = morphology.addList();
374  form.addString("form");
375  form.addString( PartOfSpeech_Form_Name( tokens->Get( i ).part_of_speech().form() ));
376  }
377 
378 
379  yarp::os::Bottle &sentiment = words.addList();
380  sentiment.addString("Sentiment");
381 
382  yarp::os::Bottle &sentiment_score = sentiment.addList();
383  sentiment_score.addString("Score");
384  std::pair<float,float> sentiment_result = read_sentiment( sentiment_sentences );
385  sentiment_score.addFloat64(sentiment_result.second);
386 
387  yarp::os::Bottle &sentiment_magnitude = sentiment.addList();
388  sentiment_magnitude.addString("Magnitude");
389  sentiment_magnitude.addFloat64(sentiment_result.first);
390 
391  yInfo() << "wordList " << wordList.toString().c_str();
392 
393  return wordList;
394  }
395 
396  /********************************************************/
397  void read_tokens( const google::protobuf::RepeatedPtrField< Token >* tokens ) {
398 
399  for ( int i = 0; i < tokens->size(); i++ ) {
400 
401  std::cout << "\n-------- " << i << std::endl;
402  yInfo() << tokens->Get( i ).text().content();
403  yInfo() << "root" << tokens->Get( i ).dependency_edge().head_token_index();
404  yInfo() << PartOfSpeech_Tag_Name(tokens->Get( i ).part_of_speech().tag());
405  yInfo() << DependencyEdge_Label_Name( tokens->Get( i ).dependency_edge().label() );
406  if ( tokens->Get( i ).lemma() == tokens->Get( i ).text().content() ) {
407  yInfo() << "lemma na";
408  }
409  else {
410  yInfo() << "lemma" << tokens->Get( i ).lemma();
411  }
412  }
413 
414  for ( int i = 0; i < tokens->size(); i++ ) {
415 
416  std::cout
417  << "-- Token " << i << " --"
418  << std::endl;
419 
420  std::cout
421  << "Token " << i << " has text: "
422  << tokens->Get( i ).has_text()
423  << std::endl;
424 
425  if ( tokens->Get( i ).has_text() ) {
426  std::cout
427  << "\tToken " << i << " text: "
428  << tokens->Get( i ).text().content() // string
429  << "\n\tScore: "
430  << tokens->Get( i ).text().begin_offset() // int32
431  << std::endl;
432  }
433 
434  std::cout
435  << "Token " << i << " has PartOfSpeech: "
436  << tokens->Get( i ).has_part_of_speech()
437  << std::endl;
438 
439  if ( tokens->Get( i ).has_part_of_speech() ) {
440  std::cout
441  << "\n\t\tTag: "
442  << PartOfSpeech_Tag_Name( tokens->Get( i ).part_of_speech().tag() )
443  //<< "\n\tToken " << i << " PartOfSpeech: "
444  //<< "\n\t\tAspect: "
445  //<< PartOfSpeech_Aspect_Name( tokens->Get( i ).part_of_speech().aspect() )
446  //<< "\n\t\tCase: "
447  //<< PartOfSpeech_Case_Name( tokens->Get( i ).part_of_speech().instance() )
448  //<< "\n\t\tForm: "
449  //<< PartOfSpeech_Form_Name( tokens->Get( i ).part_of_speech().form() )
450  //<< "\n\t\tGender: "
451  //<< PartOfSpeech_Gender_Name( tokens->Get( i ).part_of_speech().gender() )
452  //<< "\n\t\tMood: "
453  //<< PartOfSpeech_Mood_Name( tokens->Get( i ).part_of_speech().mood() )
454  //<< "\n\t\tNumber: "
455  //<< PartOfSpeech_Number_Name( tokens->Get( i ).part_of_speech().number() )
456  //<< "\n\t\tPerson: "
457  //<< PartOfSpeech_Person_Name( tokens->Get( i ).part_of_speech().person() )
458  //<< "\n\t\tProper: "
459  //<< PartOfSpeech_Proper_Name( tokens->Get( i ).part_of_speech().proper() )
460  //<< "\n\t\tReciprocity: "
461  //<< PartOfSpeech_Reciprocity_Name( tokens->Get( i ).part_of_speech().reciprocity() )
462  //<< "\n\t\tTag: "
463  //<< PartOfSpeech_Tag_Name( tokens->Get( i ).part_of_speech().tag() )
464  //<< "\n\t\tTense: "
465  //<< PartOfSpeech_Tense_Name( tokens->Get( i ).part_of_speech().tense() )
466  //<< "\n\t\tVoice: "
467  //<< PartOfSpeech_Voice_Name( tokens->Get( i ).part_of_speech().voice() )
468 
469  << std::endl;
470  }
471 
472  std::cout
473  << "Token " << i << " has DependencyEdge: "
474  << tokens->Get( i ).has_dependency_edge()
475  << std::endl;
476 
477  if ( tokens->Get( i ).has_dependency_edge() ) {
478  std::cout
479  << "\tToken " << i << " DependencyEdge: "
480  << "\n\t\tHead Token Index: "
481  << tokens->Get( i ).dependency_edge().head_token_index() // int32
482  << "\n\t\tLabel: "
483  << DependencyEdge_Label_Name( tokens->Get( i ).dependency_edge().label() )
484  << std::endl;
485  }
486  std::cout
487  << "Token " << i << " lemma: "
488  << tokens->Get( i ).lemma() // string
489  << std::endl
490  << std::endl;
491  }
492  }
493 
494  /********************************************************/
495  bool start_acquisition()
496  {
497  return true;
498  }
499 
500  /********************************************************/
501  bool stop_acquisition()
502  {
503  return true;
504  }
505 
506  /********************************************************/
507  yarp::os::Bottle get_dependency()
508  {
509  yarp::os::Bottle dependency_result;
510  yarp::os::Bottle &dependency_analysis = dependency_result.addList();
511  size_t element = wordList.get(0).asList()->size();
512  //yInfo() << "element " << element;
513  for(size_t i=1; i<element-1; i++){
514  yarp::os::Bottle* new_element = wordList.get(0).asList()->get(i).asList();
515  //printf("dependency check: %s\n", new_element->find("Dependency").asString().c_str());
516  yarp::os::Bottle &each_dependency = dependency_analysis.addList();
517  each_dependency.addString(new_element->find("word").asString()); //add the word
518  each_dependency.addString(new_element->find("Dependency").asString()); //add the corresponding dependency
519  }
520 
521 
522  yInfo() << "dependency_result " << dependency_result.toString().c_str();// ((This is) (is "n/a") (a test) (test is))
523 
524  return dependency_result;
525  }
526 
527  /********************************************************/
528  yarp::os::Bottle get_parseLabel()
529  {
530  yarp::os::Bottle parseLabel_result;
531  yarp::os::Bottle &parseLabel_analysis = parseLabel_result.addList();
532  size_t element = wordList.get(0).asList()->size();
533  for(size_t i=1; i<element-1; i++){
534  yarp::os::Bottle* new_element = wordList.get(0).asList()->get(i).asList();
535  yarp::os::Bottle &each_parseLabel = parseLabel_analysis.addList();
536  each_parseLabel.addString(new_element->find("word").asString()); //add the word
537  each_parseLabel.addString(new_element->find("Parse_Label").asString()); //add the corresponding parseLabel
538  }
539 
540 
541  yInfo() << "parseLabel_result " << parseLabel_result.toString().c_str();
542 
543  return parseLabel_result;
544  }
545 
546  /********************************************************/
547  yarp::os::Bottle get_partOfSpeech()
548  {
549  yarp::os::Bottle partOfSpeech_result;
550  yarp::os::Bottle &partOfSpeech_analysis = partOfSpeech_result.addList();
551  size_t element = wordList.get(0).asList()->size();
552  for(size_t i=1; i<element-1; i++){
553  yarp::os::Bottle* new_element = wordList.get(0).asList()->get(i).asList();
554  yarp::os::Bottle &each_partOfSpeech = partOfSpeech_analysis.addList();
555  each_partOfSpeech.addString(new_element->find("word").asString()); //add the word
556  each_partOfSpeech.addString(new_element->find("Part_of_speech").asString()); //add the corresponding partOfSpeech
557  }
558 
559 
560  yInfo() << "partOfSpeech_result " << partOfSpeech_result.toString().c_str();
561 
562  return partOfSpeech_result;
563  }
564 
565  /********************************************************/
566  yarp::os::Bottle get_lemma()
567  {
568  yarp::os::Bottle lemma_result;
569  yarp::os::Bottle &lemma_analysis = lemma_result.addList();
570  size_t element = wordList.get(0).asList()->size();
571  for(size_t i=1; i<element-1; i++){
572  yarp::os::Bottle* new_element = wordList.get(0).asList()->get(i).asList();
573  yarp::os::Bottle &each_lemma = lemma_analysis.addList();
574  each_lemma.addString(new_element->find("word").asString()); //add the word
575  each_lemma.addString(new_element->find("Lemma").asString()); //add the corresponding Lemma
576  }
577 
578 
579  yInfo() << "lemma_result " << lemma_result.toString().c_str();
580 
581  return lemma_result;
582  }
583 
584  /********************************************************/
585  yarp::os::Bottle get_morphology()
586  {
587  yarp::os::Bottle morphology_result;
588  yarp::os::Bottle &morphology_analysis = morphology_result.addList();
589  size_t element = wordList.get(0).asList()->size();
590  for(size_t i=1; i<element-1; i++){
591  yarp::os::Bottle* new_element = wordList.get(0).asList()->get(i).asList();//contains the word with all the speech-features
592  yarp::os::Bottle* morphology = new_element->get(new_element->size()-1).asList();
593  //yInfo() << "morphology " << morphology->toString();// Morphology (number NUMBER_UNKNOWN) (person PERSON_UNKNOWN) (gender GENDER_UNKNOWN) etc
594  yarp::os::Bottle &each_morphology = morphology_analysis.addList();
595  each_morphology.addString(new_element->find("word").asString()); //add the word
596  for( size_t i=1; i<morphology->size()-1; i++) {
597  yarp::os::Bottle &each_value = each_morphology.addList();
598  each_value.addString(morphology->get(i).asList()->get(0).toString());//add (number NUMBER_UNKNOWN) (person PERSON_UNKNOWN) etc
599  each_value.addString(morphology->get(i).asList()->get(1).toString());
600  }
601  }
602 
603 
604  yInfo() << "morphology_result " << morphology_result.toString().c_str();
605 
606  return morphology_result;
607  }
608 
609  /********************************************************/
610  yarp::os::Bottle get_sentiment()
611  {
612  yarp::os::Bottle sentiment_result;
613  yarp::os::Bottle &sentiment_analysis = sentiment_result.addList();
614  size_t element = wordList.get(0).asList()->size();
615  yarp::os::Bottle* first_element = wordList.get(0).asList();
616  std::string sentence = first_element->get(0).asString();
617  //yInfo() << "sentence" << sentence;
618  sentiment_analysis.addString(sentence); //add the sentence
619  yarp::os::Bottle* sentiment = wordList.get(0).asList()->get(element-1).asList();
620  //yInfo() << "sentiment " << sentiment->toString();//Sentiment (Score 0.100000001490116119385) (Magnitude 0.100000001490116119385)
621  double score = sentiment->get(1).asList()->get(1).asFloat64();
622  double magnitude = sentiment->get(2).asList()->get(1).asFloat64();
623  //printf("Values: %f %f\n", score, magnitude);
624  yarp::os::Bottle &each_score = sentiment_analysis.addList();
625  each_score.addString("Score"); //add the corresponding score
626  each_score.addFloat64(score);
627  yarp::os::Bottle &each_magnitude = sentiment_analysis.addList();
628  each_magnitude.addString("Magnitude"); //add the corresponding magnitude
629  each_magnitude.addFloat64(magnitude);
630 
631 
632 
633  yInfo() << "sentiment_result " << sentiment_result.toString().c_str();// ("You make me very angry" (Score -0.899999976158142089844) (Magnitude 0.899999976158142089844))
634 
635 
636  return sentiment_result;
637  }
638 
639  /********************************************************/
640  bool checkState(std::string new_state)
641  {
642  if(new_state!=state){
643  is_changed=true;
644  state=new_state;
645  }
646  else{
647  is_changed=false;
648  }
649  return is_changed;
650  }
651 };
652 
653 /********************************************************/
654 class Module : public yarp::os::RFModule, public googleSpeechProcess_IDL
655 {
656  yarp::os::ResourceFinder *rf;
657  yarp::os::RpcServer rpcPort;
658  std::string state;
659  yarp::os::BufferedPort<yarp::os::Bottle> statePort;
660 
661 
662  Processing *processing;
663  friend class processing;
664 
665  bool closing;
666 
667  /********************************************************/
668  bool attach(yarp::os::RpcServer &source)
669  {
670  return this->yarp().attachAsServer(source);
671  }
672 
673 public:
674 
675  /********************************************************/
676  bool configure(yarp::os::ResourceFinder &rf)
677  {
678  this->rf=&rf;
679  this->state="Ready";
680  std::string moduleName = rf.check("name", yarp::os::Value("yarp-google-speech-process"), "module name (string)").asString();
681 
682  setName(moduleName.c_str());
683 
684  rpcPort.open(("/"+getName("/rpc")).c_str());
685  statePort.open("/"+ moduleName + "/state:o");
686 
687  closing = false;
688 
689  processing = new Processing( moduleName, state );
690 
691  /* now start the thread to do the work */
692  processing->open();
693 
694  attach(rpcPort);
695 
696  return true;
697  }
698 
699  /**********************************************************/
700  bool close()
701  {
702  statePort.close();
703  processing->close();
704  delete processing;
705  return true;
706  }
707 
708  /**********************************************************/
709  bool start()
710  {
711  processing->start_acquisition();
712  return true;
713  }
714 
715  /**********************************************************/
716  bool stop()
717  {
718  processing->stop_acquisition();
719  return true;
720  }
721 
722  /********************************************************/
723  double getPeriod()
724  {
725  return 0.1;
726  }
727 
728  /********************************************************/
729  bool quit()
730  {
731  closing=true;
732  return true;
733  }
734 
735  /********************************************************/
736  bool updateModule()
737  {
738  if(is_changed){
739  is_changed=false;
740  yarp::os::Bottle &outTargets = statePort.prepare();
741  outTargets.clear();
742  outTargets.addString(state);
743  yDebug() << "outTarget:" << outTargets.toString().c_str();
744  statePort.write();
745  }
746  return !closing;
747  }
748 
749  /********************************************************/
750  yarp::os::Bottle get_dependency()
751  {
752  yarp::os::Bottle answer;
753  answer = processing->get_dependency();
754 
755  return answer;
756  }
757 
758  /********************************************************/
759  yarp::os::Bottle get_parseLabel()
760  {
761  yarp::os::Bottle answer;
762  answer = processing->get_parseLabel();
763 
764  return answer;
765  }
766 
767  /********************************************************/
768  yarp::os::Bottle get_partOfSpeech()
769  {
770  yarp::os::Bottle answer;
771  answer = processing->get_partOfSpeech();
772 
773  return answer;
774  }
775 
776  /********************************************************/
777  yarp::os::Bottle get_lemma()
778  {
779  yarp::os::Bottle answer;
780  answer = processing->get_lemma();
781 
782  return answer;
783  }
784 
785  /********************************************************/
786  yarp::os::Bottle get_morphology()
787  {
788  yarp::os::Bottle answer;
789  answer = processing->get_morphology();
790 
791  return answer;
792  }
793 
794  /********************************************************/
795  yarp::os::Bottle get_sentiment()
796  {
797  yarp::os::Bottle answer;
798  answer = processing->get_sentiment();
799 
800  return answer;
801  }
802 
803 };
804 
805 /********************************************************/
806 int main(int argc, char *argv[])
807 {
808  yarp::os::Network::init();
809 
810  yarp::os::Network yarp;
811  if (!yarp.checkNetwork())
812  {
813  yError("YARP server not available!");
814  return 1;
815  }
816 
817  Module module;
818  yarp::os::ResourceFinder rf;
819 
820  rf.setVerbose( true );
821  rf.setDefaultContext( "googleSpeechProcess" );
822  rf.setDefaultConfigFile( "config.ini" );
823  rf.setDefault("name","googleSpeechProcess");
824  rf.configure(argc,argv);
825 
826  return module.runModule(rf);
827 }