Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Don't use extra programs to do it, such as *stopwords[]) myword.h #ifndef MYWORD_H #define MYWORD_H #define MAX_WORD 30 #define MAX_LINE_LEN 1000 #define MAX_WORDS 1000 typedef

(Don't use extra programs to do it, such as *stopwords[])

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

myword.h

#ifndef MYWORD_H #define MYWORD_H #define MAX_WORD 30 #define MAX_LINE_LEN 1000 #define MAX_WORDS 1000 typedef struct word { char word[MAX_WORD]; int frequency; } WORD; typedef struct words { WORD word_array[MAX_WORDS]; int line_count; int word_count; int distinct_keyword_count; } WORDINFO; int process_word(char *filename, WORDINFO *wip); int save_to_file(char *filename, WORDINFO *wip); #endif 

myword.c

// (Don't use extra programs to do it, such as *stopwords[])

#include #include #include "mystring.h" #include "myword.h" int process_word(char *infilename, WORDINFO *wip) { const char delimiters[] = " .,;:!()&?- \t \"\'"; // your implementation } int save_to_file(char *outfilename, WORDINFO *wip) { // your implementation }

myword_main.c

#include

#include

#include "mystring.h"

#include "myword.h"

void display_word_summary(WORDINFO *wip) {

printf("%s:%d ", "line count", wip->line_count);

printf("%s:%d ", "word count", wip->word_count);

printf("%s:%d ", "distinct word count", wip->distinct_word_count);

int i;

for (i = 0; i distinct_word_count; i++) {

printf("%s:%d ", wip->word_array[i].word, wip->word_array[i].frequency);

}

}

int main(int argc, char *args[]) {

char infilename[40] = "textdata.txt"; //default input file name

char outfilename[40] = "result.txt"; //default output file name

if (argc > 1) {

if (argc >= 2) strcpy(infilename, args[1]);

if (argc >= 3) strcpy(outfilename, args[2]);

}

WORDINFO wordinfo = {0};

process_word(infilename, &wordinfo);

printf("word processing:done ");

display_word_summary(&wordinfo);

save_to_file(outfilename, &wordinfo);

printf("saving result to file:done ");

return 0;

}

mystring.h

#ifndef MYSTRING_H

#define MYSTRING_H

int str_length(char*);

int word_count(char*);

void lower_case(char*);

void trim(char*);

#endif

Develop a C program which reads and processes an input text file like this textdata.txt. It retrieves the following information. I number of lines I number of words I number of distinct words I words and their frequencies The program outputs the retrieved information to a file like this result.txt. Specifically, write C programs myword.h containing the following structure definitions and function headers, and myword.c containing the implementations of the functions. It is required to use your trim() and lower_case() functions from Q1, and it is allowed to use other string functions such as strcat() and strstr() from string library string.h 1. structure definitions: typedef struct word { char word [ 30 ] ; int frequency; } WORD; typedef struct words { WORD word_array[1000); int line_count; int word_count; int distinct_word_count; } WORDINFO; 2. int process_word (char *filename, WORDINFO *wordinfo) opens and reads text file of name passed by *filename line by line. For each line, it gets each word, check if it is already in array wordinfo->word_array, if yes, increases its frequency by 1, otherwise inserts it to the end of the word_array and set its frequency 1, and update word_count and distinct_word_count. 3. int save_to_file(char *filename, WORDINFO *wordinfo) saves the data of WORDINFO words to file of name passed by filename in specified format. Use the provided main function file myword main.c to test your programs as the following. Public test gcc mystring.c myword.c myword_main.c -o q2 q2 word processing:done line count:2 word count:10 distinct word count:6 this: 2 is:2 the:2 first:1 test:2 second:1 saving result to file:done Use mystring.h for lower_case(), and trin() functions. Use string.h for strcmp() and strcpy() functions == = = int process_word(char *filename, WORDINFO *wip) {const char delimiters[] =" .,;:!()&?- \t \"\"; char line[MAX_LINE_LEN]; char *word_token = NULL; int j; FILE *fp = fopen(filename, "r"); while (fgets(line, MAX_LINE_LEN, fp) != NULL) { // traversing all lines wip->line_count++; lower_case(line); // your function in mystring.c trim(line); // your function in mytring.c word_token = (char *) strtok(line, delimiters); // get the first word while (word_token != NULL) { // traversing all words in a line || action: insert word_token into data structure wip->word_array[] } word_token = (char*) strtok(NULL, delimiters); // next word } } fclose(fp); } = int save_to_file(char *filename, WORDINFO *wip) { = FILE *fp = fopen(filename, "w"); if (fp == NULL) { perror("file opening error"); return 1; } fprintf(fp,"%s:%d ", "line count", wip->line_count); " = int i; for (i = 0; i distinct_word_count; i++) { = } fclose(fp); return o; }

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

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago