00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef ASPELLWRAPPERCLASS_HPP
00018 #define ASPELLWRAPPERCLASS_HPP
00019
00020 #include "aspell.h"
00021 #include <string>
00022 #include <vector>
00023 #include <fstream>
00024 #include "ocString.h"
00025
00026 using namespace std;
00027
00028 typedef vector<string> wordlist;
00029 typedef wordlist alternatives;
00030
00031 class AspellWrapper
00032 {
00033 private:
00034 AspellConfig * config;
00035 AspellSpeller * speller;
00036 public:
00037 string error;
00038 alternatives suggestions;
00039
00040 public:
00041 AspellWrapper( string lang = "en_US" ):config(0),speller(0)
00042 {
00043 AspellCanHaveError * ret = 0;
00044 config = new_aspell_config();
00045 aspell_config_replace(config, "lang", lang.c_str());
00046
00047
00048 ret = new_aspell_speller(config);
00049
00050
00051 const struct AspellError * arc = aspell_error((const AspellCanHaveError *)ret);
00052 if( arc != 0)
00053 {
00054 error = aspell_error_message(ret);
00055 delete_aspell_can_have_error(ret);
00056 }
00057
00058 delete_aspell_config(config);
00059
00060
00061 speller = to_aspell_speller(ret);
00062 }
00063
00064
00065 bool addWords( string path )
00066 {
00067 char buffer[512];
00068 ifstream inFile;
00069 inFile.open(path.c_str());
00070 bool breturn = inFile.is_open();
00071 if ( breturn )
00072 {
00073 do {
00074 inFile.getline (buffer,sizeof(buffer));
00075 if( strlen(buffer) )
00076 {
00077 aspell_speller_add_to_session(speller, buffer, strlen(buffer));
00078 }
00079 } while( !inFile.eof() );
00080 }
00081 }
00082 bool checkWord( string word )
00083 {
00084 int have = 0;
00085 if(speller)
00086 have = aspell_speller_check(speller, word.c_str(), -1);
00087
00088 return have==1;
00089 }
00090
00091 bool checkText( ocString text )
00092 {
00093 bool ret = true;
00094 string tokens=" \t\n\r,.;:!?()[]{}";
00095 text.parseInit();
00096 string word = text.tokenParse(tokens);
00097 while ( word.length() )
00098 {
00099 ret = checkWord(word);
00100 if( ret == false )
00101 break;
00102 word = text.tokenParse(tokens);
00103 }
00104 return ret;
00105 }
00106
00107 bool checkText( ocString text, wordlist & badWords )
00108 {
00109 bool ret = true;
00110
00111 string tokens=" \t\n\r,.;:!?()[]{}";
00112 text.parseInit();
00113 string word = text.tokenParse(tokens);
00114 while ( word.length() )
00115 {
00116
00117 bool tRet = checkWord(word);
00118 if( tRet == false )
00119 {
00120 badWords.push_back(word);
00121 ret = false;
00122 }
00123 word = text.tokenParse(tokens);
00124 }
00125 return ret;
00126 }
00127
00128 alternatives & suggestWords( string word )
00129 {
00130 suggestions.clear();
00131 if(speller)
00132 {
00133 const AspellWordList *wl = aspell_speller_suggest(speller,word.c_str(),-1);
00134 AspellStringEnumeration * els = aspell_word_list_elements(wl);
00135 if( els )
00136 {
00137 const char * chWord;
00138 while ( (chWord = aspell_string_enumeration_next(els)) != 0)
00139 {
00140 string strWord = chWord;
00141 suggestions.push_back(strWord);
00142 }
00143 delete_aspell_string_enumeration(els);
00144 }
00145 }
00146 return suggestions;
00147 }
00148
00149 ~AspellWrapper()
00150 {
00151 if(speller) delete_aspell_speller(speller);
00152 }
00153 };
00154
00155 #endif