-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
272 lines (212 loc) · 5.05 KB
/
main.c
File metadata and controls
272 lines (212 loc) · 5.05 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
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
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
//#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hashmap.h"
#include "editDistance.h"
#define BUF_LEN 1057480 // 1M cache
#define ONE_STEP_PRI 1000 // the priority of string 1 step distance away
#define TWO_STEP_PRI 15 // the priority of string 2 step distance away
#define DB_INIT_SIZE 1024 // size of the initial hash map database
#define BUF_INIT_SIZE 8 // size of the buffer hash map
extern int superCheck;
void parse(const char* str, hash_map* map)
{
char* buf = (char*)malloc(sizeof(char)*BUF_LEN);
char* tpStr = 0;
int i,start,end;
// start parsing
i = 0;
strcpy(buf, str);
while( buf[i] )
{
start = i;
while( buf[start] < 65 || (buf[start] > 90 && buf[start] < 97) || buf[start] > 122 )
{
if( !buf[start] )
{
// free the buffer
free(buf);
return; // no word found
}
start++; // find the start of the word
}
end = start;
while( (buf[end] >= 65 && buf[end] <= 90) || (buf[end] >= 97 && buf[end] <= 122) )
{
if(buf[end] >= 65 && buf[end] <= 90)
{
// make all upper case to lower case
buf[end] = buf[end] + 32;
}
end++;
}
// end the word
buf[end] = 0;
tpStr = &buf[start];
putInHashMap(tpStr, 1, map);
i = ++end;
}
// free the buffer
free(buf);
}
void parseDict()
{
char* str = (char*)malloc(sizeof(char)*BUF_LEN);
int i;
// C standard file I/O
FILE* file;
FILE* outFile;
// used to store the words
hash_map map;
initiateHashMap(&map, 32);
if( (file = fopen ("dict.txt", "r")) == NULL )
{
printf("Error reading dictionary file. Exiting program\n");
exit(-1);
}
while( i = fread(str, 1, BUF_LEN - 1, file) )
{
// end the str
str[i] = 0;
// parse the dictionary
parse(str, &map);
}
// free the str
free(str);
fclose(file);
// Now output the hash map
if( (outFile = fopen ("parsed.txt", "w")) == NULL )
{
printf("Can't create parsed file. Exiting program.\n");
exit(-1);
}
for( i = 0; i < map.maxSize; i++ )
{
if( map.table[i] )
fprintf(outFile, "%s %d \n", map.table[i]->str, map.table[i]->value);
}
dismissHashMap(&map);
fclose(outFile);
}
void readDB(hash_map* map, FILE* dbFile)
{
char* buf = (char*)malloc(sizeof(char)*64);
int i = 0;
while( fscanf(dbFile, "%s %d\n", buf, &i) != EOF )
{
// put the entry into the map
putInHashMap(buf, i, map);
}
free(buf);
}
// calculate candidate suggestions for the string
void calcuCands(const char* str, hash_map* bufMap, hash_map* db)
{
hash_map bufMap_2;
int i = 0;
initiateHashMap(&bufMap_2, BUF_INIT_SIZE);
editDistance(str, bufMap, db, ONE_STEP_PRI);
for( i = 0; i < bufMap->maxSize; i++ )
{
if( bufMap->table[i] != 0 )
{
// calculate the distances 2 step away
editDistance(bufMap->table[i]->str, &bufMap_2, db, TWO_STEP_PRI);
}
}
// merge distance 1 & 2
for( i = 0; i < bufMap_2.maxSize; i++ )
{
if( bufMap_2.size == 0 )
{
break; // all the candidates have been added
}
if( bufMap_2.table[i] != 0 )
{
putInHashMap(bufMap_2.table[i]->str, bufMap_2.table[i]->value, bufMap);
bufMap_2.size--;
}
}
dismissHashMap(&bufMap_2);
}
int main()
{
FILE* dbFile = NULL;
hash_map map;
hash_map bufMap; // store 1 step distance
int i = 0;
int maxValue = -1; // point to the best match
char* sugStr = (char*)malloc(sizeof(char)*64);
char* inputStr = (char*)malloc(sizeof(char)*64);
// initiate the map
initiateHashMap(&map, DB_INIT_SIZE);
initiateHashMap(&bufMap, BUF_INIT_SIZE);
// initiate the vars
strcpy(sugStr, "No spell suggest. Sorry.");
// check if parsed database exits
if( (dbFile = fopen ("parsed.txt", "r")) == NULL )
{
// dictionary not parsed, parse dictionary
printf("Parsing dictionary. Please wait...\n");
parseDict();
}
// read database
if( (dbFile = fopen ("parsed.txt", "r")) == NULL )
{
// dictionary not parsed, parse dictionary
printf("Can't read database. Exiting program.\n");
exit(-1);
}
else
{
printf("Reading database. Please wait...\n");
readDB(&map, dbFile);
printf("Reading complete.\n");
}
// read the user's word which needs correction
while( 1 )
{
printf("Input your WRONG word for correction. Close the window for exiting.\n");
scanf("%s", inputStr);
// check if the word is correct
if( fetchData(inputStr, &map) != -1 )
{
printf("This word is correct, no need to check. \n");
continue;
}
// calculate candidates
calcuCands(inputStr, &bufMap, &map);
// find the largest one and display it. that's the suggestion
for( i = 0; i < bufMap.maxSize; i++ )
{
if( bufMap.table[i] != 0 )
{
if( bufMap.table[i]->value > maxValue )
{
maxValue = bufMap.table[i]->value;
strcpy(sugStr, bufMap.table[i]->str);
}
}
}
if( maxValue != -1 )
{
// print the suggestion
printf("Did you mean '%s'? \n", sugStr);
}
else
{
printf("No spell suggest, sorry.\n");
}
// reset the words buffer
dismissHashMap(&bufMap);
initiateHashMap(&bufMap, BUF_INIT_SIZE);
maxValue = -1;
}
dismissHashMap(&bufMap);
dismissHashMap(&map);
_CrtDumpMemoryLeaks();
return 0;
}