// read word from a file struct wordTuple* ReadWords(const char *filename) { FILE *f = fopen(filename, "rt"); // checking for NULL is boring; i omit it char temp[100]; // assuming the words cannot be too long struct wordTuple *first, *cur, *pre=NULL; pre = NULL; while (1) //scan the file until hit the end-of-file { // Read a word from the file if (fscanf(f, "%s", temp) != 1) break; // note: "!=1" checks for end-of-file; using feof for that is usually a bug remove_punct_and_make_lower_case(temp); cur = (struct wordTuple *) malloc(sizeof(wordTuple)); cur->key = strdup(temp); cur->next = NULL; cur->data = 1; if(pre!=NULL) { pre->next = cur; } else { first=cur; } pre = cur; } fclose(f); return first; }
int main() { FILE *fp, *fpd, *fpo; char c; char buffer[MAXS]; char outbuffer[MAXS2] = ""; char dictionary[DICS]; char* token; double flag; int isreplaced=0; int numcount=1; /* Open file for both reading and writing */ fprintf(stderr, "Opening files ...\n"); fp = fopen("input.txt", "r+"); fpd = fopen("dictionary.txt", "r+"); fpo = fopen("output.txt", "w+"); /* Write data to the file */ //fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ //fseek(fp, SEEK_SET, 0); /* Read and display data */ //fread(buffer, sizeof(buffer)+1, 1, fp); fgets(buffer, sizeof(buffer), fp); remove_newline_ch(buffer); printf("Input paragraph;\n%s\n", buffer); /* while(!feof(fpd)) { fgets(dictionary, sizeof(dictionary), fpd); printf("%s\n", dictionary); }*/ token = strtok(buffer, " "); while (token) { remove_punct_and_make_lower_case(token); // printf("\n\ntoken: %s, lenght: %d\n\n\n", token, strlen(token)); // checking for numbers if(is_valid_int(token)){ printf("\nNumber %d: %s",numcount, token); numcount++; convert_to_words(token); strcat(outbuffer, token); strcat(outbuffer, " "); } else { if(strlen(token) >= 4){ // checking for spelling fseek(fpd, SEEK_SET, 0); isreplaced = 0; while(!feof(fpd)) { flag = 0.0; fgets(dictionary, sizeof(dictionary), fpd); remove_punct_and_make_lower_case(dictionary); remove_newline_ch(dictionary); // printf("%s\n", dictionary); if(strlen(token) >= 4){ flag = is_similar(token, dictionary); /*{ strcat(outbuffer, token); strcat(outbuffer, " "); }if(flag == 0.0){ strcat(outbuffer, token); strcat(outbuffer, " "); } else */ if(isreplaced != 1){ if(flag > 0.6){ strcat(outbuffer, dictionary); strcat(outbuffer, " "); isreplaced = 1; } else if(flag == 1.0){ strcat(outbuffer, token); strcat(outbuffer, " "); isreplaced = 1; } } } } if(isreplaced == 0){ strcat(outbuffer, token); strcat(outbuffer, " "); } } else { strcat(outbuffer, token); strcat(outbuffer, " "); } } token = strtok(NULL, " "); } fwrite(outbuffer, strlen(outbuffer) + 1, 1, fpo); fclose(fp); fclose(fpd); fclose(fpo); return(0); }