ToPS
|
00001 /* 00002 * ConfigurationReader.cpp 00003 * 00004 * Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br> 00005 * Ígor Bonádio <ibonadio@ime.usp.br> 00006 * Vitor Onuchic <vitoronuchic@gmail.com> 00007 * Alan Mitchell Durham <aland@usp.br> 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00022 * MA 02110-1301, USA. 00023 */ 00024 00025 #include "ConfigurationReader.hpp" 00026 #include "ProbabilisticModelParameter.hpp" 00027 #include <boost/spirit/core.hpp> 00028 #include <fstream> 00029 #include "util.hpp" 00030 using namespace boost::spirit; 00031 namespace tops { 00032 00033 struct store_parameter 00034 { 00035 store_parameter(ConfigurationReader *c) : _c(c){}; 00036 template<typename IteratorT> 00037 void operator()(IteratorT first, IteratorT last) const 00038 { 00039 std::string str; 00040 std::copy(first, last, std::back_inserter(str)); 00041 _c->add_parameter(); 00042 _c->reset(); 00043 } 00044 private: 00045 ConfigurationReader * _c; 00046 }; 00047 00048 struct set_first_word 00049 { 00050 set_first_word(ConfigurationReader *c) : _c(c){}; 00051 template<typename IteratorT> 00052 void operator()(IteratorT first, IteratorT last) const 00053 { 00054 std::string str; 00055 std::copy(first, last, std::back_inserter(str)); 00056 _c->setAuxString2(str); 00057 } 00058 private: 00059 ConfigurationReader * _c; 00060 }; 00061 00062 struct set_second_word 00063 { 00064 set_second_word(ConfigurationReader *c) : _c(c){}; 00065 template<typename IteratorT> 00066 void operator()(IteratorT first, IteratorT last) const 00067 { 00068 std::string str; 00069 std::copy(first, last, std::back_inserter(str)); 00070 _c->setAuxString3(str); 00071 } 00072 private: 00073 ConfigurationReader * _c; 00074 }; 00075 00076 00077 00078 struct set_parameter_name 00079 { 00080 set_parameter_name(ConfigurationReader *c) : _c(c){}; 00081 template<typename IteratorT> 00082 void operator()(IteratorT first, IteratorT last) const 00083 { 00084 std::string str; 00085 std::copy(first, last, std::back_inserter(str)); 00086 _c->setCurrentParameterName(str); 00087 // std::cerr << "NAME: " << str << std::endl; 00088 } 00089 private: 00090 ConfigurationReader * _c; 00091 }; 00092 00093 struct set_parameter_value_word 00094 { 00095 set_parameter_value_word(ConfigurationReader *c) : _c(c){}; 00096 template<typename IteratorT> 00097 void operator()(IteratorT first, IteratorT last) const 00098 { 00099 std::string str; 00100 std::copy(first+1, last-1, std::back_inserter(str)); 00101 StringParameterValuePtr value = StringParameterValuePtr (new StringParameterValue(str)); 00102 _c->setCurrentParameterValue(value); 00103 // std::cerr << "STRING: " << str << std::endl; 00104 } 00105 private: 00106 ConfigurationReader * _c; 00107 }; 00108 00109 00110 struct set_parameter_value_string 00111 { 00112 set_parameter_value_string(ConfigurationReader *c) : _c(c){}; 00113 template<typename IteratorT> 00114 void operator()(IteratorT first, IteratorT last) const 00115 { 00116 std::string str; 00117 std::copy(first, last, std::back_inserter(str)); 00118 StringParameterValuePtr value = StringParameterValuePtr (new StringParameterValue(str)); 00119 _c->setCurrentParameterValue(value); 00120 // std::cerr << "STRING: " << str << std::endl; 00121 } 00122 private: 00123 ConfigurationReader * _c; 00124 }; 00125 00126 struct set_parameter_value_int 00127 { 00128 set_parameter_value_int(ConfigurationReader *c) : _c(c){}; 00129 void operator()(int num) const 00130 { 00131 IntParameterValuePtr value = IntParameterValuePtr (new IntParameterValue(num)); 00132 _c->setCurrentParameterValue(value); 00133 // std::cerr << "INT: " << num << std::endl; 00134 } 00135 private: 00136 ConfigurationReader * _c; 00137 }; 00138 00139 00140 struct set_parameter_value_double 00141 { 00142 set_parameter_value_double(ConfigurationReader *c) : _c(c){}; 00143 void operator()(double num) const 00144 { 00145 DoubleParameterValuePtr value = DoubleParameterValuePtr (new DoubleParameterValue(num)); 00146 _c->setCurrentParameterValue(value); 00147 // std::cerr << "DOUBLE: " << num << std::endl; 00148 } 00149 private: 00150 ConfigurationReader * _c; 00151 }; 00152 00153 00154 struct create_double_vector 00155 { 00156 create_double_vector(ConfigurationReader *c) : _c(c){}; 00157 void operator()(double n) const 00158 { 00159 DoubleVectorParameterValuePtr v = DoubleVectorParameterValuePtr(new DoubleVectorParameterValue()); 00160 _c->setCurrentParameterValue(v); 00161 (v->getDoubleVector()).push_back(n); 00162 // std::cerr << "DOUBLE_VECTOR_FIRST: " << n << std::endl; 00163 } 00164 private: 00165 ConfigurationReader * _c; 00166 }; 00167 struct add_value_to_double_vector 00168 { 00169 add_value_to_double_vector(ConfigurationReader *c) : _c(c){}; 00170 void operator()(double n) const 00171 { 00172 (_c->getCurrentParameterValue()->getDoubleVector()).push_back(n); 00173 // std::cerr << "ADD_DOUBLE_VECTOR: " << n << std::endl; 00174 } 00175 private: 00176 ConfigurationReader * _c; 00177 }; 00178 00179 struct create_int_vector 00180 { 00181 create_int_vector(ConfigurationReader *c) : _c(c){}; 00182 void operator()(int n) const 00183 { 00184 IntVectorParameterValuePtr v = IntVectorParameterValuePtr(new IntVectorParameterValue()); 00185 _c->setCurrentParameterValue(v); 00186 (v->getIntVector()).push_back(n); 00187 // std::cerr << "CREATE_INT_VECTOR: " << n << std::endl; 00188 } 00189 private: 00190 ConfigurationReader * _c; 00191 }; 00192 struct add_value_to_int_vector 00193 { 00194 add_value_to_int_vector(ConfigurationReader *c) : _c(c){}; 00195 void operator()(int n) const 00196 { 00197 (_c->getCurrentParameterValue()->getIntVector()).push_back(n); 00198 // std::cerr << "ADD_INT_VECTOR: " << n << std::endl; 00199 } 00200 private: 00201 ConfigurationReader * _c; 00202 }; 00203 00204 struct create_string_vector 00205 { 00206 create_string_vector(ConfigurationReader *c) : _c(c){}; 00207 template<typename IteratorT> 00208 void operator()(IteratorT first, IteratorT last) const 00209 { 00210 std::string str; 00211 std::copy(first+1, last-1, std::back_inserter(str)); 00212 StringVectorParameterValuePtr v = StringVectorParameterValuePtr(new StringVectorParameterValue()); 00213 _c->setCurrentParameterValue(v); 00214 (v->getStringVector()).push_back(str); 00215 // std::cerr << "CREATE_STRING_VECTOR: " << str << std::endl; 00216 } 00217 private: 00218 ConfigurationReader * _c; 00219 }; 00220 struct add_value_to_string_vector 00221 { 00222 add_value_to_string_vector(ConfigurationReader *c) : _c(c){}; 00223 template<typename IteratorT> 00224 void operator()(IteratorT first, IteratorT last) const 00225 { 00226 std::string str; 00227 std::copy(first+1, last-1, std::back_inserter(str)); 00228 (_c->getCurrentParameterValue()->getStringVector()).push_back(str); 00229 // std::cerr << "ADD_STRING_VECTOR: " << str << std::endl; 00230 } 00231 private: 00232 ConfigurationReader * _c; 00233 }; 00234 00235 00236 struct create_prob_table_entry 00237 { 00238 create_prob_table_entry(ConfigurationReader *c) : _c(c){}; 00239 template<typename IteratorT> 00240 void operator()(IteratorT first, IteratorT last) const 00241 { 00242 std::string str; 00243 std::copy(first+1, last-2, std::back_inserter(str)); 00244 00245 double v = 0.0; 00246 if( (_c->getCurrentParameterValue()->getDoubleMap()).find(str) == (_c->getCurrentParameterValue()->getDoubleMap()).end()) { 00247 (_c->getCurrentParameterValue()->getDoubleMap())[str] = v; 00248 } 00249 _c->setAuxString(str); 00250 } 00251 private: 00252 ConfigurationReader * _c; 00253 }; 00254 00255 00256 struct create_prob_table 00257 { 00258 create_prob_table(ConfigurationReader *c) : _c(c){}; 00259 template<typename IteratorT> 00260 void operator()(IteratorT first, IteratorT last) const 00261 { 00262 DoubleMapParameterValuePtr probTable = 00263 DoubleMapParameterValuePtr(new DoubleMapParameterValue()); 00264 std::string str; 00265 std::copy(first+1, last-2, std::back_inserter(str)); 00266 00267 double v =0.0; 00268 if( (probTable->getDoubleMap()).find(str) == (probTable->getDoubleMap()).end()) { 00269 (probTable->getDoubleMap())[str] = v; 00270 } 00271 _c->setAuxString(str); 00272 _c->setCurrentParameterValue(probTable); 00273 // std::cerr << "ADD_TABLE_ENTRY: " << str << std::endl; 00274 } 00275 private: 00276 ConfigurationReader * _c; 00277 }; 00278 00279 00280 struct add_prob 00281 { 00282 add_prob(ConfigurationReader *c) : _c(c){}; 00283 void operator()(double n) const 00284 { 00285 (((_c->getCurrentParameterValue()->getDoubleMap()).find(_c->getAuxString()))->second) = n; 00286 // std::cerr << "ADD_PROB_ELEMENT: " << n << std::endl; 00287 } 00288 private: 00289 ConfigurationReader * _c; 00290 }; 00291 00292 00293 00294 struct create_string_map 00295 { 00296 create_string_map(ConfigurationReader *c) : _c(c){}; 00297 template<typename IteratorT> 00298 void operator()(IteratorT first, IteratorT last) const 00299 { 00300 StringMapParameterValuePtr str_map = 00301 StringMapParameterValuePtr(new StringMapParameterValue()); 00302 std::string str; 00303 std::copy(first+1, last-1, std::back_inserter(str)); 00304 std::string v; 00305 if( (str_map->getStringMap()).find(str) == (str_map->getStringMap()).end()) { 00306 (str_map->getStringMap())[str] = v; 00307 } 00308 _c->setAuxString(str); 00309 _c->setCurrentParameterValue(str_map); 00310 } 00311 private: 00312 ConfigurationReader * _c; 00313 }; 00314 00315 struct add_str_map 00316 { 00317 add_str_map(ConfigurationReader *c) : _c(c){}; 00318 template<typename IteratorT> 00319 void operator()(IteratorT first, IteratorT last) const 00320 { 00321 std::string str; 00322 std::copy(first+1, last-1, std::back_inserter(str)); 00323 (_c->getCurrentParameterValue()->getStringMap() )[_c->getAuxString()] = str; 00324 } 00325 private: 00326 ConfigurationReader * _c; 00327 }; 00328 00329 00330 00331 struct add_new_map 00332 { 00333 add_new_map(ConfigurationReader *c) : _c(c){}; 00334 template<typename IteratorT> 00335 void operator()(IteratorT first, IteratorT last) const 00336 { 00337 std::string str; 00338 std::copy(first+1, last-1, std::back_inserter(str)); 00339 std::string v; 00340 if( (_c->getCurrentParameterValue()->getStringMap()).find(str) == (_c->getCurrentParameterValue()->getStringMap()).end()) { 00341 (_c->getCurrentParameterValue()->getStringMap())[str] = v; 00342 } 00343 _c->setAuxString(str); 00344 } 00345 private: 00346 ConfigurationReader * _c; 00347 }; 00348 00349 00350 00351 struct create_transition 00352 { 00353 create_transition(ConfigurationReader *c) : _c(c){}; 00354 template<typename IteratorT> 00355 void operator()(IteratorT first, IteratorT last) const 00356 { 00357 DoubleMapParameterValuePtr m = 00358 DoubleMapParameterValuePtr(new DoubleMapParameterValue()); 00359 00360 std::string str; 00361 std::copy(first, last-1, std::back_inserter(str)); 00362 double v = 0.0; 00363 00364 std::stringstream out; 00365 std::string symbol(_c->getAuxString2()); 00366 std::string context(_c->getAuxString3()); 00367 tops::trim_spaces(symbol); 00368 tops::trim_spaces(context); 00369 if(context.size() == 0) 00370 out << symbol ; 00371 else 00372 out << symbol << "|" << context ; 00373 str = out.str(); 00374 00375 00376 00377 if( (m->getDoubleMap()).find(str) == (m->getDoubleMap()).end()) { 00378 (m->getDoubleMap())[str] = v; 00379 } 00380 _c->setAuxString(str); 00381 _c->setCurrentParameterValue(m); 00382 } 00383 private: 00384 ConfigurationReader * _c; 00385 }; 00386 00387 struct create_transition_entry 00388 { 00389 create_transition_entry(ConfigurationReader *c) : _c(c){}; 00390 template<typename IteratorT> 00391 void operator()(IteratorT first, IteratorT last) const 00392 { 00393 std::string str; 00394 std::copy(first, last-1, std::back_inserter(str)); 00395 double v = 0.0; 00396 00397 std::stringstream out; 00398 std::string symbol(_c->getAuxString2()); 00399 std::string context(_c->getAuxString3()); 00400 tops::trim_spaces(symbol); 00401 tops::trim_spaces(context); 00402 if(context.size() == 0) 00403 out << symbol ; 00404 else 00405 out << symbol << "|" << context ; 00406 str = out.str(); 00407 00408 if( (_c->getCurrentParameterValue()->getDoubleMap()).find(str) == (_c->getCurrentParameterValue()->getDoubleMap()).end()) { 00409 (_c->getCurrentParameterValue()->getDoubleMap())[str] = v; 00410 } 00411 _c->setAuxString(str); 00412 // std::cerr << "CREATING_TRANSITION_ENTRY: " << str << std::endl; 00413 00414 } 00415 private: 00416 ConfigurationReader * _c; 00417 }; 00418 00419 00420 00421 00422 00423 00424 struct add_transition_prob 00425 { 00426 add_transition_prob(ConfigurationReader *c) : _c(c){}; 00427 void operator()(double n) const 00428 { 00429 (((_c->getCurrentParameterValue()->getDoubleMap()).find(_c->getAuxString()))->second) = n; 00430 } 00431 private: 00432 ConfigurationReader * _c; 00433 }; 00434 00435 00436 struct print_context 00437 { 00438 print_context() {}; 00439 template<typename IteratorT> 00440 void operator()(IteratorT first, IteratorT last) const 00441 { 00442 std::string str; 00443 std::copy(first, last, std::back_inserter(str)); 00444 std::cerr << " CONTEXT : " << std::endl; 00445 std::cerr << str << std::endl; 00446 } 00447 private: 00448 }; 00449 00450 bool ConfigurationReader::loadFromFile(const std::string & filename) 00451 { 00452 std::ifstream input; 00453 std::string line; 00454 input.open(filename.c_str()); 00455 if (!input.is_open()) { 00456 std::cerr << "Cant open file " << filename << std::endl; 00457 exit(-1); 00458 } 00459 string conf; 00460 while (!input.eof()) { 00461 getline(input, line, '\n'); 00462 line += "\n"; 00463 conf.append(line); 00464 } 00465 input.close(); 00466 return load( conf); 00467 } 00468 00469 bool ConfigurationReader::load(std::string & data) 00470 { 00471 00472 rule<phrase_scanner_t> config_file, parameter_spec, parameter_value, 00473 parameter_name, prob_table, string_vector, double_vector, 00474 int_vector, word, word_p, string_map, transition_map, nested_configuration, nested_parameter_spec; 00475 word_p 00476 = lexeme_d [ +(alnum_p | (ch_p('_') | '.' | '/' | '-' | ' ' | ',' | '+' ))] 00477 ; 00478 word 00479 = ch_p('"') 00480 >> word_p 00481 >> ch_p('"') 00482 ; 00483 double_vector 00484 = ch_p('(') 00485 >> real_p[create_double_vector(this)] 00486 >> * (',' >> real_p[add_value_to_double_vector(this)]) 00487 >> ')' 00488 ; 00489 string_vector 00490 = ch_p('(') 00491 >> word[create_string_vector(this)] 00492 >> * (',' >> word[add_value_to_string_vector(this)]) 00493 >> ')' 00494 ; 00495 transition_map 00496 = ch_p('(') 00497 >> '"' 00498 >> ( + word_p) [set_first_word(this)] 00499 >> '"' 00500 >> 00501 ( (ch_p('|') >> '"' >> ( *word_p ) [set_second_word(this)] >> '"' >> ':' ) 00502 | 00503 ch_p(':') ) [create_transition(this)] 00504 >> real_p [add_prob(this)] 00505 00506 >> *( ch_p(';') 00507 >> '"' 00508 >> (+ word_p) [set_first_word(this)] 00509 >> '"' 00510 >> 00511 ( (ch_p('|') >> '"' >> ( *word_p )[set_second_word(this)] >> '"' >> ':' ) 00512 | 00513 ch_p(':') ) [create_transition_entry(this)] 00514 >> real_p [add_prob(this)] ) 00515 >> !( ch_p(';') ) >> ')' 00516 ; 00517 string_map 00518 = ch_p('(') 00519 >> ('"' 00520 >> +word_p 00521 >> '"' )[create_string_map(this)] 00522 >> ch_p(':') 00523 >> ('"' 00524 >> +word_p 00525 >> '"' ) [add_str_map (this)] 00526 >> *( ';' 00527 >> ( '"' 00528 >> +word_p 00529 >> '"')[add_new_map(this)] 00530 >> ':' 00531 >> ('"' 00532 >> +word_p 00533 >> '"' ) [add_str_map(this)] ) 00534 >> !( ch_p(';') ) >> ')' 00535 00536 ; 00537 parameter_name 00538 = lexeme_d [ alpha_p >> *(alnum_p | (ch_p('_') | '.' | '/'))] 00539 ; 00540 parameter_value 00541 = double_vector 00542 | parameter_name [set_parameter_value_string(this)] 00543 | word [set_parameter_value_word(this)] 00544 | string_vector 00545 | transition_map 00546 | strict_real_p [set_parameter_value_double(this)] 00547 | int_p [set_parameter_value_int(this)] 00548 | nested_configuration [set_parameter_value_string(this)] 00549 | string_map 00550 ; 00551 00552 nested_parameter_spec 00553 = parameter_name >> '=' >> parameter_value 00554 ; 00555 parameter_spec 00556 = parameter_name[set_parameter_name(this)] >> '=' >> parameter_value ; 00557 ; 00558 nested_configuration 00559 = (ch_p('[') >> +nested_parameter_spec >> ch_p(']')) 00560 ; 00561 config_file 00562 = parameter_spec[store_parameter(this)] >> (config_file | !end_p) 00563 ; 00564 00565 parse_info<> info ; 00566 bool inComment = false; 00567 int k = 0; 00568 for(int i = 0; i < (int)data.size(); i++) 00569 { 00570 if (data[i] == '#' ) 00571 inComment = true; 00572 if(data[i] == '\n') 00573 inComment = false; 00574 if(!inComment) 00575 { 00576 data[k] = data[i]; 00577 k++; 00578 } 00579 00580 } 00581 data.resize(k); 00582 00583 info = parse(data.c_str(), config_file, space_p); 00584 if(info.full) 00585 { 00586 return true; 00587 } 00588 else 00589 { 00590 std::cout << "-------------------------\n"; 00591 std::cout << "Parsing failed " << info.stop <<"\n"; 00592 std::cout << "-------------------------\n"; 00593 return false; 00594 } 00595 00596 } 00597 00598 void ConfigurationReader::setCurrentParameterValue(ProbabilisticModelParameterValuePtr value){ 00599 _current_value = value; 00600 } 00601 ProbabilisticModelParameterValuePtr ConfigurationReader::getCurrentParameterValue(){ 00602 return _current_value; 00603 } 00604 void ConfigurationReader::setCurrentParameterName(const std::string & name) { 00605 _current_name = name; 00606 } 00607 00608 void ConfigurationReader::setAuxString(const std::string & aux) { 00609 _aux_string = aux; 00610 } 00611 00612 std::string ConfigurationReader::getAuxString(){ 00613 return _aux_string; 00614 } 00615 00616 std::string ConfigurationReader::getCurrentParameterName(){ 00617 return _current_name; 00618 } 00619 00620 void ConfigurationReader::add_parameter() { 00621 _parameters->add(_current_name, _current_value); 00622 } 00623 00624 ProbabilisticModelParametersPtr ConfigurationReader::parameters(){ 00625 return _parameters; 00626 } 00627 00628 ConfigurationReader::ConfigurationReader(){ 00629 _parameters = ProbabilisticModelParametersPtr (new ProbabilisticModelParameters()); 00630 } 00631 00632 std::string ConfigurationReader::getAuxString2(){ 00633 return _aux_string_2; 00634 } 00635 00636 00637 std::string ConfigurationReader::getAuxString3(){ 00638 return _aux_string_3; 00639 } 00640 00641 void ConfigurationReader::setAuxString2(const std::string & aux){ 00642 _aux_string_2 = aux; 00643 } 00644 00645 void ConfigurationReader::setAuxString3(const std::string & aux) 00646 { 00647 _aux_string_3 = aux; 00648 } 00649 00650 void ConfigurationReader::reset() { 00651 ProbabilisticModelParameterValuePtr a; 00652 _current_value =a ; 00653 _current_name = ""; 00654 _aux_string = ""; 00655 _aux_string_2 = ""; 00656 _aux_string_3 = ""; 00657 00658 } 00659 }; 00660