-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtext_processor.c
More file actions
120 lines (99 loc) · 3.45 KB
/
text_processor.c
File metadata and controls
120 lines (99 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include "text_processor.h"
// These are defined in text_processor.h, no need to redefine them here
// #define Max_Words 100
// #define Max_Words_Length 50
// #define Max_Filename_Length 100
// Using ignore_words from ignore_words.c
extern const char *ignore_words[];
extern int ignore_words_count;
void convert_lowercase(char *word) {
for (int i = 0; word[i]; i++) {
word[i] = tolower((unsigned char)word[i]);
}
}
int if_ignored(const char *word) {
for (int i = 0; i < ignore_words_count; i++) {
if (strcmp(word, ignore_words[i]) == 0) {
return 1;
}
}
return 0;
}
void write_toCSV(const char *filename, char *text) {
FILE *file = fopen(filename, "w");
if (!file) {
printf("Error opening file %s for writing!\n", filename);
return;
}
char word[Max_Words_Length] = "";
int j = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalnum(text[i])) {
if (j < Max_Words_Length - 1) {
word[j++] = tolower(text[i]);
}
} else if (j > 0) {
word[j] = '\0';
if (!if_ignored(word)) {
fprintf(file, "%s\n", word);
}
j = 0;
}
}
if (j > 0) {
word[j] = '\0';
if (!if_ignored(word)) {
fprintf(file, "%s\n", word);
}
}
fclose(file);
}
bool read_from_file(const char *filename, char *buffer, int buffer_size) {
FILE *file = fopen(filename, "r");
if (!file) {
printf("Error opening file %s for reading!\n", filename);
return false;
}
size_t len = fread(buffer, 1, buffer_size - 1, file);
buffer[len] = '\0';
fclose(file);
return true;
}
void manual_input_mode() {
char reference_input[Max_Words * Max_Words_Length];
char user_input[Max_Words * Max_Words_Length];
printf("Enter the Reference input:\n");
fgets(reference_input, sizeof(reference_input), stdin);
reference_input[strcspn(reference_input, "\n")] = '\0';
printf("Enter the user's answer:\n");
fgets(user_input, sizeof(user_input), stdin);
user_input[strcspn(user_input, "\n")] = '\0';
write_toCSV("reference.csv", reference_input);
write_toCSV("user_input.csv", user_input);
printf("\nCSV files 'reference.csv' and 'user_input.csv' created successfully.\n");
}
void file_input_mode() {
char reference_file[Max_Filename_Length];
char user_file[Max_Filename_Length];
char reference_input[Max_Words * Max_Words_Length];
char user_input[Max_Words * Max_Words_Length];
printf("Enter the filename for the reference text: ");
fgets(reference_file, sizeof(reference_file), stdin);
reference_file[strcspn(reference_file, "\n")] = '\0';
printf("Enter the filename for the user's answer: ");
fgets(user_file, sizeof(user_file), stdin);
user_file[strcspn(user_file, "\n")] = '\0';
if (!read_from_file(reference_file, reference_input, sizeof(reference_input))) {
return;
}
if (!read_from_file(user_file, user_input, sizeof(user_input))) {
return;
}
write_toCSV("reference.csv", reference_input);
write_toCSV("user_input.csv", user_input); printf("\nCSV files 'reference.csv' and 'user_input.csv' created successfully.\n");
}