-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspellcheck.c
More file actions
71 lines (65 loc) · 1.66 KB
/
spellcheck.c
File metadata and controls
71 lines (65 loc) · 1.66 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
/**
* file name : spellcheck.c
* authors : Dave Pederson
* created : Jul 20, 2011
*
* modifications:
* Date: Name: Description:
* ------------ --------------- ----------------------------------------------
* Jul 20, 2011 Dave Pederson Creation
*/
#include <stdio.h>
#include <string.h>
#include "bloom.h"
/**
* Strip '\r' or '\n' chars from a string
*/
static int strip(char *string)
{
char *c;
if ((c = strchr(string, '\r'))) {
*c = 0;
}
if ((c = strchr(string, '\n'))) {
*c = 0;
}
}
/**
* Ad-hoc command-line spell checker
*/
int main(int argc, char *argv[])
{
// Open the dictionary file
FILE *fp;
if (!(fp = fopen("dictionary", "r"))) {
fprintf(stderr, "E: Couldn't open words file\n");
fflush (stderr);
return 1;
}
// Create a bloom filter
bloom_t *filter = bloom_filter_new(2500000);
// Add all dictionary words to the filter
char *p;
char line[1024];
while (fgets(line, 1024, fp)) {
strip(line);
bloom_filter_add(filter, line);
}
fclose(fp);
printf("bloom filter count : %u\n", bloom_filter_count(filter));
printf("bloom filter size : %u\n", bloom_filter_size(filter));
// Read words from stdin and print those words not in the bloom filter
while (fgets(line, 1024, stdin)) {
strip(line);
p = strtok(line, " \t,.;:\r\n?!-/()");
while (p) {
if (!bloom_filter_contains(filter, p)) {
printf("%s\n", p);
}
p = strtok(NULL, " \t,.;:\r\n?!-/()");
}
}
// Cleanup
bloom_filter_free(filter);
return 0;
}