-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhal2012-database2012.hpp
379 lines (323 loc) · 8.93 KB
/
hal2012-database2012.hpp
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
namespace grammar2012 {
template<typename DB> database<DB>::database() :
freehal_base() {
}
template<typename DB> database<DB>::~database() {
}
template<typename DB> void database<DB>::set_lang(const string& _lang) {
freehal_base::set_lang(_lang);
resume();
}
template<typename DB> void database<DB>::set_path(const fs::path& _path) {
freehal_base::set_path(_path);
resume();
}
template<typename DB> void database<DB>::set_tagger(tagger* _t) {
t = _t;
}
template<typename DB> tagger* database<DB>::get_tagger() const {
return t;
}
template<typename DB> bool database<DB>::is_configured() const {
if (!freehal_base::is_configured()) {
return false;
} else if (t == 0) {
cout << "Error! parser2012: tagger is undefined." << endl;
return false;
} else {
return true;
}
}
template<typename DB>
int database<DB>::resume() {
if (!lang.empty() && !path.empty()) {
fs::path resumefile = get_cache_directory() / "files.cache";
try {
fs::ifstream ifs(resumefile);
if (ifs.is_open()) {
boost::archive::xml_iarchive ia(ifs);
ia >> boost::serialization::make_nvp("files", files);
}
} catch (boost::archive::archive_exception& ex) {
cout << "Error! resume: exception during deserialization: "
<< ex.what() << endl;
}
}
}
template<typename DB>
int database<DB>::suspend() {
if (!lang.empty() && !path.empty()) {
fs::path resumefile = get_cache_directory() / "files.cache";
try {
fs::ofstream ofs(resumefile, ios::binary);
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("files", files);
} catch (boost::archive::archive_exception& ex) {
cout << "Error! exception during serialization: " << ex.what()
<< endl;
}
}
}
template<typename DB>
int database<DB>::prepare(const fs::path& p) {
return prepare(p, p);
}
template<typename DB>
int database<DB>::prepare(const fs::path& p, const fs::path& as) {
if (!is_configured())
return 1;
try {
if (fs::exists(p)) {
// is p a regular file?
if (fs::is_regular_file(p)) {
uintmax_t size = fs::file_size(p);
if (files.find(as.generic_string()) == files.end()
|| files.at(as.generic_string()) != size) {
DB* idb = new DB(this);
read_xml_fact_file(p, idb);
idb->to_disk(DB::ALL_KEYS);
delete idb;
files[as.generic_string()] = size;
}
}
// is p a directory?
else if (fs::is_directory(p)) {
vector < fs::path > dir;
copy(fs::directory_iterator(p), fs::directory_iterator(),
back_inserter(dir));
sort(dir.begin(), dir.end());
for (vector<fs::path>::iterator iter(dir.begin());
iter != dir.end(); ++iter) {
const fs::path& p = *iter;
if (algo::ends_with(p.filename().generic_string(),
".xml")) {
this->prepare(p);
}
}
} else {
cout << "Error! prepare: " << p
<< " exists, but is neither a regular file nor a directory"
<< endl;
}
} else {
cout << "Error! prepare: " << p << " does not exist" << endl;
}
} catch (const fs::filesystem_error& ex) {
cout << "Error! " << ex.what() << endl;
}
suspend();
}
template<typename DB> template<typename T1, typename T2>
const fs::path database<DB>::disk_find_file(const string type, T1 a, T1 b, T1 c,
T1 d, T2 filename) {
stringstream ss;
ss << a << "/" << b << "/" << c << "/" << d;
fs::path dir(get_cache_directory() / "database" / type / ss.str());
create_directories(dir);
if (filename.empty())
return dir;
else
return dir / filename;
}
template<typename DB> template<typename M>
int database<DB>::disk_read_file(const fs::path& p, M& list) {
if (!is_configured())
return 1;
if (fs::exists(p) && fs::is_regular_file(p)) {
try {
fs::ifstream ifs(p, std::ios_base::binary | std::ios_base::in);
if (ifs.is_open()) {
std::stringstream buf(
std::ios_base::binary | std::ios_base::in
| std::ios_base::out);
buf << ifs.rdbuf();
ifs.close();
boost::archive::text_iarchive ia(buf);
ia >> boost::serialization::make_nvp("list", list);
return 0;
}
} catch (boost::archive::archive_exception& ex) {
cout << "Error! disk_read_file: " << p
<< " exception during deserialization: " << ex.what()
<< endl;
return 1;
}
}
return 1;
}
template<typename DB> template<typename M>
int database<DB>::disk_write_file(const fs::path& p, const M& list) {
if (!is_configured())
return 1;
try {
fs::ofstream ofs(p);
if (ofs.is_open()) {
boost::archive::text_oarchive oa(ofs);
oa << boost::serialization::make_nvp("list", list);
return 0;
}
} catch (boost::archive::archive_exception& ex) {
cout << "Error! disk_write_file: " << p
<< " exception during serialization: " << ex.what() << endl;
return 1;
}
return 1;
}
template<typename DB>
int database<DB>::find_by_word(vector<boost::shared_ptr<xml_fact> >& list,
const word& word) {
if (!is_configured())
return 1;
DB* idb = new DB(this);
idb->set_add_synonyms(false);
idb->get_facts(word);
idb->copy_facts_to(list);
delete idb;
std::for_each(list.begin(), list.end(),
boost::bind(&database<DB>::insert_synonyms, this, _1));
if (t != 0) {
bool verbose_copy = t->is_verbose();
t->set_verbose(false);
std::for_each(list.begin(), list.end(),
boost::bind(&xml_fact::prepare_tags, _1, t));
t->set_verbose(verbose_copy);
}
return 0;
}
template<typename DB>
int database<DB>::find_by_words(vector<boost::shared_ptr<xml_fact> >& list,
const vector<word>& words) {
if (!is_configured())
return 1;
DB* idb = new DB(this);
idb->set_add_synonyms(false);
foreach (const word& word, words) {
idb->get_facts(word);
}
idb->copy_facts_to(list);
delete idb;
std::for_each(list.begin(), list.end(),
boost::bind(&database<DB>::insert_synonyms, this, _1));
if (t != 0) {
bool verbose_copy = t->is_verbose();
t->set_verbose(false);
std::for_each(list.begin(), list.end(),
boost::bind(&xml_fact::prepare_tags, _1, t));
t->set_verbose(verbose_copy);
}
return 0;
}
template<typename DB>
int database<DB>::find_by_fact(vector<boost::shared_ptr<xml_fact> >& list,
boost::shared_ptr<xml_fact> fact) {
if (!is_configured())
return 1;
this->insert_synonyms(boost::dynamic_pointer_cast<xml_obj>(fact));
if (t != 0) {
fact->toggle(t);
bool verbose_copy = t->is_verbose();
t->set_verbose(false);
fact->prepare_tags(t);
t->set_verbose(verbose_copy);
}
cout << "find_by_fact: " << fact << " (tagger=" << t << ")" << endl;
vector<word> words;
fact->get_words(words);
vector<word> usefulwords;
filter(words, usefulwords, is_index_word());
return find_by_words(list, usefulwords);
}
template<typename DB>
int database<DB>::read_xml_fact_file(const fs::path filename, DB* idb) {
if (!is_configured())
return 1;
{
fs::ifstream i;
i.open(filename);
if (!i) {
cout
<< "Error! Could not open part of speech file (read_xml_fact_file): "
<< filename << endl;
return 1;
}
if (is_verbose()) {
if (is_buffered())
cout << "read fact file: " << filename << endl;
else
cout << "read fact file: " << filename << "\r";
}
}
{
string instr = halxml_readfile(filename);
if (instr.size() == 0) {
cerr
<< "Error! Could not open part of speech file (halxml_readfile): "
<< filename << endl;
return 1;
}
string prestr;
halxml_ordertags(instr, prestr);
if (is_verbose())
cout << "scan fact file: " << filename << endl;
idb->set_complete(true);
idb->set_filename(filename);
idb->set_add_synonyms(true);
halxml_readfacts(idb, prestr, filename);
cout << endl;
}
return 0;
}
template<typename DB>
int database<DB>::get_synonyms(vector<word>& syns, const word& w) {
if (!is_configured())
return 1;
syns.push_back(w);
DB* idb = new DB(this);
idb->set_add_synonyms(false);
idb->get_synonyms(syns, w);
delete idb;
return 0;
}
template<typename DB>
boost::shared_ptr<xml_obj> database<DB>::insert_synonyms(
boost::shared_ptr<xml_obj> xobj) {
if (!is_configured())
return xobj;
//cout << "before: " << xobj->print_str() << endl;
if (xobj->get_mode() == LIST && xobj->get_name() != "truth"
&& xobj->get_name() != "flags") {
std::vector<boost::shared_ptr<xml_obj> >& embedded =
xobj->get_embedded();
foreach(boost::shared_ptr < xml_obj > &obj, embedded)
{
if (obj->get_mode() == LIST && obj->get_name() == "text") {
const vector<word>& words = obj->get_words();
boost::shared_ptr<xml_obj> newobj(new xml_obj(LIST));
newobj->set_name("list");
foreach(const word& w, words)
{
vector<word> syns;
this->get_synonyms(syns, w);
boost::shared_ptr<xml_obj> subtree(new xml_obj(LIST));
subtree->set_name("synonyms");
foreach(word & syn, syns)
{
boost::shared_ptr<xml_obj> text_obj(new xml_obj(LIST));
text_obj->set_name("text");
boost::shared_ptr<xml_obj> t(new xml_obj(TEXT));
t->set_text(syn.get_word());
text_obj << t;
subtree << text_obj;
}
newobj << subtree;
}
obj = newobj;
} else {
insert_synonyms (obj);
}
}
}
//cout << "after: " << xobj->print_str() << endl;
return xobj;
}
}