/*
File: speller.cpp
Copyright 2009 Chris Tonkinson (cmtonkinson@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
/**
* @file speller.cpp
* @author Chris Tonkinson (cmtonkinson@gmail.com)
* @date May 2009
* @brief Implementation file for the Speller class (and associated classes).
*/
#include "speller.h"
/********************************************************* EXCEPTION *********************************************************/
SpellerException::SpellerException( void ) {
return;
}
SpellerException::SpellerException( const SpellerException& ref ) {
message( ref.message() );
return;
}
SpellerException::SpellerException( const char* format, ... ) {
char buffer[SPELLER_MAX_BUFFER];
std::string foo( "Speller: " );
va_list args;
// Print our variable string to the buffer...
va_start( args, format );
vsprintf( buffer, format, args );
va_end( args );
// Finalize the message...
message( foo.append( buffer ) );
return;
}
SpellerException::~SpellerException( void ) {
return;
}
/********************************************************* SPELLER *********************************************************/
Speller::Speller( void ) {
config( new_aspell_config() );
speller( NULL );
return;
}
Speller::~Speller( void ) {
delete_aspell_config( config() );
if ( speller() ) {
delete_aspell_speller( speller() );
}
return;
}
void Speller::config( const char* key, const char* value ) { // public
if ( value ) {
/* we've got a value, so set the key */
if ( !aspell_config_replace( config(), key, value ) ) {
throw SpellerException( aspell_config_error_message( config() ) );
}
} else {
/* value was null, so reset the given key to default */
aspell_config_remove( config(), key );
}
/* we've changed the config, so if we've already got a speller, reset it */
if ( speller() ) {
delete_aspell_speller( speller() );
speller( NULL );
init();
}
return;
}
void Speller::init( void ) { // public
if ( !speller() ) {
AspellCanHaveError* possible_error = new_aspell_speller( config() );
if ( aspell_error_number( possible_error ) != 0 ) {
throw SpellerException( aspell_error_message( possible_error ) );
} else {
speller( to_aspell_speller( possible_error ) );
}
}
return;
}
bool Speller::check( const char* word ) { // public
/* aspell_speller_check() is documented to return
* -nonzero if the word is found
* -zero if the word is not found
*/
return aspell_speller_check( speller(), word, -1 );
}
std::vector Speller::suggest( const char* word, int limit ) {
int i = 0;
std::vector suggestions;
const char* suggested_word;
/* get our suggestion list from Aspell */
const AspellWordList* suggestion_list = aspell_speller_suggest( speller(), word, -1 );
AspellStringEnumeration* iterator = aspell_word_list_elements( suggestion_list );
/* bounds checking */
if ( limit < 0 ) {
limit = SPELLER_MAX_SUGGESTIONS;
}
/* populate the vector */
while ( ( i++ < limit ) && ( suggested_word = aspell_string_enumeration_next( iterator ) ) != NULL ) {
suggestions.push_back( suggested_word );
}
/* clean up */
delete_aspell_string_enumeration( iterator );
return suggestions;
}
const char* Speller::recommend( const char* word ) {
std::string recommendation;
/* get our suggestion list from Aspell */
const AspellWordList* suggestion_list = aspell_speller_suggest( speller(), word, -1 );
AspellStringEnumeration* iterator = aspell_word_list_elements( suggestion_list );
/* take the first suggestion */
recommendation.assign( aspell_string_enumeration_next( iterator ) );
/* clean up */
delete_aspell_string_enumeration( iterator );
return recommendation.c_str();
}