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
|
#include "jdtalk.h"
/**
* Initializes a dictionary
* @return Empty initialized Dictionary structure
*/
struct Dictionary *dictionary_new() {
struct Dictionary *dict;
dict = malloc(1 * sizeof(*dict));
if (!dict) {
perror("Unable to initialize new dictionary");
exit(1);
}
dict->words = malloc(DICT_INITIAL_SIZE * sizeof(**dict->words));
if (!dict->words) {
perror("Unable to initialize array of dictionary words");
exit(1);
}
dict->nelem_alloc = DICT_INITIAL_SIZE;
dict->nelem_inuse = 0;
return dict;
}
void dictionary_grow_as_needed(struct Dictionary **dict) {
if ((*dict)->nelem_inuse + 1 > (*dict)->nelem_alloc) {
struct Word **tmp;
(*dict)->nelem_alloc += DICT_INITIAL_SIZE;
tmp = realloc((*dict)->words, (*dict)->nelem_alloc * sizeof((*dict)->words));
if (!tmp) {
perror("Unable to extend word list");
exit(1);
}
(*dict)->words = tmp;
}
}
void dictionary_alloc_word_record(struct Dictionary **dict) {
(*dict)->words[(*dict)->nelem_inuse] = malloc(1 * sizeof(**(*dict)->words));
if (!(*dict)->words[(*dict)->nelem_inuse]) {
perror("Unable to allocate dictionary word list record");
exit(1);
}
}
void dictionary_new_word(struct Dictionary **dict, char *s, unsigned type) {
(*dict)->words[(*dict)->nelem_inuse]->word = strdup(s);
if (!(*dict)->words[(*dict)->nelem_inuse]->word) {
perror("Unable to allocate dictionary word in list");
exit(1);
}
(*dict)->words[(*dict)->nelem_inuse]->nchar = strlen(s) - 1;
*((*dict)->words[(*dict)->nelem_inuse]->word + ((*dict)->words[(*dict)->nelem_inuse]->nchar)) = '\0';
(*dict)->words[(*dict)->nelem_inuse]->type = type;
}
struct Dictionary *dictionary_of(struct Dictionary **src, unsigned type) {
struct Dictionary *dest;
dest = dictionary_new();
dictionary_grow_as_needed(&dest);
for (size_t i = 0, x = 0; i < (*src)->nelem_inuse; i++) {
if ((*src)->words[i]->type != type)
continue;
dictionary_alloc_word_record(&dest);
dest->words[x] = malloc(sizeof(**(*src)->words));
dest->words[x] = (*src)->words[i];
dest->nelem_inuse++;
x++;
}
return dest;
}
/**
* Add a word to the dictionary
*
* @param dict pointer to dictionary
* @param s string to append to word list
* @param type type of word (WT_NOUN, WT_VERB, WT_ADVERB, WT_ADJECTIVE)
*/
void dictionary_append(struct Dictionary **dict, char *s, unsigned type) {
dictionary_grow_as_needed(dict);
dictionary_alloc_word_record(dict);
dictionary_new_word(dict, s, type);
(*dict)->nelem_inuse++;
}
/**
* Extract words from a file and append them to a Dictionary
* structure with a specific type
*
* @param fp raw dictionary file handle
* @param dict pointer to Dictionary structure
* @param type type of words in raw dictionary file
* @return 0=success or value of errno on failure
*/
int dictionary_read(FILE *fp, struct Dictionary **dict, unsigned type) {
char *buf;
buf = malloc(DICT_WORD_SIZE_MAX * sizeof(char));
if (!buf) {
perror("Unable to allocate buffer for dictionary_read");
exit(1);
}
while ((fgets(buf, DICT_WORD_SIZE_MAX - 1, fp) != NULL)) {
if (errno) {
return errno;
}
if (*buf == '\0' || *buf == '\n')
continue;
dictionary_append(&(*dict), buf, type);
}
free(buf);
return 0;
}
/**
* Consume all dictionary files
*
* @return fully populated dictionary of words
*/
struct Dictionary *dictionary_populate() {
FILE *fp;
struct Dictionary *dict;
const char *files[] = {
"nouns.txt",
"adjectives.txt",
"adverbs.txt",
"verbs.txt",
NULL,
};
// Types of words expected by files[] array
const unsigned files_type[] = {
WT_NOUN,
WT_ADJECTIVE,
WT_ADVERB,
WT_VERB,
};
// Initialize the dictionary
dict = dictionary_new();
// Consume each dictionary in files[]
for (size_t i = 0; files[i] != NULL; i++) {
char *datadir;
char filename[PATH_MAX];
filename[0] = '\0';
datadir = getenv("JDTALK_DATA");
if (!datadir) {
fprintf(stderr, "JDTALK_DATA environment variable is not set\n");
exit(1);
}
sprintf(filename, "%s/%s", datadir, files[i]);
fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Unable to open dictionary: %s", filename);
exit(1);
}
// Append the contents of file[i] to dictionary
dictionary_read(fp, &dict, files_type[i]);
fclose(fp);
}
return dict;
}
/**
* Get the types of a word
* @param dict pointer to dictionary
* @param s dictionary word to search for
* @return a string containing the word types (i.e. n,a,d,v)
*/
char *dictionary_word_formats(struct Dictionary *dict, const char *s) {
static char buf[OUTPUT_SIZE_MAX];
buf[0] = '\0';
for (size_t i = 0; i < dict->nelem_inuse; i++) {
if (strcmp(dict->words[i]->word, s) != 0) {
continue;
}
switch (dict->words[i]->type) {
case WT_NOUN:
strcat(buf, "n");
break;
case WT_ADJECTIVE:
strcat(buf, "a");
break;
case WT_ADVERB:
strcat(buf, "d");
break;
case WT_VERB:
strcat(buf, "v");
break;
default:
strcat(buf, "x");
break;
}
}
if (!strlen(buf)) {
return NULL;
}
return buf;
}
/**
* Search dictionary for a word
*
* int result;
*
* // "beef" exists and is a verb
* result = dictionary_contains(dict, "beef", WT_VERB);
* // 1
*
* // "beef" exists and is a noun
* result = dictionary_contains(dict, "beef", WT_NOUN);
* // 1
*
* // "beef" is not an adjective
* result = dictionary_contains(dict, "beef", WT_ADJECTIVE);
* // 0
*
* // "Beef" does not exist as written (case sensitive search)
* result = dictionary_contains(dict, "Beef", WT_NOUN);
* // 0
*
* // "Beef" exists (case insensitive search) and is a noun
* result = dictionary_contains(dict, "Beef", WT_NOUN | WT_ICASE);
* // 1
*
* // "Beef" exists (case insensitive search), matching any type of word
* result = dictionary_contains(dict, "Beef", WT_ANY | WT_ICASE);
* // 1
*
* @param dict pointer to populated dictionary
* @param s pointer to pattern string
* @param type type of word (WT_NOUN, WT_VERB, WT_ADVERB, WT_ADJECTIVE) || (WT_ANY, WT_ICASE)
* @return 0=not found, !0=found
*/
unsigned dictionary_contains(struct Dictionary *dict[], const char *s, unsigned type) {
unsigned result;
int found;
unsigned icase;
icase = type & WT_ICASE; // Determine case-sensitivity of the search function
type &= 0x7f; // Strip case-insensitive flag from type
result = 0;
for (size_t x = 0; dict[x] != NULL; x++) {
found = 0;
struct Dictionary *rec = dict[x];
for (size_t i = 0; i < rec->nelem_inuse; i++) {
if (*s != *rec->words[i]->word) {
// Didn't start with first character in s
continue;
}
if (type != WT_ANY && rec->words[i]->type != type) {
// Incorrect type of word
continue;
}
if (icase) {
found = strcasecmp(rec->words[i]->word, s) == 0;
} else {
found = strcmp(rec->words[i]->word, s) == 0;
}
if (found) {
result |= (unsigned) rec->words[i]->type;
break;
}
}
}
return result;
}
/**
* Produce a random word from the dictionary of type
*
* When type is WT_ANY, a random word irrespective of type will be produced
*
* @param dict pointer to dictionary
* @param type type of word to produce
* @return pointer to dictionary word
*/
char *dictionary_word(struct Dictionary *dict, unsigned type) {
struct Word *word;
while (1) {
size_t index = random() % dict->nelem_inuse;
word = dict->words[index];
if (word->type == type || type == WT_ANY) {
return word->word;
}
}
}
/**
* Free a dictionary
* @param dict pointer to dictionary
*/
void dictionary_free(struct Dictionary *dict) {
for (size_t i = 0; i < dict->nelem_inuse; i++) {
free(dict->words[i]->word);
free(dict->words[i]);
}
free(dict->words);
free(dict);
}
|