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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
#include "verbly.h"
namespace verbly {
adjective::adjective(const data& _data, int _id) : word(_data, _id)
{
}
std::string adjective::base_form() const
{
return _base_form;
}
std::string adjective::comparative_form() const
{
return _comparative_form;
}
std::string adjective::superlative_form() const
{
return _superlative_form;
}
adjective::positioning adjective::position() const
{
return _position;
}
bool adjective::has_comparative_form() const
{
return !_comparative_form.empty();
}
bool adjective::has_superlative_form() const
{
return !_superlative_form.empty();
}
bool adjective::has_position() const
{
return _position != adjective::positioning::undefined;
}
adjective_query adjective::antonyms() const
{
return _data.adjectives().antonym_of(*this);
}
adjective_query adjective::synonyms() const
{
return _data.adjectives().synonym_of(*this);
}
adjective_query adjective::generalizations() const
{
return _data.adjectives().generalization_of(*this);
}
adjective_query adjective::specifications() const
{
return _data.adjectives().specification_of(*this);
}
noun_query adjective::anti_pertainyms() const
{
return _data.nouns().anti_pertainym_of(*this);
}
adverb_query adjective::mannernyms() const
{
return _data.adverbs().mannernym_of(*this);
}
noun_query adjective::attributes() const
{
return _data.nouns().attribute_of(*this);
}
adjective_query::adjective_query(const data& _data) : _data(_data)
{
}
adjective_query& adjective_query::limit(int _limit)
{
if ((_limit > 0) || (_limit == unlimited))
{
this->_limit = _limit;
}
return *this;
}
adjective_query& adjective_query::random(bool _random)
{
this->_random = _random;
return *this;
}
adjective_query& adjective_query::except(const adjective& _word)
{
_except.push_back(_word);
return *this;
}
adjective_query& adjective_query::rhymes_with(const word& _word)
{
for (auto rhyme : _word.rhyme_phonemes())
{
_rhymes.push_back(rhyme);
}
if (dynamic_cast<const adjective*>(&_word) != nullptr)
{
_except.push_back(dynamic_cast<const adjective&>(_word));
}
return *this;
}
adjective_query& adjective_query::has_pronunciation(bool _has_prn)
{
this->_has_prn = _has_prn;
return *this;
}
adjective_query& adjective_query::is_variant(bool _is_variant)
{
this->_is_variant = _is_variant;
return *this;
}
adjective_query& adjective_query::variant_of(const noun& _noun)
{
_variant_of.push_back(_noun);
return *this;
}
adjective_query& adjective_query::not_variant_of(const noun& _noun)
{
_not_variant_of.push_back(_noun);
return *this;
}
adjective_query& adjective_query::has_antonyms(bool _is_antonymic)
{
this->_is_antonymic = _is_antonymic;
return *this;
}
adjective_query& adjective_query::antonym_of(const adjective& _adj)
{
_antonym_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::not_antonym_of(const adjective& _adj)
{
_not_antonym_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::has_synonyms(bool _is_synonymic)
{
this->_is_synonymic = _is_synonymic;
return *this;
}
adjective_query& adjective_query::synonym_of(const adjective& _adj)
{
_synonym_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::not_synonym_of(const adjective& _adj)
{
_not_synonym_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::is_generalization(bool _is_generalization)
{
this->_is_generalization = _is_generalization;
return *this;
}
adjective_query& adjective_query::generalization_of(const adjective& _adj)
{
_generalization_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::not_generalization_of(const adjective& _adj)
{
_not_generalization_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::is_specification(bool _is_specification)
{
this->_is_specification = _is_specification;
return *this;
}
adjective_query& adjective_query::specification_of(const adjective& _adj)
{
_specification_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::not_specification_of(const adjective& _adj)
{
_not_specification_of.push_back(_adj);
return *this;
}
adjective_query& adjective_query::is_pertainymic(bool _is_pertainymic)
{
this->_is_pertainymic = _is_pertainymic;
return *this;
}
adjective_query& adjective_query::pertainym_of(const noun& _noun)
{
_pertainym_of.push_back(_noun);
return *this;
}
adjective_query& adjective_query::is_mannernymic(bool _is_mannernymic)
{
this->_is_mannernymic = _is_mannernymic;
return *this;
}
adjective_query& adjective_query::anti_mannernym_of(const adverb& _adv)
{
_anti_mannernym_of.push_back(_adv);
return *this;
}
adjective_query& adjective_query::derived_from(const word& _w)
{
if (dynamic_cast<const adjective*>(&_w) != nullptr)
{
_derived_from_adjective.push_back(dynamic_cast<const adjective&>(_w));
} else if (dynamic_cast<const adverb*>(&_w) != nullptr)
{
_derived_from_adverb.push_back(dynamic_cast<const adverb&>(_w));
} else if (dynamic_cast<const noun*>(&_w) != nullptr)
{
_derived_from_noun.push_back(dynamic_cast<const noun&>(_w));
}
return *this;
}
adjective_query& adjective_query::not_derived_from(const word& _w)
{
if (dynamic_cast<const adjective*>(&_w) != nullptr)
{
_not_derived_from_adjective.push_back(dynamic_cast<const adjective&>(_w));
} else if (dynamic_cast<const adverb*>(&_w) != nullptr)
{
_not_derived_from_adverb.push_back(dynamic_cast<const adverb&>(_w));
} else if (dynamic_cast<const noun*>(&_w) != nullptr)
{
_not_derived_from_noun.push_back(dynamic_cast<const noun&>(_w));
}
return *this;
}
std::list<adjective> adjective_query::run() const
{
std::stringstream construct;
construct << "SELECT adjective_id, base_form, comparative, superlative, position FROM adjectives";
std::list<std::string> conditions;
if (_has_prn)
{
conditions.push_back("adjective_id IN (SELECT adjective_id FROM adjective_pronunciations)");
}
if (!_rhymes.empty())
{
std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE @RHMPRN");
std::string cond = "adjective_id IN (SELECT adjective_id FROM adjective_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
for (auto except : _except)
{
conditions.push_back("adjective_id != @EXCID");
}
if (_requires_comparative_form)
{
conditions.push_back("comparative IS NOT NULL");
}
if (_requires_superlative_form)
{
conditions.push_back("superlative IS NOT NULL");
}
if (_position != adjective::positioning::undefined)
{
switch (_position)
{
case adjective::positioning::predicate: conditions.push_back("position = 'p'"); break;
case adjective::positioning::attributive: conditions.push_back("position = 'a'"); break;
case adjective::positioning::postnominal: conditions.push_back("position = 'i'"); break;
}
}
if (_is_variant)
{
conditions.push_back("adjective_id IN (SELECT adjective_id FROM variation)");
}
if (!_variant_of.empty())
{
std::list<std::string> clauses(_variant_of.size(), "noun_id = @ATTRID");
std::string cond = "adjective_id IN (SELECT adjective_id FROM variation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_variant_of.empty())
{
std::list<std::string> clauses(_not_variant_of.size(), "noun_id = @NATTRID");
std::string cond = "adjective_id NOT IN (SELECT adjective_id FROM variation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_antonymic)
{
conditions.push_back("adjective_id IN (SELECT adjective_2_id FROM adjective_antonymy)");
}
if (!_antonym_of.empty())
{
std::list<std::string> clauses(_antonym_of.size(), "adjective_1_id = @ANTID");
std::string cond = "adjective_id IN (SELECT adjective_2_id FROM adjective_antonymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_antonym_of.empty())
{
std::list<std::string> clauses(_not_antonym_of.size(), "adjective_1_id = @NANTID");
std::string cond = "adjective_id NOT IN (SELECT adjective_2_id FROM adjective_antonymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_synonymic)
{
conditions.push_back("adjective_id IN (SELECT adjective_2_id FROM adjective_synonymy)");
}
if (!_synonym_of.empty())
{
std::list<std::string> clauses(_synonym_of.size(), "adjective_1_id = @SYNID");
std::string cond = "adjective_id IN (SELECT adjective_2_id FROM adjective_synonymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_synonym_of.empty())
{
std::list<std::string> clauses(_not_synonym_of.size(), "adjective_1_id = @NSYNID");
std::string cond = "adjective_id NOT IN (SELECT adjective_2_id FROM adjective_synonymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_generalization)
{
conditions.push_back("adjective_id IN (SELECT general_id FROM specification)");
}
if (!_generalization_of.empty())
{
std::list<std::string> clauses(_generalization_of.size(), "specific_id = @SPECID");
std::string cond = "adjective_id IN (SELECT general_id FROM specification WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_generalization_of.empty())
{
std::list<std::string> clauses(_not_generalization_of.size(), "specific_id = @NSPECID");
std::string cond = "adjective_id NOT IN (SELECT general_id FROM specification WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_specification)
{
conditions.push_back("adjective_id IN (SELECT specific_id FROM specification)");
}
if (!_specification_of.empty())
{
std::list<std::string> clauses(_specification_of.size(), "general_id = @GENID");
std::string cond = "adjective_id IN (SELECT specific_id FROM specification WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_specification_of.empty())
{
std::list<std::string> clauses(_not_specification_of.size(), "general_id = @NGENID");
std::string cond = "adjective_id NOT IN (SELECT specific_id FROM specification WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_pertainymic)
{
conditions.push_back("adjective_id IN (SELECT pertainym_id FROM pertainymy)");
}
if (!_pertainym_of.empty())
{
std::list<std::string> clauses(_pertainym_of.size(), "noun_id = @APERID");
std::string cond = "adjective_id IN (SELECT pertainym_id FROM pertainymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (_is_mannernymic)
{
conditions.push_back("adjective_id IN (SELECT adjective_id FROM mannernymy)");
}
if (!_anti_mannernym_of.empty())
{
std::list<std::string> clauses(_anti_mannernym_of.size(), "mannernym_id = @MANID");
std::string cond = "adjective_id IN (SELECT adjective_id FROM mannernymy WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_derived_from_adjective.empty())
{
std::list<std::string> clauses(_derived_from_adjective.size(), "adjective_2_id = @DERADJ");
std::string cond = "adjective_id IN (SELECT adjective_1_id FROM adjective_adjective_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_derived_from_adjective.empty())
{
std::list<std::string> clauses(_not_derived_from_adjective.size(), "adjective_2_id = @NDERADJ");
std::string cond = "adjective_id NOT IN (SELECT adjective_1_id FROM adjective_adjective_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_derived_from_adverb.empty())
{
std::list<std::string> clauses(_derived_from_adverb.size(), "adverb_id = @DERADV");
std::string cond = "adjective_id IN (SELECT adjective_id FROM adjective_adverb_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_derived_from_adverb.empty())
{
std::list<std::string> clauses(_not_derived_from_adverb.size(), "adverb_id = @NDERADV");
std::string cond = "adjective_id NOT IN (SELECT adjective_id FROM adjective_adverb_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_derived_from_noun.empty())
{
std::list<std::string> clauses(_derived_from_noun.size(), "noun_id = @DERN");
std::string cond = "adjective_id IN (SELECT adjective_id FROM noun_adjective_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!_not_derived_from_noun.empty())
{
std::list<std::string> clauses(_not_derived_from_noun.size(), "noun_id = @NDERN");
std::string cond = "adjective_id NOT IN (SELECT adjective_id FROM noun_adjective_derivation WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
conditions.push_back(cond);
}
if (!conditions.empty())
{
construct << " WHERE ";
construct << verbly::implode(std::begin(conditions), std::end(conditions), " AND ");
}
if (_random)
{
construct << " ORDER BY RANDOM()";
}
if (_limit != unlimited)
{
construct << " LIMIT " << _limit;
}
sqlite3_stmt* ppstmt;
std::string query = construct.str();
if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
{
throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
}
if (!_rhymes.empty())
{
int i = 0;
for (auto rhyme : _rhymes)
{
std::string rhymer = "%" + rhyme;
sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@RHMPRN"), rhymer.c_str(), rhymer.length(), SQLITE_STATIC);
i++;
}
}
for (auto except : _except)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id);
}
for (auto attribute : _variant_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ATTRID"), attribute._id);
}
for (auto attribute : _not_variant_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NATTRID"), attribute._id);
}
for (auto antonym : _antonym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ANTID"), antonym._id);
}
for (auto antonym : _not_antonym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NANTID"), antonym._id);
}
for (auto synonym : _synonym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SYNID"), synonym._id);
}
for (auto synonym : _not_synonym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NSYNID"), synonym._id);
}
for (auto specific : _generalization_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SPECID"), specific._id);
}
for (auto specific : _not_generalization_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NSPECID"), specific._id);
}
for (auto general : _specification_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@GENID"), general._id);
}
for (auto general : _not_specification_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NGENID"), general._id);
}
for (auto n : _pertainym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@APERID"), n._id);
}
for (auto mannernym : _anti_mannernym_of)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@MANID"), mannernym._id);
}
for (auto adj : _derived_from_adjective)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@DERADJ"), adj._id);
}
for (auto adj : _not_derived_from_adjective)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NDERADJ"), adj._id);
}
for (auto adv : _derived_from_adverb)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@DERADV"), adv._id);
}
for (auto adv : _not_derived_from_adverb)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NDERADV"), adv._id);
}
for (auto n : _derived_from_noun)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@DERN"), n._id);
}
for (auto n : _not_derived_from_noun)
{
sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@NDERN"), n._id);
}
std::list<adjective> output;
while (sqlite3_step(ppstmt) == SQLITE_ROW)
{
adjective tnc {_data, sqlite3_column_int(ppstmt, 0)};
tnc._base_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
if (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL)
{
tnc._comparative_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
}
if (sqlite3_column_type(ppstmt, 3) != SQLITE_NULL)
{
tnc._superlative_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3)));
}
if (sqlite3_column_type(ppstmt, 4) != SQLITE_NULL)
{
std::string adjpos(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4)));
if (adjpos == "p")
{
tnc._position = adjective::positioning::predicate;
} else if (adjpos == "a")
{
tnc._position = adjective::positioning::attributive;
} else if (adjpos == "i")
{
tnc._position = adjective::positioning::postnominal;
}
}
output.push_back(tnc);
}
sqlite3_finalize(ppstmt);
for (auto& adjective : output)
{
query = "SELECT pronunciation FROM adjective_pronunciations WHERE adjective_id = ?";
if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
{
throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
}
sqlite3_bind_int(ppstmt, 1, adjective._id);
while (sqlite3_step(ppstmt) == SQLITE_ROW)
{
std::string pronunciation(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0)));
auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " ");
adjective.pronunciations.push_back(phonemes);
}
sqlite3_finalize(ppstmt);
}
return output;
}
};
|