-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell_checker.c
617 lines (492 loc) · 16.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#ifdef __linux
#define stricmp strcasecmp
#endif
// #define ACCESSCOUNT_DEBUG // Prints the hit counts of the words. The more hits a word has, the earlier it will be checked.
#define PRINT_DICTIONARY
// How much to expand the arraylist when the allocated size becomes full
#define SIZE_MULTIPLIER 2
// Average length of the words. Will be used to determine initial buffer size.
#define AVG_WORD_LEN 6
// ARRAYLIST PROTOTYPES
typedef struct {
char** words;
int* accessCount; // hit counts of each word
int wordCount; // total number of words in the list
int size; // total size available (allocated but not used)
} arrayList;
arrayList* arrayListCreate(int);
void arrayListAdd(arrayList*, char*);
void arrayListPrintHorizontal(arrayList*);
int partition(arrayList*, int, int);
void arrayListQuickSort(arrayList*, int, int);
bool arrayListBinarySearch(arrayList*, int, int, char*);
void arrayListPrintMostAccessed(arrayList*);
void arrayListTouchWord(arrayList*, int);
void arrayListSwapElements(arrayList*, int, int);
// PROGRAM PROTOTYPES
void openFiles(FILE**, FILE**, const char*, const char*);
char* readWordFromFile(FILE*);
void fillDictionary(arrayList***, FILE*);
int indexOfChar(char);
void printDictionary(arrayList**);
void quickSortDictionary(arrayList**);
void checkTextFile(FILE*, arrayList**);
bool checkWord(char*, arrayList**);
/*
* __________________
* PROGRAM FUNCTIONS
* __________________
*
*/
int main(int argc, char** argv) {
if(argc < 3) {
printf("Not enough argument.\n"
"Usage: %s <dictionary file> <file to be checked>\n", argv[0]);
return 0;
}
FILE* fpDictionary;
FILE* fpText;
openFiles(&fpDictionary, &fpText, argv[1], argv[2]);
arrayList** dictionary = (arrayList**) malloc( sizeof(arrayList*) * 26 );
if(dictionary == NULL) {
printf("Memory allocation error while creating dictionary.\n");
return 0;
}
fillDictionary(&dictionary, fpDictionary);
#if defined PRINT_DICTIONARY
printf("\nDICTIONARY BEFORE SORTING:\n");
printDictionary(dictionary);
#endif
quickSortDictionary(dictionary);
#if defined PRINT_DICTIONARY
printf("\nDICTIONARY AFTER SORTING: \n");
printDictionary(dictionary);
#endif
printf("\nTEST RESULTS: \n");
checkTextFile(fpText, dictionary);
#if defined PRINT_DICTIONARY
printf("\nUPDATED VERSION OF DICTIONARY (Words are sorted realtime according to their hit counts):\n");
printDictionary(dictionary);
#endif
return 0;
}
/*
* FUNCTION: checkTextFile
* @param1 fp: Pointer to the file that will be checked for misspellings.
* @param2 dictionary: Pointer to the dictionary
* @returns nothing
*
* INFO: Checks if a file has incorrect (misspelled) words in it using a dictionary.
* Updates the dictionary so that the most used words will be checked first.
* Prints the results to the standard output.
*/
void checkTextFile(FILE* fp, arrayList** dictionary) {
char* word;
int counter=1;
while(!feof(fp)) {
word = readWordFromFile(fp);
if(strlen(word) > 0) {
if(checkWord(word, dictionary) == false) {
#ifdef __linux
printf("Incorrect word detected at %d. word in the file:\x1B[31m %s\n\x1B[0m",counter,word);
#elif
printf("--!--Incorrect word detected at %d. word in the file: %s\n",counter,word);
#endif
}
}
counter++;
}
printf("File check is completed.\n");
}
/*
* FUNCTION: checkWord
* @param1 word: Pointer to the string to be checked
* @param2 dictionary: Pointer to the dictionary
* @returns true if the word is found in the dictionary, false otherwise
*
* INFO: Checks whether a word is in the dictionary (i.e. a valid word).
*/
bool checkWord(char* word, arrayList** dictionary) {
arrayList* list = dictionary[ indexOfChar(word[0]) ];
if(list == NULL) {
return false;
}
// First search the word in the most accessed words.
int i=0;
while( (i < list->wordCount) && (list->accessCount[i] != 0) ) {
if(stricmp(list->words[i], word) == 0) {
arrayListTouchWord(list, i); // Increment the access count for the word.
#ifdef ACCESSCOUNT_DEBUG
printf("Word \"%s\" is a hit. Most accessed words beginning with letter '%c' are now: \n",word, word[0]);
arrayListPrintMostAccessed(list);
#endif
return true;
}
i++;
}
// End of the list, but no match
if(i == list->wordCount) {
return false;
}
// Else; list still has words with 0 access counts:
// Search the rest of the list using binary search algorithm
return arrayListBinarySearch(list, i, list->wordCount - 1, word);
}
/*
* FUNCTION: quickSortDictionary
* @param1 dictionary: Pointer to the dictionary
* @returns nothing
*
* INFO: Sorts the entire dictionary in an alphabetical order (A->Z)
* using quicksort algorithm. (case-insensitive)
*/
void quickSortDictionary(arrayList** dictionary) {
int i;
for(i=0; i < 26; i++) {
if(dictionary[i] != NULL) {
arrayListQuickSort(dictionary[i], 0, dictionary[i]->wordCount - 1);
}
}
}
/*
* FUNCTION: printDictionary
* @param1 dictionary: Pointer to the dictionary
* @returns nothing
*
* INFO: Prints the content of the dictionary to the standard output.
*/
void printDictionary(arrayList** dictionary) {
int i;
for(i=0; i < 26; i++) {
if(dictionary[i] != NULL) {
arrayListPrintHorizontal(dictionary[i]);
}
}
}
/*
* FUNCTION: fillDictionary
* @param1 dictionary: Address of the dictionary to be filled with words
* @param2 fp: Pointer to the file to read words from
* @returns nothing (@param1 is used as the return parameter)
*
* INFO: Reads the given file and fills the dictionary list.
*/
void fillDictionary(arrayList*** dictionary, FILE* fp) {
while(!feof(fp)) {
char* line = readWordFromFile(fp);
if(strlen(line) > 0) {
int index = indexOfChar(line[0]);
// If there is no arraylist created for this word's first letter yet, create it.
if((*dictionary)[ index ] == NULL) {
(*dictionary)[ index ] = arrayListCreate(2);
}
// Add the word to the list
arrayListAdd((*dictionary)[ index ], line);
}
}
}
/*
* FUNCTION: indexOfChar
* @param1 ch: The character
* @returns the given character's index
*
* INFO: Finds the given character's alphabetical order,
* starting from a/A = 0 (case-insensitive)
*/
int indexOfChar(char ch) {
// if char is a lowercase letter
if(ch >= 'a') {
return ch - 'a';
}
// if char is an uppercase letter
return ch - 'A';
}
/*
* FUNCTION: openFiles
* @param1 fpDictionary: Address of the file pointer to be assigned as the dictionary
* @param2 fpText: Address of the file pointer to be assigned as the text
* @param3 fnDictionary: Path of the dictionary file
* @param4 fnText: Path of the text file
* @returns nothing (@param1 and @param2 are used as the return parameters)
*
* INFO: Opens necessary files and assigns their addresses to
* respective pointers while checking for errors.
*/
void openFiles(FILE** fpDictionary, FILE** fpText, const char* fnDictionary, const char* fnText) {
*fpDictionary = fopen(fnDictionary, "r");
if(*fpDictionary == NULL) {
printf("Error reading file: %s\n", fnDictionary);
exit(0);
}
*fpText = fopen(fnText, "r");
if(*fpText == NULL) {
printf("Error reading file: %s\n", fnText);
exit(0);
}
}
/*
* FUNCTION: readWordFromFile
* @param1 fp: Pointer of the file to be read
* @returns the pointer to the scanned string
*
* INFO: Securely reads a single word that is seperated by an
* empty space or a newline from the given file without
* causing buffer overflow.
*/
char* readWordFromFile(FILE* fp) {
char* buffer = (char*) malloc( sizeof(char) * AVG_WORD_LEN);
if(buffer == NULL) {
printf("Memory allocation error while reading a file stream.\n");
exit(0);
}
buffer[0] = 0;
int currentMax = AVG_WORD_LEN;
int currentLen = 0;
char ch = fgetc(fp); // Read first char
// Read the stream char by char until the end of line or until the end of file.
while( (ch != '\n') && (ch != EOF) && (ch != 13) && (ch != ' ')) {
// If the allocated space for the buffer is full, expand it.
if(currentLen == currentMax) {
currentMax *= 1.5;
buffer = (char*) realloc(buffer, sizeof(char) * currentMax);
if(buffer == NULL) {
printf("Memory allocation error while reading a file stream. "
"(Couldn't extend buffer to %d bytes)\n", currentMax);
exit(0);
}
}
// Push the char to the buffer
buffer[currentLen] = ch;
currentLen++;
// Read next char
ch = fgetc(fp);
}
// Terminate the string with string terminator.
if(currentLen == currentMax) {
currentMax++;
buffer = (char*) realloc(buffer, sizeof(char) * currentMax);
if(buffer == NULL) {
printf("Memory allocation error while reading a file stream. "
"(Couldn't extend buffer to %d bytes)\n", currentMax);
exit(0);
}
}
buffer[currentLen] = 0; // 0 = string terminator
return buffer;
}
/*
* ____________________
* ARRAYLIST FUNCTIONS
* ____________________
*
*/
/*
* FUNCTION: arrayListCreate
* @param1 initialSize: Initial size to be allocated
* @returns arrayList pointer
*
* INFO: Creates an arraylist struct and returns its pointer.
*/
arrayList* arrayListCreate(int initialSize) {
arrayList* list = (arrayList*) malloc( sizeof(arrayList) );
if(list == NULL) {
printf("Memory allocation error while creating an arraylist\n");
exit(0);
}
list->words = (char**) malloc( sizeof(char*) * initialSize);
if(list->words == NULL) {
printf("Memory allocation error while creating an arraylist.\n");
exit(0);
}
list->accessCount = (int*) malloc( sizeof(int) * initialSize);
if(list->accessCount == NULL) {
printf("Memory allocation error while creating an arraylist.\n");
exit(0);
}
list->size = initialSize;
list->wordCount = 0;
return list;
}
/*
* FUNCTION: arrayListAdd
* @param1 list: Pointer of the arraylist to add to
* @param2 word: Pointer of the char array to be added
* @returns nothing
*
* INFO: Adds the given word to the given list.
*/
void arrayListAdd(arrayList* list, char* word) {
// If the allocated size is full, expand it.
if(list->wordCount == list->size) {
int newSize = list->size * SIZE_MULTIPLIER;
list->size = newSize;
list->words = (char**) realloc(list->words, sizeof(char*) * newSize);
if(list->words == NULL) {
printf("Memory allocation error while adding an element to the arraylist."
"(Couldn't extend the size of the arraylist to %d bytes)", newSize);
exit(0);
}
list->accessCount = (int*) realloc(list->accessCount, sizeof(int) * newSize);
if(list->accessCount == NULL) {
printf("Memory allocation error while adding an element to the arraylist."
"(Couldn't extend the size of the arraylist to %d bytes)", newSize);
exit(0);
}
}
// Place the word at the end of the list.
list->words[list->wordCount] = word;
list->accessCount[list->wordCount] = 0;
list->wordCount++;
}
/*
* FUNCTION: arrayListPrintHorizontal
* @param1 list: Pointer of the arraylist to be printed
* @returns nothing
*
* INFO: Prints the content of the given arraylist to
* the standard output horizontally
*/
void arrayListPrintHorizontal(arrayList* list) {
if(list->wordCount > 0) {
printf("%s", list->words[0]);
int i;
for(i=1; i < list->wordCount; i++) {
printf(" - %s", list->words[i]);
}
printf("\n");
}
}
/*
* FUNCTION: arrayListPrintMostAccessed
* @param1 list: Pointer of the arraylist to be printed
* @returns nothing
*
* INFO: Prints the most accessed words in a given arraylist in a descending order.
* Words with 0 access will not be printed.
*/
void arrayListPrintMostAccessed(arrayList* list) {
if(list->accessCount[0] != 0) {
printf("%s(%d)", list->words[0], list->accessCount[0]);
}
int i=1;
while(list->accessCount[i] != 0) {
printf(" - %s(%d)", list->words[i], list->accessCount[i]);
i++;
}
printf("\n");
}
/*
* FUNCTION: arrayListTouchWord
* @param1 list: Pointer to the arraylist
* @param2 index: Index of the element in the arraylist that will be updated
* @returns nothing
*
* INFO: Increments the access count of the word with the given index and
* updates the position of the word based on its access count.
*/
void arrayListTouchWord(arrayList* list, int index) {
list->accessCount[index]++; // Increment the access count.
// As long as the element has a larger access count than the leftside elements,
// swap the elements until it is correctly placed
while( (index>0) && (list->accessCount[index] > list->accessCount[index-1]) ) {
arrayListSwapElements(list, index, index-1);
index--;
}
}
/*
* FUNCTION: arrayListSwapElements
* @param1 list: Pointer to the arraylist
* @param2 i: Index of the first element that is wanted to be swapped
* @param3 j: Index of the second element that is wanted to be swapped
* @returns nothing
*
* INFO: Swaps the locations of the given 2 elements in the arraylist.
*/
void arrayListSwapElements(arrayList* list, int i, int j) {
char* strTemp = list->words[i];
int iTemp = list->accessCount[i];
list->words[i] = list->words[j];
list->accessCount[i] = list->accessCount[j];
list->words[j] = strTemp;
list->accessCount[j] = iTemp;
}
/*
* FUNCTION: arrayListBinarySearch
* @param1 list: Pointer to the arraylist
* @param2 left: Leftmost index of the arraylist
* @param3 right: Rightmost index of the arraylist
* @param3 key: The string to search for
* @returns true if the key is found in the arraylist, false otherwise
*
* INFO: Searches a word in the arraylist using binary search algorithm.
*/
bool arrayListBinarySearch(arrayList* list, int left, int right, char* key) {
if(left>right) {
return false;
}
int middle = (left+right)/2;
if(stricmp(list->words[middle], key) == 0) {
arrayListTouchWord(list, middle); // Increment the access count for the word.
#ifdef ACCESSCOUNT_DEBUG
printf("Word \"%s\" is a hit. Most accessed words beginning with letter '%c' are now: \n",key, key[0]);
arrayListPrintMostAccessed(list);
#endif
return true;
}
if(stricmp(list->words[middle], key) < 0) {
return arrayListBinarySearch(list, middle+1, right, key);
}
return arrayListBinarySearch(list, left, middle-1, key);
}
/*
* FUNCTION: arrayListQuickSort
* @param1 list: Pointer of the arraylist to be sorted
* @param2 left: Leftmost index of the array
* @param3 right: Rightmost index of the array
* @returns nothing
*
* INFO: Sorts the array in an alphabetical order (A->Z)
* using quicksort algorithm. (case-insensitive)
*/
void arrayListQuickSort(arrayList* list, int left, int right) {
if(left<right) {
int pivot = partition(list, left, right);
arrayListQuickSort(list, left, pivot-1);
arrayListQuickSort(list, pivot+1, right);
}
}
/*
* FUNCTION: partition
* @param1 list: Pointer of the arraylist to be partitioned
* @param2 left: Leftmost index of the array
* @param3 right: Rightmost index of the array
* @returns the pivot (middle element)
*
* INFO: Selects the leftmost element as pivot and places it at its
* correct position. All smaller elements are placed to left of
* the pivot and all greater elements are placed to the right of
* the pivot.
*/
int partition(arrayList* list, int left, int right) {
int pivot = left;
int i = left;
int j = right;
do {
do {
i++;
} while( i<j && ( stricmp(list->words[i], list->words[pivot]) < 0 ) );
while( ( stricmp(list->words[pivot], list->words[j]) < 0 ) ) {
j--;
}
if(i<j) {
arrayListSwapElements(list, i, j);
j--;
}
} while(i<j);
arrayListSwapElements(list, left, j);
return j;
}