00001
00002
00003
00004
00005
00006
00007
00008 #include "suggest.h"
00012 const char* Speller::Aspell::Suggest::kDICT_DELIM = "/";
00013 const char* Speller::Aspell::Suggest::kEMPTY = "*";
00014 const char* Speller::Aspell::Suggest::kDEF_LANG = "en";
00015 const char* Speller::Aspell::Suggest::kDEF_JARGON = "";
00016 const char* Speller::Aspell::Suggest::kDEF_ENCODING = "utf-8";
00017
00018 void Speller::Aspell::Suggest::checkError() throw( std::runtime_error )
00019 {
00020 if( aspell_speller_error_number( fspeller ) != 0 )
00021 {
00022 std::string err_msg =
00023 std::string( "(Aspell::Speller::Suggest::checkError): "
00024 "aspell speller error " ) +
00025 aspell_speller_error_message( fspeller );
00026 throw std::runtime_error( err_msg );
00027 }
00028 }
00029
00030 void Speller::Aspell::Suggest::checkConfigError()
00031 throw( std::runtime_error )
00032 {
00033 if( aspell_config_error_number( fconfig ) != 0 )
00034 {
00035 std::string err_msg =
00036 std::string( "(Aspell::Speller::Suggest::checkConfig"
00037 "Error): aspell speller error " ) +
00038 aspell_config_error_message( fconfig );
00039 throw std::runtime_error( err_msg );
00040 }
00041 }
00042
00043 void Speller::Aspell::Suggest::init(const std::string& lang,
00044 const std::string& jargon,
00045 const std::string& encoding)
00046 throw( std::invalid_argument, std::runtime_error )
00047 {
00048
00049 flang = lang;
00050 fjargon = jargon;
00051 fencoding = encoding;
00052
00053 fconfig = new_aspell_config();
00054 try
00055 {
00056 setConfig();
00057 }
00058 catch( const std::invalid_argument& err )
00059 {
00060 throw err;
00061 }
00062
00063 AspellCanHaveError* ret = new_aspell_speller( fconfig );
00064 delete_aspell_config( fconfig );
00065 if( aspell_error_number( ret ) != 0 )
00066 {
00067 delete_aspell_can_have_error( ret );
00068 throw std::runtime_error( "(Aspell::Speller::Suggest::init"
00069 "): Error in creating speller." );
00070 }
00071 else
00072 {
00073 fspeller = to_aspell_speller( ret );
00074 fconfig = aspell_speller_config( fspeller );
00075 }
00076 }
00077
00078 void Speller::Aspell::Suggest::listDicts(std::vector<AspellDictInfo>& vals)
00079 {
00080 AspellDictInfoList* dlist = get_aspell_dict_info_list( fconfig );
00081 AspellDictInfoEnumeration* dinfo_enum =
00082 aspell_dict_info_list_elements( dlist );
00083 const AspellDictInfo* entry;
00084 while( (entry = aspell_dict_info_enumeration_next( dinfo_enum )) )
00085 {
00086 vals.push_back( *entry );
00087 }
00088 delete_aspell_dict_info_enumeration( dinfo_enum );
00089 }
00090
00091 void Speller::Aspell::Suggest::listDicts(std::vector<std::string>& vals)
00092 {
00093 std::vector<AspellDictInfo> entries;
00094 listDicts( entries );
00095 for( std::vector<AspellDictInfo>::const_iterator i = entries.begin();
00096 i != entries.end();
00097 ++i )
00098 {
00099 std::string jargon( i->jargon );
00100 std::ostringstream fmt_entry;
00101 fmt_entry << i->name << kDICT_DELIM << i->code << kDICT_DELIM
00102 << (jargon == "" ? kEMPTY : jargon) << kDICT_DELIM
00103 << i->size;
00104 vals.push_back( fmt_entry.str() );
00105 }
00106 }
00107
00108 void
00109 Speller::Aspell::Suggest::printWordList(const AspellWordList* wlist,
00110 char delim)
00111 throw( std::invalid_argument )
00112 {
00113 if( ! wlist )
00114 {
00115 throw std::invalid_argument( "(Aspell.Speller.Suggest.print"
00116 "WordList): word list pointer "
00117 "is null." );
00118 }
00119
00120 AspellStringEnumeration* enum_list =
00121 aspell_word_list_elements( wlist );
00122 const char* next;
00123 while( (next = aspell_string_enumeration_next( enum_list )) )
00124 {
00125 std::cout << next << delim;
00126 }
00127 delete_aspell_string_enumeration( enum_list );
00128 }
00129
00130 void Speller::Aspell::Suggest::setConfig() throw( std::invalid_argument )
00131 {
00132 try
00133 {
00134 setConfigOpt( "lang", flang );
00135 setConfigOpt( "jargon", fjargon );
00136 setConfigOpt( "encoding", fencoding );
00137 }
00138 catch( const std::invalid_argument& err )
00139 {
00140 throw err;
00141 }
00142 }
00143
00144 void
00145 Speller::Aspell::Suggest::storeWordList(const AspellWordList* wlist,
00146 std::vector<std::string>& replacement)
00147 throw( std::invalid_argument )
00148 {
00149 if( ! wlist )
00150 {
00151 throw std::invalid_argument( "(Aspell.Speller.Suggest.store"
00152 "WordList): word list pointer "
00153 "is null." );
00154 }
00155
00156 AspellStringEnumeration* enum_list =
00157 aspell_word_list_elements( wlist );
00158 const char* next;
00159 while( (next = aspell_string_enumeration_next( enum_list )) )
00160 {
00161 replacement.push_back( next );
00162 }
00163 delete_aspell_string_enumeration( enum_list );
00164 }
00165
00166 Speller::Aspell::Suggest::Suggest(const std::string& lang,
00167 const std::string& jargon,
00168 const std::string& encoding)
00169 throw( std::invalid_argument, std::runtime_error )
00170 {
00171
00172 try
00173 {
00174 init( lang, jargon, encoding );
00175 }
00176 catch( const std::invalid_argument& err )
00177 {
00178 throw err;
00179 }
00180 catch( const std::runtime_error& err )
00181 {
00182 throw err;
00183 }
00184 }
00185
00186 Speller::Aspell::Suggest::Suggest(const AspellDictInfo* dinfo,
00187 const std::string& encoding)
00188 throw( std::invalid_argument, std::runtime_error )
00189 {
00190 try
00191 {
00192 init( dinfo->code, dinfo->jargon, encoding );
00193 }
00194 catch( const std::invalid_argument& err )
00195 {
00196 throw err;
00197 }
00198 catch( const std::runtime_error& err )
00199 {
00200 throw err;
00201 }
00202 }
00203
00204 void Speller::Aspell::Suggest::addPersonalList(const std::string& word)
00205 throw( std::runtime_error )
00206 {
00207
00208
00209
00210
00211
00212 aspell_speller_add_to_personal( fspeller, word.c_str(), -1 );
00213 try
00214 {
00215 checkError();
00216 }
00217 catch( const std::runtime_error& err )
00218 {
00219 throw err;
00220 }
00221 }
00222
00223 bool Speller::Aspell::Suggest::checkWord(const std::string& word)
00224 {
00225 bool status = true;
00226
00227 if( aspell_speller_check( fspeller, word.c_str(), -1 ) == 0 )
00228 {
00229 status = false;
00230 }
00231 return status;
00232 }
00233
00234 bool Speller::Aspell::Suggest::checkWord(const std::string& word,
00235 std::vector<std::string>& replacement,
00236 bool always)
00237 throw( std::invalid_argument )
00238 {
00239
00240
00241
00242
00243
00244
00245 bool status = checkWord( word );
00246
00247 if( always )
00248 {
00249 const AspellWordList* wlist =
00250 aspell_speller_suggest( fspeller, word.c_str(), -1 );
00251 try
00252 {
00253 storeWordList( wlist, replacement );
00254 }
00255 catch( const std::invalid_argument& err )
00256 {
00257 throw err;
00258 }
00259 }
00260 else
00261 {
00262 if( ! status )
00263 {
00264 const AspellWordList* wlist =
00265 aspell_speller_suggest( fspeller, word.c_str(),
00266 -1 );
00267 try
00268 {
00269 storeWordList( wlist, replacement );
00270 }
00271 catch( const std::invalid_argument& err )
00272 {
00273 throw err;
00274 }
00275 }
00276 }
00277 return status;
00278 }
00279
00280 void Speller::Aspell::Suggest::clearSessionList() throw( std::runtime_error )
00281 {
00282 aspell_speller_clear_session( fspeller );
00283 try
00284 {
00285 checkError();
00286 }
00287 catch( const std::runtime_error& err )
00288 {
00289 throw err;
00290 }
00291 }
00292
00293 std::string Speller::Aspell::Suggest::getConfigOpt(const std::string& opt)
00294 {
00295 return std::string( aspell_config_retrieve( fconfig, opt.c_str() ) );
00296 }
00297
00298 void Speller::Aspell::Suggest::getConfigOpt(const std::string& opt,
00299 std::vector<std::string>& vals)
00300 {
00301
00302
00303 AspellStringList* list = new_aspell_string_list();
00304 AspellMutableContainer* lst0 =
00305 aspell_string_list_to_mutable_container( list );
00306 aspell_config_retrieve_list( fconfig, opt.c_str(), lst0 );
00307 AspellStringEnumeration* enum_list =
00308 aspell_string_list_elements( list );
00309 const char* next;
00310 while( (next = aspell_string_enumeration_next( enum_list )) )
00311 {
00312 vals.push_back( next );
00313 }
00314 delete_aspell_string_enumeration( enum_list );
00315 delete_aspell_string_list( list );
00316 }
00317
00318 void Speller::Aspell::Suggest::ignoreWord(const std::string& word)
00319 throw( std::runtime_error )
00320 {
00321
00322
00323 aspell_speller_add_to_session( fspeller, word.c_str(), -1 );
00324 try
00325 {
00326 checkError();
00327 }
00328 catch( const std::runtime_error& err )
00329 {
00330 throw err;
00331 }
00332 }
00333
00334 void Speller::Aspell::Suggest::printMainList() throw( std::invalid_argument )
00335 {
00336 const AspellWordList* wlist =
00337 aspell_speller_main_word_list( fspeller );
00338 try
00339 {
00340 printWordList( wlist );
00341 }
00342 catch( const std::invalid_argument& err )
00343 {
00344 throw err;
00345 }
00346 }
00347
00348 void Speller::Aspell::Suggest::printPersonalList()
00349 throw( std::invalid_argument )
00350 {
00351 const AspellWordList* wlist =
00352 aspell_speller_personal_word_list( fspeller );
00353 try
00354 {
00355 printWordList( wlist );
00356 }
00357 catch( const std::invalid_argument& err )
00358 {
00359 throw err;
00360 }
00361 }
00362
00363 void Speller::Aspell::Suggest::printSessionList()
00364 throw( std::invalid_argument )
00365 {
00366 const AspellWordList* wlist =
00367 aspell_speller_session_word_list( fspeller );
00368 try
00369 {
00370 printWordList( wlist );
00371 }
00372 catch( const std::invalid_argument& err )
00373 {
00374 throw err;
00375 }
00376 }
00377
00378 bool Speller::Aspell::Suggest::printSuggestions(const std::string& word,
00379 bool always)
00380 throw( std::invalid_argument )
00381 {
00382
00383
00384
00385
00386
00387 bool status = checkWord( word );
00388
00389 if( always )
00390 {
00391 const AspellWordList* wlist =
00392 aspell_speller_suggest( fspeller, word.c_str(), -1 );
00393 try
00394 {
00395 printWordList( wlist );
00396 }
00397 catch( const std::invalid_argument& err )
00398 {
00399 throw err;
00400 }
00401 }
00402 else
00403 {
00404 if( ! status )
00405 {
00406 const AspellWordList* wlist =
00407 aspell_speller_suggest( fspeller, word.c_str(),
00408 -1 );
00409 try
00410 {
00411 printWordList( wlist );
00412 }
00413 catch( const std::invalid_argument& err )
00414 {
00415 throw err;
00416 }
00417 }
00418 }
00419 return status;
00420 }
00421
00422 void Speller::Aspell::Suggest::resetConfig()
00423 throw( std::invalid_argument, std::runtime_error )
00424 {
00425 delete_aspell_config( fconfig );
00426 fconfig = new_aspell_config();
00427 try
00428 {
00429 setConfig();
00430 }
00431 catch( const std::invalid_argument& err )
00432 {
00433 throw err;
00434 }
00435
00436 AspellCanHaveError* ret = new_aspell_speller( fconfig );
00437 if( aspell_error_number( ret ) != 0 )
00438 {
00439 delete_aspell_can_have_error( ret );
00440 throw std::runtime_error( "(Aspell::Speller::Suggest::Reset"
00441 "Config): Error in creating "
00442 "speller." );
00443 }
00444 else
00445 {
00446
00447
00448 fspeller = to_aspell_speller( ret );
00449 delete_aspell_config( fconfig );
00450 fconfig = aspell_speller_config( fspeller );
00451 }
00452 }
00453
00454 void Speller::Aspell::Suggest::resetConfig(const std::string& lang,
00455 const std::string& jargon,
00456 const std::string& encoding)
00457 throw( std::invalid_argument, std::runtime_error )
00458 {
00459
00460 flang = lang;
00461 fjargon = jargon;
00462 fencoding = encoding;
00463
00464 try
00465 {
00466 resetConfig();
00467 }
00468 catch( const std::invalid_argument& err )
00469 {
00470 throw err;
00471 }
00472 catch( const std::runtime_error& err )
00473 {
00474 throw err;
00475 }
00476 }
00477
00478 void Speller::Aspell::Suggest::saveLists() throw( std::runtime_error )
00479 {
00480
00481 aspell_speller_save_all_word_lists( fspeller );
00482 try
00483 {
00484 checkError();
00485 }
00486 catch( const std::runtime_error& err )
00487 {
00488 throw err;
00489 }
00490 }
00491
00492 void Speller::Aspell::Suggest::setConfigOpt(const std::string& opt,
00493 const std::string& val)
00494 throw( std::invalid_argument )
00495 {
00496 aspell_config_replace( fconfig, opt.c_str(), val.c_str() );
00497 try
00498 {
00499 checkConfigError();
00500 }
00501 catch( const std::invalid_argument& err )
00502 {
00503 throw err;
00504 }
00505 }
00506
00507 void
00508 Speller::Aspell::Suggest::StoreMainList(std::vector<std::string>& replacement)
00509 throw( std::invalid_argument )
00510 {
00511 const AspellWordList* wlist =
00512 aspell_speller_main_word_list( fspeller );
00513 try
00514 {
00515 storeWordList( wlist, replacement );
00516 }
00517 catch( const std::invalid_argument& err )
00518 {
00519 throw err;
00520 }
00521 }
00522
00523 void Speller::Aspell::Suggest::StorePersonalList(std::vector<std::string>& replacement) throw( std::invalid_argument )
00524 {
00525
00526
00527 const AspellWordList* wlist =
00528 aspell_speller_personal_word_list( fspeller );
00529 try
00530 {
00531 storeWordList( wlist, replacement );
00532 }
00533 catch( const std::invalid_argument& err )
00534 {
00535 throw err;
00536 }
00537 }
00538
00539 void Speller::Aspell::Suggest::StoreSessionList(std::vector<std::string>& replacement) throw( std::invalid_argument )
00540 {
00541
00542
00543 const AspellWordList* wlist =
00544 aspell_speller_session_word_list( fspeller );
00545 try
00546 {
00547 storeWordList( wlist, replacement );
00548 }
00549 catch( const std::invalid_argument& err )
00550 {
00551 throw err;
00552 }
00553 }
00554
00555