kspell_aspelldict.cpp00001
00021 #include "kspell_aspelldict.h"
00022
00023 #include <kdebug.h>
00024
00025 #include <qtextcodec.h>
00026
00027 using namespace KSpell2;
00028
00029 ASpellDict::ASpellDict( const QString& lang )
00030 : Dictionary( lang )
00031 {
00032 m_config = new_aspell_config();
00033 aspell_config_replace( m_config, "lang", lang.latin1() );
00034
00035
00036 aspell_config_replace( m_config, "encoding", "utf-8" );
00037
00038 AspellCanHaveError * possible_err = new_aspell_speller( m_config );
00039
00040 if ( aspell_error_number( possible_err ) != 0 )
00041 kdDebug()<< "Error : "<< aspell_error_message( possible_err ) <<endl;
00042 else
00043 m_speller = to_aspell_speller( possible_err );
00044
00045 }
00046
00047 ASpellDict::~ASpellDict()
00048 {
00049 delete_aspell_speller( m_speller );
00050 delete_aspell_config( m_config );
00051 }
00052
00053 bool ASpellDict::check( const QString& word )
00054 {
00055
00056
00057 int correct = aspell_speller_check( m_speller, word.utf8(), word.utf8().length() );
00058 return correct;
00059 }
00060
00061 QStringList ASpellDict::suggest( const QString& word )
00062 {
00063
00064 QTextCodec *codec = QTextCodec::codecForName("utf8");
00065
00066
00067
00068 const AspellWordList * suggestions = aspell_speller_suggest( m_speller,
00069 word.utf8(),
00070 word.utf8().length() );
00071
00072 AspellStringEnumeration * elements = aspell_word_list_elements( suggestions );
00073
00074 QStringList qsug;
00075 const char * cword;
00076
00077 while ( (cword = aspell_string_enumeration_next( elements )) ) {
00078
00079
00080 qsug.append( codec->toUnicode( cword ) );
00081 }
00082
00083 delete_aspell_string_enumeration( elements );
00084 return qsug;
00085 }
00086
00087 bool ASpellDict::checkAndSuggest( const QString& word,
00088 QStringList& suggestions )
00089 {
00090 bool c = check( word );
00091 if ( c )
00092 suggestions = suggest( word );
00093 return c;
00094 }
00095
00096 bool ASpellDict::storeReplacement( const QString& bad,
00097 const QString& good )
00098 {
00099
00100
00101 return aspell_speller_store_replacement( m_speller,
00102 bad.utf8(), bad.utf8().length(),
00103 good.utf8(), good.utf8().length() );
00104 }
00105
00106 bool ASpellDict::addToPersonal( const QString& word )
00107 {
00108 kdDebug() << "ASpellDict::addToPersonal: word = " << word << endl;
00109
00110
00111 aspell_speller_add_to_personal( m_speller, word.utf8(),
00112 word.utf8().length() );
00113
00114
00115
00116 return aspell_speller_save_all_word_lists( m_speller );
00117 }
00118
00119 bool ASpellDict::addToSession( const QString& word )
00120 {
00121
00122
00123 return aspell_speller_add_to_session( m_speller, word.utf8(),
00124 word.utf8().length() );
00125 }
|