Question
need help with my C code for process_word and save_to_file int process_word(char *filename, WORDSUMMARY *words, char *stopwords[]); this function opens and reads text file of
need help with my C code for process_word and save_to_file
int process_word(char *filename, WORDSUMMARY *words, char *stopwords[]); this function opens and reads text file of name passed by *filename line by line. For each line, it gets each word, if it is not a stop word, check if it is already in array words->word_array, if yes, increases its frequency by 1, otherwise inserts it to the end of the word_array and set its frequency 1. Meantime, it updates the count information.
int save_to_file(char *filename, WORDSUMMARY *words); this saves the data of WORDSUMMARY words to file of name passed by filename in specified format.
code:
#include
#include
#include "myword.h"
#include "mystring.h"
typedef struct word {
char word[30];
int frequency;
} WORD;
typedef struct words {
WORD word_array[1000];
int line_count;
int word_count;
int keyword_count;
} WORDSUMMARY;
void set_stopword(char *filename, char *stopwords[])
{
// your implementation
char line[1000];
char delimiters[] = "., \t ";
char *token;
int i;
FILE *fp = fopen(*filename, "r");
if(fp == NULL)
{
perror("Error opening file");
return 0;
}
while(fgets(line, 1000, fp) != NULL)
{
token = (char*) strtok(line, delimiters);
while(token != NULL)
{
i = (int)(*token - 'a');
strcat(stopwords[i], token);
strcat(stopwords[i], ",");
token = (char*) strtok(NULL, delimiters);
}
}
}
// this function check if the word is contained in directory stopwords[]
// returns 1 if yes, 0 otherwise. It use function str_contain_word()
int contain_word(char *stopwords[], char *word)
{
if(word == NULL || *word == '\0')
return 0;
else {
return str_contain_word(stopwords[*word - 'a'], word);
}
}
// this function check if word is a word in string str,
// returns 1 if yes, 0 otherwise
int str_contain_word(char *str, char *word)
{
if (str == NULL || word == NULL) return 0;
char temp[20] = {0};
strcat(temp, ",");
strcat(temp, word);
strcat(temp, ",");
if(strstr(str, temp))
return 1;
else
return 0;
}
int process_word(char *filename, WORDSUMMARY *words, char *stopwords[])
{
const char delimiters[] = " .,;:!()&?- \t \"\'";
char line[1000];
char buf[128];
char *token;
int frequency;
FILE *fp = fopen(*filename, "r");
while(!feof(fp))
{
if(fgets(buf, sizeof(buf), fp));
printf("%s", buf);
}
while(fgets(line, MAX_LINE_LEN, fp) != NULL)
{
words->line_count++;
lower_case(line);
trim(line);
token = (char*) strtok(line, delimiters);
while (token != NULL)
{
if(contain_word(stopwords, token) == 0)
{
}
token = (char*) strtok(NULL, delimiters);
}
}
}
int save_to_file(char *filename, WORDSUMMARY *words)
{
// your implementation
FILE *fp = fopen(*filename, "r");
fprintf(fp, "%-20s %8d ", "Word count", words->word_count);
fprintf(fp, "%-20s %8d ", "Keyword count", words->keyword_count);
fprintf(fp, "%-18s %10s ", "Keyword", "frequency");
// your implementation
for(int i = 0; i < (*words).keyword_count; i++)
{
fprintf(fp, "%-20s %8d ", words->word_array[i].word, words->word_array[i].frequency);
}
return 1;
}
output should look like this if everything works:
Line count 2 Word count 10 Keyword count 3 Keyword frequency first 1 test 2 second 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started