struct spelling_document * spelling_document_init(char *lang) { AspellCanHaveError *ret; AspellDocumentChecker *checker; struct spelling *speller; struct spelling_document *sd; sd = malloc(sizeof(*sd)); if (sd == NULL) return NULL; speller = spelling_init(lang); if (speller == NULL) { free(sd); return NULL; } ret = new_aspell_document_checker(speller->speller); if (aspell_error(ret) != 0) { free(sd); spelling_destroy(speller); return NULL; } checker = to_aspell_document_checker(ret); sd->spelling = speller; sd->checker = checker; return sd; }
/** * Generate a document checker object from a given speller. * @param speller the speller that shall chech a document. * @return a fresh document checker. */ static AspellDocumentChecker* get_checker(AspellSpeller *speller) { AspellCanHaveError * ret; AspellDocumentChecker * checker; ret = new_aspell_document_checker(speller); if (aspell_error(ret) != 0) rb_raise(cAspellError, "%s" ,aspell_error_message(ret)); checker = to_aspell_document_checker(ret); return checker; }
void doSpellCheckDoc(GtkWidget* widget,gpointer data) { GtkTextIter start; GtkTextIter end; AspellCanHaveError* ret; AspellDocumentChecker* checker; AspellToken token; int diff; unsigned int goodwordlen; char* word_begin; char* badword; GtkTextIter startiter; GtkTextIter enditer; char* line; pageStruct* page=getPageStructPtr(-1); gtk_text_buffer_get_start_iter((GtkTextBuffer*)page->buffer,&startiter); gtk_text_buffer_get_end_iter((GtkTextBuffer*)page->buffer,&enditer); line=gtk_text_buffer_get_text((GtkTextBuffer*)page->buffer,&startiter,&enditer,false); /* Set up the document checker */ ret=new_aspell_document_checker(spellChecker); if (aspell_error(ret)!=0) { printf("Error: %s\n",aspell_error_message(ret)); return; } checker=to_aspell_document_checker(ret); /* First process the line */ aspell_document_checker_process(checker,line,-1); diff=0; /* Now find the misspellings in the line */ while(token=aspell_document_checker_next_misspelling(checker),token.len!=0) { /* Pay particular attention to how token.offset and diff is used */ asprintf(&badword,"%.*s",token.len,(char*)&line[token.offset+diff]); goodWord=NULL; checkTheWord(badword,1); if(cancelCheck==true) { delete_aspell_document_checker(checker); return; } word_begin=line+token.offset+diff; if(goodWord!=NULL) { goodwordlen=strlen(goodWord); /* Replace the misspelled word with the replacement */ diff+=goodwordlen-token.len; memmove(word_begin+goodwordlen,word_begin+token.len,strlen(word_begin+token.len)+1); memcpy(word_begin,goodWord,goodwordlen); } } delete_aspell_document_checker(checker); gtk_text_buffer_get_bounds((GtkTextBuffer*)page->buffer,&start,&end); gtk_text_buffer_select_range((GtkTextBuffer*)page->buffer,&start,&end); gtk_text_buffer_delete_selection((GtkTextBuffer*)page->buffer,true,true); gtk_text_buffer_get_start_iter((GtkTextBuffer*)page->buffer,&start); gtk_text_buffer_insert((GtkTextBuffer*)page->buffer,&start,line,-1); if(spellCheckWord!=NULL) { gtk_widget_destroy(spellCheckWord); spellCheckWord=NULL; } }
static void check_document(AspellSpeller * speller, const char * filename) { /* For readablity this function does not worry about buffer overrun. This is meant as an illustrative example only. Please do not attent to spell check your docuemnts with this function. */ AspellCanHaveError * ret; AspellDocumentChecker * checker; AspellToken token; FILE * doc, * out; char line[256], repl[256], checked_filename[256]; int diff; unsigned int repl_len; char * word_begin; /* Open the file */ doc = fopen(filename, "r"); if (doc == 0) { printf("Error: Unable to open the file \"%s\" for reading.", filename); return; } /* Open filename.checked for writing the results */ strcpy(checked_filename, filename); strcat(checked_filename, ".checked"); out = fopen(checked_filename, "w"); if (out == 0) { printf("Error: Unable to open the file \"%s\" for writing.", checked_filename); return; } /* Set up the document checker */ ret = new_aspell_document_checker(speller); if (aspell_error(ret) != 0) { printf("Error: %s\n",aspell_error_message(ret)); return; } checker = to_aspell_document_checker(ret); while (fgets(line, 256, doc)) { /* First process the line */ aspell_document_checker_process(checker, line, -1); diff = 0; /* Now find the misspellings in the line */ while (token = aspell_document_checker_next_misspelling(checker), token.len != 0) { /* Print out the misspelling and get a replasment from the user */ /* Pay particular attention to how token.offset and diff is used */ word_begin = line + token.offset + diff; printf("%.*s*%.*s*%s", (int)(token.offset + diff), line, (int)token.len, word_begin, word_begin + token.len); printf("Suggestions: "); print_word_list(speller, aspell_speller_suggest(speller, word_begin, token.len), ' '); printf("\n"); printf("Replacement? "); fgets(repl, 256, stdin); printf("\n"); if (repl[0] == '\n') continue; /* ignore the current misspelling */ repl_len = strlen(repl) - 1; repl[repl_len] = '\0'; /* Replace the misspelled word with the replacement */ diff += repl_len - token.len; memmove(word_begin + repl_len, word_begin + token.len, strlen(word_begin + token.len) + 1); memcpy(word_begin, repl, repl_len); } /* print the line to filename.checked */ fputs(line, out); } delete_aspell_document_checker(checker); printf("Done. Results saved to \"%s\".", checked_filename); }