-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCounter.c
366 lines (273 loc) · 8.46 KB
/
WordCounter.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
/*
Author : Vikas Katariya
License : Free
This program is a console application that creates a dictionary of words found in a text file and prints the dictionary
together with the frequency of each unique word found
The case or punctuation is ignored and the output is not sorted.
It accepts the file name as a paramter via command line run.
RULES:
* Any letter A-Z or a-z is converted to lower case
* Occurrence of alphabet defines the start of the word and a non-alphabet defines the end of the word.
* Any numbers or special characters are not considered to be a part of a word
* Word recognised by the program doesn't validate that it has a meaning in a real world.
* Larger the file more RAM requirement will be needed
* Maximum length of the word is (WORD_MAX_SIZE - 1), if bigger than this then it will be truncated
Example OUTPUT:
--------Word Counter----------
Words starting with 't'
testing -- 4
today -- 1
the -- 2
totally -- 2
to -- 1
Words starting with 'a'
a -- 1
alpha -- 1
--------------------------------
Parsing took about 0.000000 seconds
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define WORD_MAX_SIZE 21 //This is maximum word length allowed to be recorded in dictionary (x-1), last one string terminator
#define DEBUG
#undef DEBUG
//Words dictionary
typedef struct words_dict
{
char word[WORD_MAX_SIZE];
unsigned long count;//How many found?
unsigned char word_length;//What's the length of the word
struct words_dict *next;
} words_t;
//Entries - Usually first letter (a-z)
typedef struct entries_dict
{
unsigned char entry_char;
words_t *subset;//Nested linked list
struct entries_dict *next;//Next node
} dict_t;
/* Function : Check if the character is alphabet, this also convert a Upper case to lower
@param : (char *) Alphabet
@return : (bool) True if the character is alphabet*/
bool checkAlpha(char *alpha)
{
bool status = false;
if ((*alpha >= 'a' && *alpha <= 'z')
||
(*alpha >= 'A' && *alpha <= 'Z'))
{
*alpha |= 0x20;//Convert upper to lower, if already lower case it has not effect
status = true;
}
return status;
}
/* Function : Print the dictionary
@param : (dict *) Start of the dictionary
@return : none*/
void printDictionaryCount(dict_t *head)
{
dict_t *current = head;
unsigned long totalWords = 0;
printf("\n--------Word Counter----------\n\n");
while (current != NULL)
{
words_t *curWord = current->subset;
printf("Words starting with '%c'\n", current->entry_char);
while (curWord != NULL)
{
printf("\n\t%s -- %ld", curWord->word, curWord->count);
totalWords += curWord->count;
curWord = curWord->next;
}
printf("\n\n");
current = current->next;
}
printf("\n Total number of words : %ld", totalWords);
printf("\n--------------------------------\n");
}
/* Function : Start reading the characters from the file and build a dictionary of found words
@param : (FILE *) file
@return : none*/
void buildDict(FILE *readFile)
{
unsigned char curIndex = 0;//Temporary buffer counter
char readChar;
char tempWord[WORD_MAX_SIZE] = {0};
bool isAlpha = false;
dict_t *head = NULL;
#ifdef DEBUG
printf("--------DEBUG PRINT--------");
#endif //DEBUG
while ((readChar = fgetc(readFile)) != EOF //Start reading character by character until end of file
||
curIndex > 0) //Or if we have reached EOF, see if we have something in the buffer?
{
isAlpha = checkAlpha(&readChar);
if(isAlpha == false && curIndex > 0) //Check if we have non alphabet and have something in temporary buffer
{
bool wordFound = false;
tempWord[curIndex++] = '\0'; //Add the string terminator
#ifdef DEBUG
printf("\nCurrent word : %s", tempWord);
#endif //DEBUG
dict_t *current = head;//Lets take a working copy
//Scan for duplicate entry
while (current != NULL && wordFound == false)//Valid pointer and we should have not found what we where looking for
{
if(tempWord[0] == current->entry_char)//If it matches the first letter, then search the subset
{
words_t *newWord = current->subset;
while(newWord != NULL) //Search each word
{
if(newWord->word_length == curIndex //We know the length of the word, just quickly find it and compare further if necessary
&&
strcmp(&newWord->word[0], &tempWord[0]) == 0)
{
printf("\t\tSimilar word found");
newWord->count++;//Increment
wordFound = true; //start the scan again
break; //We are done
}
if(newWord->next != NULL)//We move to next entry if its present or else we might have to add one...
{
newWord = newWord->next;
}
else
{
break;
}
}
if (wordFound == false)//If not yet found, add a new word to the subset
{
words_t *newSubset = NULL;
newSubset = (words_t *)malloc(sizeof(words_t));
#ifdef DEBUG
printf("\t\tAdding new entry to the subset");
#endif //DEBUG
if(newSubset == NULL)
{
printf("\nCan't append a new word");
exit(-1);
}
newSubset->word_length = curIndex; //Length of the word
newSubset->count = 1; //How many counts we have for this word
memcpy(&newSubset->word[0], &tempWord[0], curIndex);
newSubset->next = NULL;
wordFound = true; //start the scan again
if(current->subset == NULL) //Assign for the first time
{
current->subset = newSubset;
}
else
{
// We should be at the end of the linked list here, so just add, save time
newWord->next = newSubset;
/*words_t *parse = current->subset;
while (parse->next != NULL)
{
parse = parse->next;
}
parse->next = newSubset;// now we can add a new variable */
}
break;
}
}
if(current->next != NULL) //We move to next entry if its present or else we might have to add one...
{
current = current->next;
}
else
{
break;//We have reached the end of the list
}
}
if(wordFound == false) //Still no main entry? Add a new one
{
#ifdef DEBUG
printf("\t\tAdding new Entry");
#endif //DEBUG
dict_t *newEntry = NULL;
newEntry = (dict_t *)malloc(sizeof(dict_t));
if(newEntry == NULL)
{
printf("\nCan't append entries to dictionary");
exit(-1);
}
newEntry->entry_char = tempWord[0]; //Get the first character
newEntry->subset = (words_t *)malloc(sizeof(words_t));
if(newEntry->subset == NULL)
{
printf("\nCan't append entries to dictionary - subset");
exit(-1);
}
newEntry->subset->word_length = curIndex; //Length of the word
newEntry->subset->count = 1; //How many counts we have for this word
memcpy(&newEntry->subset->word[0], &tempWord[0], curIndex);
newEntry->subset->next = NULL;
newEntry->next = NULL;
if(head == NULL) //Assign it for the first time
{
head = newEntry;
}
else
{
// We should be at the end of the linked list here, so just add, save time
current->next = newEntry;
/*dict_t *parse = head;
while (parse->next != NULL)
{
parse = parse->next;
}
parse->next = newEntry; // now we can add a new variable */
}
}
curIndex = 0; //reset for new word to be accumulated
}
if(isAlpha == true && curIndex < (WORD_MAX_SIZE - 1))//Leave last byte for string terminator
{
tempWord[curIndex++] = readChar;//Just add to the temp buffer
}
}
printDictionaryCount (head); //Lets print
}
/* Function : Parse the file for words
@param : (char *) filename
@return : none*/
void parseFile(char *input)
{
FILE *input_file = fopen(input, "r");//Open as read only
if (input_file == NULL)
{
printf("Sorry, cannot open input file '%s', please refer example below\n", input);
printf("$./WordCounter example.txt\n"); //fopen returns 0, the NULL pointer, on failure
exit(0);
}
else
{
time_t start, stop;
time(&start);
buildDict(input_file);
time(&stop);
printf("Parsing took about %.6f seconds. \n", difftime(stop, start));
fclose(input_file); //Close file
}
}
/* Function : main
@param : (int) number of arguments
@param : (char **) arguments
@return : none*/
void main(int argc, char **argv)
{
char *input = argv[1]; //Argument as file name
#ifdef DEBUG
//Test cases
parseFile("case1.txt");
parseFile("case2_100k.txt");
parseFile("case3_1M_plus.txt");
#endif //DEBUG
parseFile(input);
}