Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the following C program using only C language so that the code looks different but still does the same thing. Here's what the program

Rewrite the following C program using only C language so that the code looks different but still does the same thing. Here's what the program does.

. image text in transcribed

Now this is the C code to be rewritten only in C.

#include #include #include #include #include "second.h"

#define MAX_WORD_SIZE 512

/* * Head will be used to store head of trie, LINE_COUNTER stores which line of the map file is being parsed */ struct node *head; int map_line_counter = 1;

int main(int argc, char **argv) { FILE *map_file; char linebuffer[512];

if (argc

/* Pull out file names from line */ char *dictfn = strtok(linebuffer," "); char *datafn = strtok(NULL," ");

head = NULL;

/* Read dict file, add words to trie, close dict file */ dict_file = fopen(dictfn,"r"); if (!dict_file) { printf("invalid input "); return 0; } readDict(dict_file); fclose(dict_file); /* Read data file, which will find all words and increment trie accordingly */ data_file = fopen(datafn,"r"); if (!data_file) { printf("invalid input "); return 0; } readData(data_file); fclose(data_file); /* Print trie to file and free it so we can continue to next file pair */ printResult(); deallocTrie(head); map_line_counter += 1; } fclose(map_file); return 0; }

void readDict(FILE *dict_file) { char c; size_t cindex = 0; char buffer[MAX_WORD_SIZE];

/* Loop through every char in file */ for (;;) { if ( (c = fgetc(dict_file)) == EOF || !isalpha(c) ) { /* We have a non-alpha char, check if we have a word stored in the buffer, if so call function to add it to trie */ if (cindex > 0) { buffer[cindex] = '\0'; addDictWord(buffer); cindex = 0; } /* If we reach EOF we can break, the last word will have already been added */ if (c == EOF) break; } else { /* We have alphabetic char, get its lowercase val and add it to word buffer */ buffer[cindex++] = tolower(c); } } }

void readData(FILE *data_file) { char c; size_t cindex = 0; char buffer[MAX_WORD_SIZE];

/* Same thing as readDict, except this time we call matchStr instead of addDictWord */ for (;;) { if ( (c = fgetc(data_file)) == EOF || !isalpha(c) ) { if (cindex > 0) { buffer[cindex] = '\0'; matchStr(buffer); cindex = 0; } if (c == EOF) break; } else { buffer[cindex++] = tolower(c); } } }

struct node *createNode(void) { struct node *nNode = (struct node*) malloc(sizeof(struct node)); int x;

nNode->alpha = -1; nNode->wordcount = 0; nNode->word = NULL;

/* Set all children to 0, so we can check if they don't exist without having to malloc space for 26 new nodes every time we have a new node */ for (x = 0; x children[x] = 0; } return nNode; }

void addDictWord(char *word) { int x; struct node *ptr; int wordc = strlen(word);

if (!head) head = createNode();

/* Traverse tree, adding nodes if needed, until we finish the word */ ptr = head; for (x = 0; x

if (!ptr->children[curc_a]) { ptr->children[curc_a] = createNode(); ptr->children[curc_a]->alpha = curc_a; } ptr = ptr->children[curc_a]; } /* Set word member for the final node */ ptr->word = (char*) malloc(sizeof(char) * (wordc+1)); for (x = 0; x word[x] = word[x]; } ptr->word[wordc] = '\0'; }

void matchStr(char *str) { int x; struct node *ptr; int strc = strlen(str);

if (!head) return;

ptr = head;

for (x = 0; x children[curc_a])) return; } ptr->wordcount += 1; }

void printResult() { FILE *out_file; char out_filename[64]; sprintf(out_filename, "out%d.txt", map_line_counter); out_file = fopen(out_filename,"w"); if (!out_file) { printf("error "); return; }

traverseWrite(out_file,head,0); fclose(out_file); }

void traverseWrite(FILE *out_file, struct node *ptr, int parent_words) { int i;

if (!ptr) return;

if (ptr->word) fprintf(out_file,"%s %d %d ",ptr->word,ptr->wordcount,parent_words); parent_words += ptr->wordcount;

for (i = 0; i children[i],parent_words); } }

void deallocTrie(struct node *ptr) { int x;

if (!ptr) return;

for (x = 0; x children[x]); } if (ptr->word) free(ptr->word);

free(ptr); }

Your task in this second part is to write a C program called second that reads a input file, which contains a list of dictionary and data file pairs, and generates statistics for each pair. Each line in your input file contains the names of the dictionary and data files. You have to read the dictionary and data files and generate the following statistics: 1. For every word u in the diction file, count the number of words w' that occur in the data file such that w w. 2. For every word w in the dictionary file, count the number of words w' that occur in the data file such that w' is a proper prefix of w. Write all the unique words in the dictionary along with these counts to the output file in lexicographical (i.e. alphabetical) order. A word is define the same way as in the first part. Case-insensitivity also applies when matching prefixes: for example, both "Boo" and "boo" are proper prefixes of "book", which itself is a proper prefix of "bookING" (See the example below). As an example, suppose the content of the dictionary file is: boo22sBook5555bookiNg#boo#Tex123tEXT(JOHN) and that of the data file is: JohnlTEXAN4isalBOORiSH%whohasa2bo3KING BOOKING bookIngssi2for a TEX-Text (BOOKS (textBooKS) Then, the various counts for the unique words in the dictionary file are: Unique words No. of occurrences. No. of prefixes boo book booking text text john

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

1. Explain the 2nd world war. 2. Who is the father of history?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago