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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
#include <libxml/parser.h>
#include <iostream>
#include <dirent.h>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <fstream>
#include <sqlite3.h>
#include <sstream>
#include <regex>
struct verb {
std::string infinitive;
std::string past_tense;
std::string past_participle;
std::string ing_form;
std::string s_form;
};
struct group {
std::string id;
std::set<std::string> members;
};
std::map<std::string, group> groups;
std::map<std::string, verb> verbs;
std::map<int, std::map<int, int>> wn;
void print_usage()
{
std::cout << "Verbly Datafile Generator" << std::endl;
std::cout << "-------------------------" << std::endl;
std::cout << "Requires exactly four arguments." << std::endl;
std::cout << "1. The path to a VerbNet data directory." << std::endl;
std::cout << "2. The path to a SemLink vnpbMappings file." << std::endl;
std::cout << "3. The path to an AGID infl.txt file." << std::endl;
std::cout << "4. The path to a WordNet prolog data directory." << std::endl;
std::cout << "5. Datafile output path." << std::endl;
exit(1);
}
/*
void parse_group(xmlNodePtr top, std::string filename)
{
xmlChar* key = xmlGetProp(top, (xmlChar*) "ID");
if (key == 0)
{
std::cout << "Bad VerbNet file format: " << filename << std::endl;
print_usage();
}
std::string vnid = key;
vnid = vnid.substr(vnid.find_first_of("-")+1);
xmlFree(key);
group g;
g.id = vnid;
for (xmlNodePtr node = top->xmlChildrenNode; node != nullptr; node = node->next)
{
if (!xmlStrcmp(node->name, (const xmlChar*) "MEMBERS"))
{
for (xmlNodePtr member = node->xmlChildrenNode; member != nullptr; member = member->next)
{
if (!xmlStrcmp(member->name, (const xmlChar*) "MEMBER"))
{
key = xmlGetProp(member, (xmlChar*) "name");
g.members.insert(key);
xmlFree(key);
}
}
} else if (!xmlStrcmp(node->name, (const xmlChar*) "FRAMES"))
{
for (xmlNodePtr frame = node->xmlChildrenNode; frame != nullptr; frame = frame->next)
{
if (!xmlStrcmp(frame->name, (const xmlChar*) "FRAME"))
{
for (xmlNodePtr framenode = frame->xmlChildrenNode; framenode != nullptr; framenode = framenode->next)
{
}
}
}
}
}
}*/
int main(int argc, char** argv)
{
if (argc != 6)
{
print_usage();
}
/*DIR* dir;
if ((dir = opendir(argv[1])) == nullptr)
{
std::cout << "Invalid VerbNet data directory." << std::endl;
print_usage();
}
struct dirent* ent;
while ((ent = readdir(dir)) != nullptr)
{
std::string filename(argv[1]);
if (filename.back() != '/')
{
filename += '/';
}
filename += ent->d_name;
//std::cout << ent->d_name << std::endl;
if (filename.rfind(".xml") != filename.size() - 4)
{
continue;
}
xmlDocPtr doc = xmlParseFile(filename.c_str());
if (doc == nullptr)
{
std::cout << "Error opening " << filename << std::endl;
print_usage();
}
xmlNodePtr top = xmlDocGetRootElement(doc);
if ((top == nullptr) || (xmlStrcmp(top->name, (xmlChar*) "VNCLASS")))
{
std::cout << "Bad VerbNet file format: " << filename << std::endl;
print_usage();
}
parse_group(top, filename);
}
closedir(dir);*/
// Get verbs from AGID
std::cout << "Reading verb inflection..." << std::endl;
std::ifstream agidfile(argv[3]);
if (!agidfile.is_open())
{
std::cout << "Could not open AGID file: " << argv[3] << std::endl;
print_usage();
}
for (;;)
{
std::string line;
if (!getline(agidfile, line))
{
break;
}
if (line.back() == '\r')
{
line.pop_back();
}
int divider = line.find_first_of(" ");
std::string word = line.substr(0, divider);
line = line.substr(divider+1);
if (line[0] != 'V')
{
continue;
}
if (line[1] == '?')
{
line.erase(0, 4);
} else {
line.erase(0, 3);
}
std::vector<std::string> forms;
while (!line.empty())
{
std::string inflection;
if ((divider = line.find(" | ")) != std::string::npos)
{
inflection = line.substr(0, divider);
line = line.substr(divider + 3);
} else {
inflection = line;
line = "";
}
if ((divider = inflection.find_first_of(",?")) != std::string::npos)
{
inflection = inflection.substr(0, divider);
}
forms.push_back(inflection);
}
verb v;
v.infinitive = word;
if (forms.size() == 4)
{
v.past_tense = forms[0];
v.past_participle = forms[1];
v.ing_form = forms[2];
v.s_form = forms[3];
} else if (forms.size() == 3)
{
v.past_tense = forms[0];
v.past_participle = forms[0];
v.ing_form = forms[1];
v.s_form = forms[2];
} else if (forms.size() == 8)
{
// As of AGID 2014.08.11, this is only "to be"
v.past_tense = forms[0];
v.past_participle = forms[2];
v.ing_form = forms[3];
v.s_form = forms[4];
} else {
// Words that don't fit the cases above as of AGID 2014.08.11:
// - may and shall do not conjugate the way we want them to
// - methinks only has a past tense and is an outlier
// - wit has five forms, and is archaic/obscure enough that we can ignore it for now
std::cout << "Ignoring verb \"" << word << "\" due to non-standard number of forms." << std::endl;
}
verbs[word] = v;
}
// Start writing output
std::cout << "Writing output..." << std::endl;
sqlite3* ppdb;
if (sqlite3_open_v2(argv[5], &ppdb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK)
{
std::cout << "Error opening output datafile: " << sqlite3_errmsg(ppdb) << std::endl;
print_usage();
}
std::ifstream schemafile("schema.sql");
if (!schemafile.is_open())
{
std::cout << "Could not find schema file" << std::endl;
print_usage();
}
std::stringstream schemabuilder;
for (;;)
{
std::string line;
if (!getline(schemafile, line))
{
break;
}
if (line.back() == '\r')
{
line.pop_back();
}
schemabuilder << line << std::endl;
}
std::string schema = schemabuilder.str();
while (!schema.empty())
{
std::string query;
int divider = schema.find(";");
if (divider != std::string::npos)
{
query = schema.substr(0, divider+1);
schema = schema.substr(divider+2);
} else {
break;
}
sqlite3_stmt* schmstmt;
if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &schmstmt, NULL) != SQLITE_OK)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
if (sqlite3_step(schmstmt) != SQLITE_DONE)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
sqlite3_finalize(schmstmt);
}
std::cout << "Writing verbs..." << std::endl;
for (auto& mapping : verbs)
{
sqlite3_stmt* ppstmt;
std::string query("INSERT INTO verbs (infinitive, past_tense, past_participle, ing_form, s_form) VALUES (?, ?, ?, ?, ?)");
if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
sqlite3_bind_text(ppstmt, 1, mapping.second.infinitive.c_str(), mapping.second.infinitive.length(), SQLITE_STATIC);
sqlite3_bind_text(ppstmt, 2, mapping.second.past_tense.c_str(), mapping.second.past_tense.length(), SQLITE_STATIC);
sqlite3_bind_text(ppstmt, 3, mapping.second.past_participle.c_str(), mapping.second.past_participle.length(), SQLITE_STATIC);
sqlite3_bind_text(ppstmt, 4, mapping.second.ing_form.c_str(), mapping.second.ing_form.length(), SQLITE_STATIC);
sqlite3_bind_text(ppstmt, 5, mapping.second.s_form.c_str(), mapping.second.s_form.length(), SQLITE_STATIC);
if (sqlite3_step(ppstmt) != SQLITE_DONE)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
sqlite3_finalize(ppstmt);
}
// Get nouns/adjectives/adverbs from WordNet
// Useful relations:
// - s: master list
// - ant: antonymy (e.g. happy/sad, sad/happy, happiness/sadness)
// - at: variation (e.g. a measurement can be standard or nonstandard)
// - hyp: hypernymy/hyponymy (e.g. color/red, color/blue)
// - ins: instantiation (do we need this? let's see)
// - mm: member meronymy/holonymy (e.g. family/mother, family/child)
// - mp: part meronymy/holonymy (e.g. wheel/spoke, wheel/tire)
// - ms: substance meronymy/holonymy (e.g. tire/rubber, doorstop/rubber)
// - per: pertainymy (e.g. something that is Alaskan pertains to Alaska)
// mannernymy (e.g. something done quickly is done in a manner that is quick)
// - sa: specification (e.g. inaccurate (general) can mean imprecise or incorrect (specific))
// - sim: synonymy (e.g. cheerful/happy, happy/cheerful)
// - syntax: positioning flags for some adjectives
std::string wnpref {argv[4]};
if (wnpref.back() != '/')
{
wnpref += '/';
}
std::cout << "Reading words from WordNet..." << std::endl;
std::ifstream wnsfile(wnpref + "wn_s.pl");
if (!wnsfile.is_open())
{
std::cout << "Invalid WordNet data directory." << std::endl;
print_usage();
}
for (;;)
{
std::string line;
if (!getline(wnsfile, line))
{
break;
}
if (line.back() == '\r')
{
line.pop_back();
}
std::regex relation("^s\\(([134]\\d{8}),(\\d+),'([\\w ]+)',");
std::smatch relation_data;
if (!std::regex_search(line, relation_data, relation))
{
continue;
}
int synset_id = stoi(relation_data[1]);
int wnum = stoi(relation_data[2]);
std::string word = relation_data[3];
std::string query;
switch (synset_id / 100000000)
{
case 1: // Noun
{
query = "INSERT INTO nouns (form) VALUES (?)";
break;
}
case 2: // Verb
{
// Ignore
break;
}
case 3: // Adjective
{
query = "INSERT INTO adjectives (form) VALUES (?)";
break;
}
case 4: // Adverb
{
query = "INSERT INTO adverbs (form) VALUES (?)";
break;
}
}
sqlite3_stmt* ppstmt;
if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
sqlite3_bind_text(ppstmt, 1, word.c_str(), word.length(), SQLITE_STATIC);
if (sqlite3_step(ppstmt) != SQLITE_DONE)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
sqlite3_finalize(ppstmt);
query = "SELECT last_insert_rowid()";
if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
if (sqlite3_step(ppstmt) != SQLITE_ROW)
{
std::cout << "Error writing to output database: " << sqlite3_errmsg(ppdb) << std::endl;
sqlite3_close_v2(ppdb);
print_usage();
}
wn[synset_id][wnum] = sqlite3_column_int(ppstmt, 0);
sqlite3_finalize(ppstmt);
}
sqlite3_close_v2(ppdb);
std::cout << "Done." << std::endl;
}
|