-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdictionary.c
executable file
·197 lines (171 loc) · 4.18 KB
/
dictionary.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
/**
* @author Michael Bull
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
#define ALPHABET_SIZE 26
#define false 0
#define true 1
struct trie tree;
struct trie_node
{
char *value;
// *2 as we must account for a-z as well as A-Z
struct trie_node *children[ALPHABET_SIZE * 2];
};
struct trie
{
struct trie_node root;
};
/**
* Converts a character to an integer value based upon its position in
* the alphabet, or returns -1 if the character is not in the alphabet.
*/
int letter_to_int(char letter)
{
if (letter >= 'A' && letter <= 'Z')
{
return letter - 'A' + 26;
}
else if (letter >= 'a' && letter <= 'z')
{
return letter - 'a';
}
return -1;
}
/**
* Prints and identifies an invalid character in a word
* based upon its index within the word.
*
* e.g.
* word: "plat%eau"
* ^
*/
void print_invalid_word(const char *word, int index)
{
printf(" word: \"%s\"\n", word);
printf(" ");
int i;
for (i = 0; i < index; i++)
{
printf(" ");
}
printf("^\n");
}
/**
* Inserts a word and its meaning into the trie.
*/
int trie_insert(struct trie_node *node, const char *word, char *description)
{
int i;
for (i = 0; i < strlen(word); i++)
{
int letter = letter_to_int(word[i]);
if (letter == -1)
{
// invalid character in the string, cannot be inserted into the trie
printf("failed to insert due to invalid character in word\n");
print_invalid_word(word, i);
printf(" description: \"%s\"\n", description);
return false;
}
struct trie_node *parent = node;
node = node->children[letter];
if (!node)
{
node = malloc(sizeof(struct trie_node));
parent->children[letter] = node;
}
}
/*
* this will prove very efficient we are only allocating enough memory
* for every character in the description, which will save a lot of space
* that would otherwise be allocated to the full MAX_DESC_SIZE
*/
int len = strlen(description);
node->value = malloc(len + 1);
strncpy(node->value, description, len);
return true;
}
/**
* Traverses through a trie from the root node and searches
* for a word's description based on the individual letters
* that make up the word.
*/
char *trie_get(struct trie_node *node, const char *word)
{
int i;
for (i = 0; i < strlen(word); i++)
{
int letter = letter_to_int(word[i]);
if (letter == -1)
{
return false;
}
node = node->children[letter];
if (!node)
{
return false; // not found
}
}
return node->value;
}
void dictionary_initialise()
{
tree = (struct trie) {}; // zeroes the tree
}
int dictionary_read_from_file(const char * filename)
{
// attempts to open the file in read only mode
FILE *file = fopen(filename, "r");
if (!file)
{
printf("could not find/open file \"%s\"\n", filename);
return false;
}
char word[MAX_WORD_SIZE];
char desc[MAX_DESC_SIZE];
int count = 0;
// ensure that at least two items are being parsed (word & desc)
while (fscanf(file, "%s %[^\n]", word, desc) > 1)
{
if (!trie_insert(&tree.root, word, desc))
{
fclose(file);
return false;
}
else
{
count++;
}
}
fclose(file);
printf("parsed file \"%s\" with %i entries\n", filename, count);
return true;
}
int dictionary_lookup(const char *word, char *meaning)
{
// check for invalid letters in input
int i;
for (i = 0; i < strlen(word); i++)
{
int letter = letter_to_int(word[i]);
if (letter == -1)
{
printf("invalid character in word\n");
print_invalid_word(word, i);
return false;
}
}
// grab the description from the tree
char *description = trie_get(&tree.root, word);
if (!description)
{
return false;
}
// copy the description into the user supplied buffer
strcpy(meaning, description);
return true;
}