-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
457 lines (367 loc) · 13.6 KB
/
main.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
#include "hashMap.h"
#include "hashMap.c"
#include <assert.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
# define TYPE const char *
// This is used to store the 5 lowest Levenshtein distances.
struct array {
TYPE data [10];
int count;
};
void initArray(struct array * a) {
a->count = 0;
}
void addToArray(struct array * a, TYPE c) {
a->data[a->count] = c;
a->count++;
}
/**
* This function is used to check if a word already exists in the list of lowest Levenshtein distances.
* @param a array to be checked.
* @param c string to be checked for.
* @return 1 if the array has c, 0 otherwise.
*/
int contains(struct array * a, TYPE c) {
assert(a != 0);
for (int i = 0; i < a->count; i++) {
if(strcmp(c, a->data[i]) == 0) {
return 1;
}
}
return 0;
}
/**
* This function calculates the Levenshtein edit distance between two words.
* @param wordOne
* @param lengthOne
* @param wordTwo
* @param lengthTwo
* @return
*/
int LevenshteinDistance(const char * wordOne, int lengthOne, const char * wordTwo, int lengthTwo) {
int wordLengths[lengthOne + 1][lengthTwo + 1];
int del;
int insert;
int sub;
int min;
int i;
for (i = 0; i <= lengthOne; i++) {
wordLengths[i][0] = i;
}
for (i = 0; i <= lengthTwo; i++) {
wordLengths[0][i] = i;
}
for (i = 1; i <= lengthOne; i++) {
int j;
char charrOne;
charrOne = wordOne[i - 1];
for (j = 1; j <= lengthTwo; j++) {
char charrTwo;
charrTwo = wordTwo[j - 1];
if (charrOne == charrTwo) {
wordLengths[i][j] = wordLengths[i - 1][j - 1];
}
else {
del = wordLengths[i - 1][j] + 1;
insert = wordLengths[i][j - 1] + 1;
sub = wordLengths[i - 1][j - 1] + 1;
min = del;
if (insert < min) {
min = insert;
}
if (sub < min) {
min = sub;
}
wordLengths[i][j] = min;
}
}
}
return wordLengths[lengthOne][lengthTwo];
}
/**
* Allocates a string for the next word in the file and returns it. This string
* is null terminated. Returns NULL after reaching the end of the file.
* @param file
* @return Allocated string or NULL.
*/
char* nextWord(FILE* file) {
int maxLength = 16;
int length = 0;
char* word = malloc(sizeof(char) * maxLength);
while (1)
{
char c = fgetc(file);
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '\'')
{
if (length + 1 >= maxLength)
{
maxLength *= 2;
word = realloc(word, maxLength);
}
word[length] = c;
length++;
}
else if (length > 0 || c == EOF)
{
break;
}
}
if (length == 0)
{
free(word);
return NULL;
}
word[length] = '\0';
return word;
}
/**
* Loads the contents of the file into the hash map.
* @param file
* @param map
*/
void loadDictionary(FILE* file, HashMap* map) {
assert(map != 0);
char * word = nextWord(file);
while (word) {
hashMapPut(map, word, 1);
free(word);
word = nextWord(file);
}
free(word);
}
/**
* Loads the contents of the file into a hash map and sets the value of each element to the Levenshtein Distance
* from the entered word.
* @param file
* @param map
* @param word - everything in the dictionary is compared to this word for Levenshtein Distance.
*/
void loadDictionaryWithLevenshteinDistance(FILE* file, HashMap* map, const char * word) {
assert(map != 0);
const char * newWord = nextWord(file);
int length1 = strlen(newWord);
int length2 = strlen(word);
int distance = LevenshteinDistance(newWord, length1, word, length2);
while (newWord) {
hashMapPut(map, newWord, distance);
// Reset variable
distance = 0;
length1 = 0;
length2 = 0;
//free(newWord);
// Re-assign variable values
newWord = nextWord(file);
if(newWord != NULL) {
length1 += strlen(newWord);
length2 += strlen(word);
distance = LevenshteinDistance(newWord, length1, word, length2);
}
}
}
/**
* Checks the spelling of the word provided by the user. If the word is spelled incorrectly,
* print the 5 closest words as determined by a metric like the Levenshtein distance.
* Otherwise, indicate that the provided word is spelled correctly. Use dictionary.txt to
* create the dictionary.
* @param argc
* @param argv
* @return
*/
int main(int argc, const char** argv) {
HashMap* map = hashMapNew(1000);
FILE* file = fopen("/Users/josh/CLionProjects/CS261Project5_2/dictionary.txt", "r");
clock_t timer = clock();
loadDictionary(file, map);
timer = clock() - timer;
printf("Dictionary loaded in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);
fclose(file);
char inputBuffer[256];
int quit = 0;
while (!quit) {
printf("\nEnter a word or \"quit\" to quit: ");
scanf("%s", inputBuffer);
if (strcmp(inputBuffer, "quit") == 0) {
quit = 1;
break;
}
if(hashMapContainsKey(map, inputBuffer)) {
printf("The inputted word %s is spelled correctly\n", inputBuffer);
} else {
FILE* newFile = fopen("/Users/josh/CLionProjects/CS261Project5_2/dictionary.txt", "r");
// New HashMap with values equal to Levenshtein Distance from entered word.
HashMap * levMap = hashMapNew(1000); // freed
loadDictionaryWithLevenshteinDistance(newFile, levMap, inputBuffer);
fclose(newFile);
// Array used to hold the 5 lowest Levenshtein distance words.
struct array * levArray = (struct array *) malloc(sizeof(struct array) * 5); // freed
initArray(levArray);
// Word & int 1
HashLink * tmp;
tmp = levMap->table[levMap->capacity - 1];
// Gets a HashLink with a word with a Lev distance > 1.
int x = levMap->capacity - 1;
while((tmp == NULL) && (x > 0)) {
tmp = levMap->table[x];
x--;
}
// This Levenshtein distance is compared to every distance in the map, searching for a lower one.
int tempNum = tmp->value;
// Once the lowest Levenshtein distance is found, this word is added to an array of words with the 5 lowest
// distances.
const char * lowestLevenshteinDistanceWord = tmp->key; // first test word
HashLink * curr;
for (int i = 0; i < levMap->capacity; i++) {
curr = levMap->table[i];
while(curr != 0) {
if (curr->value < tempNum) {
tempNum = curr->value;
lowestLevenshteinDistanceWord = curr->key;
// Restart loop to make sure this is the lowest Lev Distance.
i = -1;
break;
} else {
curr = curr->next;
}
}
}
// 1.
// Adds the lowest Levenshtein distance word to the array.
addToArray(levArray, lowestLevenshteinDistanceWord);
// To find the next word with the next lowest Levenshtein distance, it's distance must be equal to or
// greater than the first one.
int baseValue = tempNum;
// Sets tmp to the last word in the map.
tmp = levMap->table[levMap->capacity - 1];
x = levMap->capacity - 1;
while((tmp == NULL) && (x > 0)) {
tmp = levMap->table[x];
x--;
}
tempNum = tmp->value;
lowestLevenshteinDistanceWord = tmp->key;
for (int i = 0; i < levMap->capacity; i++) {
curr = levMap->table[i];
while(curr != 0) {
// If curr's value is lower than tempNum and >= baseValue and curr's key isn't already in the array
if ((curr->value < tempNum) && (curr->value >= baseValue) && (!contains(levArray,curr->key))) {
tempNum = curr->value;
lowestLevenshteinDistanceWord = curr->key;
// Restart loop to make sure this is the lowest Lev Distance.
i = -1;
break;
} else {
curr = curr->next;
}
}
}
// 2.
// Adds the next lowest Levenshtein distance word to the array.
addToArray(levArray, lowestLevenshteinDistanceWord);
// To find the next word with the next lowest Levenshtein distance, it's distance must be equal to or
// greater than the first one.
baseValue = tempNum;
// Sets tmp to the last word in the map.
tmp = levMap->table[levMap->capacity - 1];
x = levMap->capacity - 1;
while((tmp == NULL) && (x > 0)) {
tmp = levMap->table[x];
x--;
}
tempNum = tmp->value;
lowestLevenshteinDistanceWord = tmp->key;
for (int i = 0; i < levMap->capacity; i++) {
curr = levMap->table[i];
while(curr != 0) {
// If curr's value is lower than tempNum and >= baseValue and curr's key isn't already in the array
if ((curr->value < tempNum) && (curr->value >= baseValue) && (!contains(levArray,curr->key))) {
tempNum = curr->value;
lowestLevenshteinDistanceWord = curr->key;
// Restart loop to make sure this is the lowest Lev Distance.
i = -1;
break;
} else {
curr = curr->next;
}
}
}
// 3.
// Adds the next lowest Levenshtein distance word to the array.
addToArray(levArray, lowestLevenshteinDistanceWord);
// To find the next word with the next lowest Levenshtein distance, it's distance must be equal to or
// greater than the first one.
baseValue = tempNum;
// Sets tmp to the last word in the map.
tmp = levMap->table[levMap->capacity - 1];
x = levMap->capacity - 1;
while((tmp == NULL) && (x > 0)) {
tmp = levMap->table[x];
x--;
}
tempNum = tmp->value;
lowestLevenshteinDistanceWord = tmp->key;
for (int i = 0; i < levMap->capacity; i++) {
curr = levMap->table[i];
while(curr != 0) {
// If curr's value is lower than tempNum and >= baseValue and curr's key isn't already in the array
if ((curr->value < tempNum) && (curr->value >= baseValue) && (!contains(levArray,curr->key))) {
tempNum = curr->value;
lowestLevenshteinDistanceWord = curr->key;
// Restart loop to make sure this is the lowest Lev Distance.
i = -1;
break;
} else {
curr = curr->next;
}
}
}
// 4.
// Adds the next lowest Levenshtein distance word to the array.
addToArray(levArray, lowestLevenshteinDistanceWord);
// To find the next word with the next lowest Levenshtein distance, it's distance must be equal to or
// greater than the first one.
baseValue = tempNum;
// Sets tmp to the last word in the map.
tmp = levMap->table[levMap->capacity - 1];
x = levMap->capacity - 1;
while((tmp == NULL) && (x > 0)) {
tmp = levMap->table[x];
x--;
}
tempNum = tmp->value;
lowestLevenshteinDistanceWord = tmp->key;
for (int i = 0; i < levMap->capacity; i++) {
curr = levMap->table[i];
while(curr != 0) {
// If curr's value is lower than tempNum and >= baseValue and curr's key isn't already in the array
if ((curr->value < tempNum) && (curr->value >= baseValue) && (!contains(levArray,curr->key))) {
tempNum = curr->value;
lowestLevenshteinDistanceWord = curr->key;
// Restart loop to make sure this is the lowest Lev Distance.
i = -1;
break;
} else {
curr = curr->next;
}
}
}
// 5.
// Adds the next lowest Levenshtein distance word to the array.
addToArray(levArray, lowestLevenshteinDistanceWord);
printf("The inputted word %s is spelled incorrectly. Did you mean:\n", inputBuffer);
for (int i = 0; i < 5; i++) {
printf("%s\n", levArray->data[i]);
}
hashMapDelete(levMap);
free(levArray);
}
}
hashMapDelete(map);
return 0;
}