#include #include #include #include #include #include #include using namespace std; enum TimerAction { TIMER_ACTION_START, TIMER_ACTION_END }; #define TIMER_START() Timer(TIMER_ACTION_START) #define TIMER_END(x,var) do {\ int t = Timer(TIMER_ACTION_END);\ cout << "Time taken for " << x << " : " << t << " us." << endl;\ var.total += t;\ var.count++;\ } while (false) int Timer(TimerAction action) { static struct timeval begin; struct timeval end; long retval; if (action == TIMER_ACTION_START) { retval = gettimeofday(&begin, NULL); } else { gettimeofday(&end, NULL); retval = (end.tv_sec-begin.tv_sec)*1000*1000 + (end.tv_usec-begin.tv_usec); } return retval; /* No. of microseconds since timer was started */ } struct Stat { int total; int count; }; int main() { Stat other = {0,0}; Stat check = {0,0}; Stat sug = {0,0}; /* New config object */ AspellConfig *spell_config = new_aspell_config(); /* Set conf */ aspell_config_replace(spell_config, "lang", "en_US"); aspell_config_replace(spell_config, "sug-mode", "ultra"); /* New speller object */ AspellCanHaveError *possible_err = new_aspell_speller(spell_config); AspellSpeller *spell_checker = NULL; if (aspell_error_number(possible_err) != 0) { cout << "ASPELL Error : " << aspell_error_message(possible_err) << endl; return 1; } else { spell_checker = to_aspell_speller(possible_err); } /* Load the word list from the file */ ifstream wordStream("/home/kevina/aspell-time/spell/words.lst"); if (wordStream.is_open() == false) { cout << "Word list cannot be opened" << endl; return 2; } string aWord; vector wordList; while (wordStream.eof() == false) { wordStream >> aWord; if (wordStream.eof() == false) { wordList.push_back(aWord); } } wordStream.close(); int iteration = 1; do { char input[80]; int randIndex = random() % wordList.size(); aWord = wordList[randIndex]; strcpy(input, aWord.c_str()); int isCorrect; /* Check if the word is correct */ TIMER_START(); isCorrect = aspell_speller_check(spell_checker, input, -1); TIMER_END("checking a word for correct spelling", check); if (isCorrect == 0) { /* Inc(Iteration) if word is misspelt */ iteration++; cout << "\"" << input << "\"" << " is misspelled." << endl; /* Get the suggestion for a misspelt word */ TIMER_START(); const AspellWordList *suggestions = aspell_speller_suggest(spell_checker, input, -1); TIMER_END("getting a list of suggestions from aspell", sug); AspellStringEnumeration *elements = aspell_word_list_elements(suggestions); const char *aSuggestion; int i = 1; while ((aSuggestion = aspell_string_enumeration_next(elements)) != NULL) { if (i == 1) { cout << "Suggestions :-" << endl; cout << "------------ " << endl; } cout << i++ << ". " << aSuggestion << endl; } delete_aspell_string_enumeration(elements); } else { cout << "\"" << input << "\"" << " is correctly spelled." << endl; } cout << "WORD LENGTH : " << strlen(input) << endl; } while (iteration <= 500); /* Delete the object before quitting */ delete_aspell_config(spell_config); delete_aspell_speller(spell_checker); cout << "check avg: " << check.total/check.count << endl; cout << "sug avg: " << sug.total/sug.count << endl; return 0; }