-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspell_checker.c
345 lines (298 loc) · 10.2 KB
/
spell_checker.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
struct TreeNode
{
char *word;
char *meaning;
char *grammar;
struct TreeNode *left;
struct TreeNode *right;
};
void toLowercase(char *str)
{
for (int i = 0; str[i]; i++)
{
str[i] = tolower((unsigned char)str[i]);
}
}
void removePunctuation(char *str)
{
int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++)
{
if (ispunct(str[i]))
continue;
str[j++] = str[i];
}
str[j] = '\0';
}
struct TreeNode *createNode(char *word, char *meaning, char *grammar)
{
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
newNode->word = (word != NULL) ? strdup(word) : NULL;
newNode->meaning = (meaning != NULL) ? strdup(meaning) : NULL;
newNode->grammar = (grammar != NULL) ? strdup(grammar) : NULL;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct TreeNode *insertCaseInsensitive(struct TreeNode *root, char *word, char *meaning, char *grammar)
{
toLowercase(word);
char *cleanedWord = strdup(word);
removePunctuation(cleanedWord);
if (root == NULL)
return createNode(cleanedWord, meaning, grammar);
int cmp = strcasecmp(cleanedWord, root->word);
if (cmp < 0)
root->left = insertCaseInsensitive(root->left, cleanedWord, meaning, grammar);
else if (cmp > 0)
root->right = insertCaseInsensitive(root->right, cleanedWord, meaning, grammar);
return root;
}
void readCsvFile(char filename, char *words, char *meanings, char **grammars, int *index)
{
FILE *csvFile = fopen(filename, "r");
if (csvFile == NULL)
{
perror("Error opening CSV file");
return;
}
char line[1000];
int lineCounter = 0;
while (fgets(line, sizeof(line), csvFile) != NULL)
{
line[strcspn(line, "\n")] = '\0';
lineCounter++;
char *word = strtok(line, ",");
char *grammar = strtok(NULL, ",");
char *meaning = strtok(NULL, ",");
if (word != NULL)
{
toLowercase(word);
char *cleanedWord = strdup(word);
removePunctuation(cleanedWord);
(*words)[*index] = cleanedWord;
(*meanings)[*index] = (meaning != NULL) ? strdup(meaning) : NULL;
(*grammars)[*index] = (grammar != NULL) ? strdup(grammar) : NULL;
(*index)++;
}
else
{
continue;
}
}
fclose(csvFile);
}
struct TreeNode *createBalancedBST(char **words, char **meanings, char **grammars, int start, int end)
{
if (start > end)
{
return NULL;
}
int mid = (start + end) / 2;
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
newNode->word = strdup(words[mid]);
newNode->meaning = (meanings[mid] != NULL) ? strdup(meanings[mid]) : NULL;
newNode->grammar = (grammars[mid] != NULL) ? strdup(grammars[mid]) : NULL;
newNode->left = createBalancedBST(words, meanings, grammars, start, mid - 1);
newNode->right = createBalancedBST(words, meanings, grammars, mid + 1, end);
return newNode;
}
void findSmallestAndLargest(struct TreeNode *node, char **smallest, char **largest)
{
if (node == NULL)
{
return;
}
if (*smallest == NULL || strcmp(node->word, *smallest) < 0)
{
*smallest = node->word;
}
if (*largest == NULL || strcmp(node->word, *largest) > 0)
{
*largest = node->word;
}
findSmallestAndLargest(node->left, smallest, largest);
findSmallestAndLargest(node->right, smallest, largest);
}
void printSmallestAndLargest(struct TreeNode *root)
{
if (root == NULL)
{
printf("The tree is empty.\n");
return;
}
char *smallestLeft = NULL;
char *largestLeft = NULL;
char *smallestRight = NULL;
char *largestRight = NULL;
char *largestLeftRight = NULL;
char *smallestLeftRight = NULL;
char *largestRightRight = NULL;
char *smallestRightLeft = NULL;
printf("\n\t\t\t\t\t %s\n", root->word);
findSmallestAndLargest(root, &smallestLeft, &largestRight);
printf("\t\t\t\t (%s---%s)\n", (smallestLeft != NULL) ? smallestLeft : "N/A", (largestRight != NULL) ? largestRight : "N/A");
printf("\t\t\t\t\t /\t \\ \n");
printf("\t\t\t\t %s\t %s\n", root->left->word, root->right->word);
findSmallestAndLargest(root->left, &smallestLeft, &largestLeft);
findSmallestAndLargest(root->right, &smallestRight, &largestRight);
printf("\t\t\t (%s---%s) (%s---%s)\n", (smallestLeft != NULL) ? smallestLeft : "N/A", (largestLeft != NULL) ? largestLeft : "N/A", (smallestRight != NULL) ? smallestRight : "N/A", (largestRight != NULL) ? largestRight : "N/A");
printf("\t\t\t /\t \\\t /\t \\\n");
printf("\t\t\t %s\t %s %s\t %s\n", root->left->left->word, root->left->right->word, root->right->left->word, root->right->right->word);
findSmallestAndLargest(root->left->left, &smallestLeft, &largestLeftRight);
findSmallestAndLargest(root->left->right, &smallestLeftRight, &largestLeft);
findSmallestAndLargest(root->right->left, &smallestRight, &largestRightRight);
findSmallestAndLargest(root->right->right, &smallestRightLeft, &largestRight);
printf("\t\t\t (%s- (%s- (%s- (%s-\n", (smallestLeft != NULL) ? smallestLeft : "N/A", (smallestLeftRight != NULL) ? smallestLeftRight : "N/A", (smallestRight != NULL) ? smallestRight : "N/A", (smallestRightLeft != NULL) ? smallestRightLeft : "N/A");
printf("\t\t\t -%s) -%s) -%s) -%s)\n\n", (largestLeftRight != NULL) ? largestLeftRight : "N/A", (largestLeft != NULL) ? largestLeft : "N/A", (largestRightRight != NULL) ? largestRightRight : "N/A", (largestRight != NULL) ? largestRight : "N/A");
}
void freeArray(char **array, int size)
{
for (int i = 0; i < size; i++)
{
free(array[i]);
}
free(array);
}
bool searchAndGetMeaningCaseInsensitive(struct TreeNode *root, char *word, char **meaning, char **grammar)
{
if (root == NULL)
{
return false;
}
int cmp = strcasecmp(word, root->word);
if (cmp < 0)
{
return searchAndGetMeaningCaseInsensitive(root->left, word, meaning, grammar);
}
else if (cmp > 0)
{
return searchAndGetMeaningCaseInsensitive(root->right, word, meaning, grammar);
}
else
{
*meaning = (root->meaning != NULL) ? root->meaning : "The meaning of the word is unknown as the word was added to the database by the user.";
*grammar = (root->grammar != NULL) ? root->grammar : "The grammar of the word is unknown as the word was added to the database by the user.";
return true;
}
}
void checkSpellingOfSentence(struct TreeNode *root, char *sentence)
{
char *token = strtok(sentence, " ");
while (token != NULL)
{
removePunctuation(token);
char *meaning;
char *grammar;
if (searchAndGetMeaningCaseInsensitive(root, token, &meaning, &grammar))
{
printf("The word '%s' is spelled correctly.\n", token);
printf("Grammar: %s\n", grammar);
printf("Meaning: %s\n", meaning);
}
else
{
printf("The word '%s' is not found/spelled correctly.\n", token);
printf("Do you want to add this word to the database? (y/n): ");
char addWordChoice;
scanf(" %c", &addWordChoice);
if (addWordChoice == 'y' || addWordChoice == 'Y')
{
root = insertCaseInsensitive(root, token, NULL, NULL);
printf("The word has been added to the database.\n");
}
}
token = strtok(NULL, " ");
}
}
void inOrderTraversal(struct TreeNode *root, FILE *csvFile)
{
if (root == NULL)
{
return;
}
inOrderTraversal(root->left, csvFile);
if (root->word != NULL)
{
fprintf(csvFile, "%s,", root->word);
}
if (root->grammar != NULL)
{
fprintf(csvFile, "%s,", root->grammar);
}
else
{
fprintf(csvFile, ",");
}
if (root->meaning != NULL)
{
char *meaningCopy = strdup(root->meaning);
char *pos;
while ((pos = strchr(meaningCopy, ',')) != NULL)
{
*pos = ';';
}
fprintf(csvFile, "%s", meaningCopy);
free(meaningCopy);
}
else
{
fprintf(csvFile, ",");
}
fprintf(csvFile, "\n");
inOrderTraversal(root->right, csvFile);
}
void writeCsvFile(struct TreeNode *root, char *filename)
{
FILE *csvFile = fopen(filename, "w");
if (csvFile == NULL)
{
perror("Error opening CSV file for writing");
return;
}
inOrderTraversal(root, csvFile);
fclose(csvFile);
}
void freeTree(struct TreeNode *root)
{
if (root == NULL)
return;
freeTree(root->left);
freeTree(root->right);
free(root->word);
free(root->meaning);
free(root->grammar);
free(root);
}
int main()
{
int index = 0;
char **words = malloc(sizeof(char *) * 5000);
char **meanings = malloc(sizeof(char *) * 5000);
char **grammars = malloc(sizeof(char *) * 5000);
readCsvFile("word.csv", &words, &meanings, &grammars, &index);
struct TreeNode *root = createBalancedBST(words, meanings, grammars, 0, index - 1);
printSmallestAndLargest(root);
freeArray(words, index);
freeArray(meanings, index);
freeArray(grammars, index);
while (1)
{
char inputSentence[256];
printf("Enter a word or sentence to check its spelling (or 'q' to quit): ");
scanf(" %[^\n]%*c", inputSentence);
if (strcmp(inputSentence, "q") == 0)
{
break;
}
checkSpellingOfSentence(root, inputSentence);
}
writeCsvFile(root, "word.csv");
freeTree(root);
return 0;
}