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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
|
#include "generator.h"
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <hkutil/string.h>
verbly::filter Generator::MakeHintFilter(verbly::filter subfilter, Height height, Colour colour, FilterDirection filter_direction) const
{
switch (colour) {
case kWhite: {
switch (height) {
case kBottom: {
return (verbly::word::synonyms %= subfilter);
}
case kTop: {
return (verbly::form::pronunciations %=
verbly::filter("homophones", false,
(verbly::pronunciation::forms %= (subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)))));
}
case kMiddle: {
return subfilter;
}
default: break; // Not supported yet.
}
break;
}
case kBlack: {
switch (height) {
case kBottom: {
return (verbly::word::antonyms %= subfilter);
}
case kMiddle: {
return (verbly::form::antogram %= subfilter);
}
case kTop: {
return (verbly::pronunciation::antophone %= subfilter);
}
default: break; // Not supported yet.
}
break;
}
case kBrown: {
break; // Not supported yet.
}
case kRed: {
switch (height) {
case kTop: {
if (filter_direction == kTowardSolution)
{
return (verbly::pronunciation::merophones %= subfilter);
} else {
return (verbly::pronunciation::holophones %= subfilter);
}
}
case kMiddle: {
if (filter_direction == kTowardSolution)
{
return (verbly::form::merographs %= subfilter);
} else {
return (verbly::form::holographs %= subfilter);
}
}
case kBottom: {
if (filter_direction == kTowardSolution)
{
return (verbly::notion::partMeronyms %=
verbly::filter("partMeronyms", false,
subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)));
} else {
return (verbly::notion::partHolonyms %=
verbly::filter("partHolonyms", false,
subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)));
}
}
default: break; // Not supported yet.
}
break;
}
case kBlue: {
switch (height) {
case kTop: {
if (filter_direction == kTowardSolution)
{
return (verbly::pronunciation::holophones %= subfilter);
} else {
return (verbly::pronunciation::merophones %= subfilter);
}
}
case kMiddle: {
if (filter_direction == kTowardSolution)
{
return (verbly::form::holographs %= subfilter);
} else {
return (verbly::form::merographs %= subfilter);
}
}
case kBottom: {
if (filter_direction == kTowardSolution)
{
return (verbly::notion::partHolonyms %=
verbly::filter("partHolonyms", false,
subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)));
} else {
return (verbly::notion::partMeronyms %=
verbly::filter("partMeronyms", false,
subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)));
}
}
default: break; // Not supported yet.
}
break;
}
case kPurple: {
switch (height) {
case kMiddle: {
return (verbly::form::holographs %=
verbly::filter("midpurp", false,
(verbly::form::length >= 4 && (verbly::form::merographs %=
(subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id))))));
}
case kTop: {
return (verbly::pronunciation::rhymes %= subfilter);
}
default: break; // Not supported yet.
}
break;
}
case kYellow: {
switch (height) {
case kTop: {
return (verbly::pronunciation::anaphones %= (subfilter && verbly::filter(
verbly::pronunciation::id,
verbly::filter::comparison::field_does_not_equal,
verbly::pronunciation::id)));
}
case kMiddle: {
return (verbly::form::anagrams %= (subfilter && verbly::filter(
verbly::form::id,
verbly::filter::comparison::field_does_not_equal,
verbly::form::id)));
}
default: break; // Not supported yet.
}
break;
}
case kGreen: {
if (filter_direction == kTowardSolution)
{
switch (height) {
case kBottom: {
verbly::filter whitelist =
(verbly::notion::wnid == 109287968) // Geological formations
|| (verbly::notion::wnid == 109208496) // Asterisms (collections of stars)
|| (verbly::notion::wnid == 109239740) // Celestial bodies
|| (verbly::notion::wnid == 109277686) // Exterrestrial objects (comets and meteroids)
|| (verbly::notion::wnid == 109403211) // Radiators (supposedly natural radiators but actually these are just pictures of radiators)
|| (verbly::notion::wnid == 109416076) // Rocks
|| (verbly::notion::wnid == 105442131) // Chromosomes
|| (verbly::notion::wnid == 100324978) // Tightrope walking
|| (verbly::notion::wnid == 100326094) // Rock climbing
|| (verbly::notion::wnid == 100433458) // Contact sports
|| (verbly::notion::wnid == 100433802) // Gymnastics
|| (verbly::notion::wnid == 100439826) // Track and field
|| (verbly::notion::wnid == 100440747) // Skiing
|| (verbly::notion::wnid == 100441824) // Water sport
|| (verbly::notion::wnid == 100445351) // Rowing
|| (verbly::notion::wnid == 100446980) // Archery
// TODO: add more sports
|| (verbly::notion::wnid == 100021939) // Artifacts
|| (verbly::notion::wnid == 101471682) // Vertebrates
;
verbly::filter blacklist =
(verbly::notion::wnid == 106883725) // swastika
|| (verbly::notion::wnid == 104416901) // tetraskele
|| (verbly::notion::wnid == 102512053) // fish
|| (verbly::notion::wnid == 103575691) // instrument of execution
|| (verbly::notion::wnid == 103829563) // noose
|| (verbly::notion::wnid == 103663910) // life support
;
return subfilter
&& (verbly::notion::fullHypernyms %= whitelist)
&& !(verbly::notion::fullHypernyms %= blacklist)
&& (verbly::notion::partOfSpeech == verbly::part_of_speech::noun)
&& (verbly::notion::numOfImages >= 1);
}
case kMiddle: {
return subfilter;
}
default: break; // Never supported.
}
} else {
return (verbly::form::text == "picture");
}
break;
}
default: break; // Not supported yet.
}
return {};
}
std::string ApplyWanderlust(const std::string& word) {
std::string result;
for (char ch : word) {
if (ch == 'w') {
result += '1';
} else if (ch == 'a') {
result += '2';
} else if (ch == 'n') {
result += '3';
} else if (ch == 'd') {
result += '4';
} else if (ch == 'e') {
result += '5';
} else if (ch == 'r') {
result += '6';
} else if (ch == 'l') {
result += '7';
} else if (ch == 'u') {
result += '8';
} else if (ch == 's') {
result += '9';
} else if (ch == 't') {
result += '0';
} else if (ch == ' ') {
result += ' ';
}
}
return result;
}
void Generator::GenerateStaticPanel(std::string name, std::string question, std::string answer) {
SavePanel(name, question, answer.empty() ? question : answer, {});
}
void Generator::GenerateSinglePanel(std::string name, Height height, Colour colour, GenerateOptions options) {
while (!GenerateSinglePanelImpl(name, height, colour, options));
}
bool Generator::GenerateSinglePanelImpl(std::string name, Height height, Colour colour, GenerateOptions options) {
verbly::form solution;
if (options.reuse_solution) {
const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
solution = database_->forms(verbly::form::text == word).first();
} else {
verbly::filter forward = MakeHintFilter({}, height, colour, kTowardSolution);
if (options.must_be_broad) {
forward &= MakeHintFilter({}, kBottom, kWhite, kTowardSolution);
forward &= MakeHintFilter({}, kBottom, kRed, kTowardSolution);
forward &= MakeHintFilter({}, kBottom, kBlue, kTowardSolution);
}
std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
}
if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
return false;
}
if (options.palindrome != kPalindromeUnspecified) {
std::string reversed = solution.getText();
std::reverse(reversed.begin(), reversed.end());
if ((options.palindrome == kForcePalindrome && reversed != solution.getText()) ||
(options.palindrome == kRejectPalindrome && reversed == solution.getText())) {
return false;
}
}
// Finish early if this is a middle white.
if (height == kMiddle && colour == kWhite) {
SavePanel(name, solution.getText(), solution.getText(), options);
if (!options.copy_to.empty()) {
SavePanel(options.copy_to, solution.getText(), solution.getText(), options);
}
if (!options.copy_hidden.empty()) {
SavePanel(options.copy_hidden, std::string(solution.getText().size(), '?'), solution.getText(), options);
}
return true;
}
verbly::filter questionFilter = MakeHintFilter(solution, height, colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
if (questions.size() < 1) return false;
verbly::form question = questions.front();// questions.at(std::uniform_int_distribution<int>(0, questions.size())(rng_));
if (IsClueTrivial(height, colour, question, solution, options)) {
return false;
}
if (options.max_len_diff >= 0 && std::abs(static_cast<int>(question.getText().size() - solution.getText().size())) > options.max_len_diff) {
return false;
}
SavePanel(name, question.getText(), solution.getText(), options);
if (!options.copy_to.empty()) {
SavePanel(options.copy_to, question.getText(), solution.getText(), options);
}
return true;
}
void Generator::GenerateDoublePanel(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
while (!GenerateDoublePanelImpl(name1, name2, height, colour, options));
}
bool Generator::GenerateDoublePanelImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
verbly::form solution;
if (options.reuse_solution) {
const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
solution = database_->forms(verbly::form::text == word).first();
} else {
verbly::filter forward = MakeHintFilter({}, height, colour, kTowardSolution);
if (options.must_be_broad) {
forward &= MakeHintFilter({}, kBottom, kWhite, kTowardSolution);
forward &= MakeHintFilter({}, kBottom, kRed, kTowardSolution);
forward &= MakeHintFilter({}, kBottom, kBlue, kTowardSolution);
}
std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
}
if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
return false;
}
// Finish early if this is a middle white.
if (height == kMiddle && colour == kWhite) {
SavePanel(name1, solution.getText(), solution.getText(), options);
SavePanel(name2, solution.getText(), solution.getText(), options);
return true;
}
verbly::filter questionFilter = MakeHintFilter(solution, height, colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 2).all();
if (questions.size() < 2) return false;
//std::shuffle(questions.begin(), questions.end(), rng_);
if (IsClueTrivial(height, colour, questions[0], solution, options) || IsClueTrivial(height, colour, questions[1], solution, options)) {
return false;
}
if (options.max_len_diff >= 0) {
if (std::abs(static_cast<int>(questions[0].getText().size() - solution.getText().size())) > options.max_len_diff
|| std::abs(static_cast<int>(questions[1].getText().size() - solution.getText().size())) > options.max_len_diff) {
return false;
}
}
SavePanel(name1, questions[0].getText(), solution.getText(), options);
SavePanel(name2, questions[1].getText(), solution.getText(), options);
return true;
}
void Generator::GenerateCohintedPanels(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
while (!GenerateCohintedPanelsImpl(name1, name2, height, colour, options));
}
bool Generator::GenerateCohintedPanelsImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
verbly::filter backward = MakeHintFilter({}, height, colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(backward && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
verbly::form question = questions.front();// solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
// Finish early if this is a middle white.
if (height == kMiddle && colour == kWhite) {
SavePanel(name1, question.getText(), question.getText(), options);
SavePanel(name2, question.getText(), question.getText(), options);
return true;
}
verbly::filter solutionFilter = MakeHintFilter(question, height, colour, kTowardSolution);
std::vector<verbly::form> solutions = database_->forms(solutionFilter && GetWordFilter(kTowardSolution, options), {}, 2).all();
if (solutions.size() < 2) return false;
//std::shuffle(questions.begin(), questions.end(), rng_);
if (IsClueTrivial(height, colour, question, solutions[0], options)
|| IsClueTrivial(height, colour, question, solutions[1], options)) {
return false;
}
if (options.max_len_diff >= 0) {
if (std::abs(static_cast<int>(question.getText().size() - solutions[0].getText().size())) > options.max_len_diff
|| std::abs(static_cast<int>(question.getText().size() - solutions[1].getText().size())) > options.max_len_diff) {
return false;
}
}
SavePanel(name1, question.getText(), solutions[0].getText(), options);
SavePanel(name2, question.getText(), solutions[1].getText(), options);
return true;
}
void Generator::GeneratePairedPanels(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
while (!GeneratePairedPanelsImpl(name1, name2, height, colour, options));
}
bool Generator::GeneratePairedPanelsImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
Colour effectiveColour = (height == kMiddle && colour == kWhite) ? kBlack : colour;
Height effectiveHeight = (height == kMiddle && colour == kWhite) ? kBottom : height;
verbly::filter forward = MakeHintFilter({}, effectiveHeight, effectiveColour, kTowardSolution);
std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
verbly::form solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
verbly::filter questionFilter = MakeHintFilter(solution, effectiveHeight, effectiveColour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
if (questions.size() < 1) return false;
verbly::form question = questions.front();// questions.at(std::uniform_int_distribution<int>(0, questions.size())(rng_));
if (IsClueTrivial(height, effectiveColour, question, solution, options)) {
return false;
}
if (options.palindrome != kPalindromeUnspecified) {
std::string reversed = question.getText();
std::reverse(reversed.begin(), reversed.end());
if ((options.palindrome == kForcePalindrome && reversed != question.getText()) ||
(options.palindrome == kRejectPalindrome && reversed == question.getText())) {
return false;
}
}
if (options.max_len_diff >= 0) {
if (std::abs(static_cast<int>(question.getText().size() - solution.getText().size())) > options.max_len_diff) {
return false;
}
}
if (height == kMiddle && colour == kWhite) {
SavePanel(name1, question.getText(), question.getText(), options);
SavePanel(name2, solution.getText(), solution.getText(), options);
if (!options.copy_to.empty() && !options.copy_to2.empty()) {
SavePanel(options.copy_to, question.getText(), question.getText(), options);
SavePanel(options.copy_to2, solution.getText(), solution.getText(), options);
}
} else {
SavePanel(name1, question.getText(), solution.getText(), options);
SavePanel(name2, solution.getText(), question.getText(), options);
if (!options.copy_to.empty() && !options.copy_to2.empty()) {
SavePanel(options.copy_to, question.getText(), solution.getText(), options);
SavePanel(options.copy_to2, solution.getText(), question.getText(), options);
}
}
return true;
}
void Generator::GeneratePanelStack(std::string top_name, Colour top_colour, std::string middle_name, Colour middle_colour, std::string bottom_name, Colour bottom_colour, GenerateOptions options) {
while (!GeneratePanelStackImpl(top_name, top_colour, middle_name, middle_colour, bottom_name, bottom_colour, options));
}
bool Generator::GeneratePanelStackImpl(std::string top_name, Colour top_colour, std::string middle_name, Colour middle_colour, std::string bottom_name, Colour bottom_colour, GenerateOptions options) {
verbly::form solution;
if (options.reuse_solution) {
const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
solution = database_->forms(verbly::form::text == word).first();
} else {
verbly::filter forward = GetWordFilter(kTowardSolution, options);
if (!top_name.empty()) {
forward &= MakeHintFilter({}, kTop, top_colour, kTowardSolution);
}
if (!middle_name.empty()) {
forward &= MakeHintFilter({}, kMiddle, middle_colour, kTowardSolution);
}
if (!bottom_name.empty()) {
forward &= MakeHintFilter({}, kBottom, bottom_colour, kTowardSolution);
}
std::vector<verbly::form> solutions = database_->forms(forward).all();
solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
}
if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
return false;
}
std::string top_hint;
std::string middle_hint;
std::string bottom_hint;
if (!top_name.empty()) {
verbly::filter questionFilter = MakeHintFilter(solution, kTop, top_colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
if (questions.empty()) return false;
top_hint = questions.front().getText();
if (IsClueTrivial(kTop, top_colour, questions.front(), solution, options)) {
return false;
}
}
if (!middle_name.empty()) {
verbly::filter questionFilter = MakeHintFilter(solution, kMiddle, middle_colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
if (questions.empty()) return false;
middle_hint = questions.front().getText();
if (IsClueTrivial(kMiddle, middle_colour, questions.front(), solution, options)) {
return false;
}
}
if (!bottom_name.empty()) {
verbly::filter questionFilter = MakeHintFilter(solution, kBottom, bottom_colour, kTowardQuestion);
std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
if (questions.empty()) return false;
bottom_hint = questions.front().getText();
if (IsClueTrivial(kBottom, bottom_colour, questions.front(), solution, options)) {
return false;
}
}
if (!top_name.empty()) {
SavePanel(top_name, top_hint, solution.getText(), options);
}
if (!middle_name.empty()) {
SavePanel(middle_name, middle_hint, solution.getText(), options);
}
if (!bottom_name.empty()) {
SavePanel(bottom_name, bottom_hint, solution.getText(), options);
}
return true;
}
void Generator::GenerateOrangeNumberPanel(std::string name) {
std::string solution = wanderlust_->GetWord(rng_);
std::string question = ApplyWanderlust(solution);
SavePanel(name, question, solution);
}
void Generator::GenerateOrangeWordPanel(std::string name) {
std::string question = wanderlust_->GetWord(rng_);
std::string solution = ApplyWanderlust(question);
SavePanel(name, question, solution);
}
void Generator::GenerateOrangeAdditionPanel(std::string name) {
auto [question, solution] = wanderlust_->GetPuzzle(rng_);
SavePanel(name, question, solution);
}
void Generator::GenerateOneRoadManyTurns(std::string order_name, std::string part1_name, std::string part2_name, std::string part3_name, std::string part4_name) {
const auto& [part1_q, part1_a] = panels_.at(part1_name);
const auto& [part2_q, part2_a] = panels_.at(part2_name);
const auto& [part3_q, part3_a] = panels_.at(part3_name);
const auto& [part4_q, part4_a] = panels_.at(part4_name);
SavePanel(order_name, "order", part1_a + " " + part2_a + " " + part3_a + " " + part4_a);
}
void Generator::GenerateComboPanel(std::string name, Height left_height, Colour left_colour, Height right_height, Colour right_colour, GenerateOptions options) {
while (!GenerateComboPanelImpl(name, left_height, left_colour, right_height, right_colour, options));
}
bool Generator::GenerateComboPanelImpl(std::string name, Height left_height, Colour left_colour, Height right_height, Colour right_colour, GenerateOptions options) {
options.force_two_words = true;
verbly::form solution = database_->forms(GetWordFilter(kTowardSolution, options)).first();
std::string soltext = solution.getText();
int spacepos = soltext.find(" ");
std::string leftword = soltext.substr(0, spacepos);
std::string rightword = soltext.substr(spacepos+1);
options.force_two_words = false;
verbly::filter left_filter = MakeHintFilter(verbly::form::text == leftword, left_height, left_colour, kTowardQuestion);
std::vector<verbly::form> left_questions = database_->forms(left_filter && GetWordFilter(kTowardQuestion, options)).all();
if (left_questions.size() < 1) return false;
verbly::form left_question = left_questions.front();
verbly::filter right_filter = MakeHintFilter(verbly::form::text == rightword, right_height, right_colour, kTowardQuestion);
std::vector<verbly::form> right_questions = database_->forms(right_filter && GetWordFilter(kTowardQuestion, options)).all();
if (right_questions.size() < 1) return false;
verbly::form right_question = right_questions.front();
SavePanel(name, left_question.getText() + " " + right_question.getText(), soltext, options);
return true;
}
void Generator::GenerateCrossTower(
std::string north_tower_name,
std::string south_tower_name,
std::string east_tower_name,
std::string west_tower_name,
std::string north_lookout_name,
std::string south_lookout_name,
std::string east_lookout_name,
std::string west_lookout_name,
std::string north_other_name1,
std::string north_other_name2,
std::string north_other_name3,
std::string south_other_name1,
std::string south_other_name2,
std::string south_other_name3,
std::string east_other_name1,
std::string east_other_name2,
std::string east_other_name3,
std::string west_other_name1,
std::string west_other_name2,
std::string west_other_name3)
{
std::vector<std::vector<std::string>> sets = cross_tower_->GetPuzzleSet(rng_);
SavePanel(north_tower_name, "", sets[0][0]);
SavePanel(north_lookout_name, "", sets[0][0]);
SavePanel(north_other_name1, sets[0][1], sets[0][1]);
SavePanel(north_other_name2, sets[0][2], sets[0][2]);
SavePanel(north_other_name3, sets[0][3], sets[0][3]);
SavePanel(south_tower_name, "", sets[1][0]);
SavePanel(south_lookout_name, "", sets[1][0]);
SavePanel(south_other_name1, sets[1][1], sets[1][1]);
SavePanel(south_other_name2, sets[1][2], sets[1][2]);
SavePanel(south_other_name3, sets[1][3], sets[1][3]);
SavePanel(east_tower_name, "", sets[2][0]);
SavePanel(east_lookout_name, "", sets[2][0]);
SavePanel(east_other_name1, sets[2][1], sets[2][1]);
SavePanel(east_other_name2, sets[2][2], sets[2][2]);
SavePanel(east_other_name3, sets[2][3], sets[2][3]);
SavePanel(west_tower_name, "", sets[3][0]);
SavePanel(west_lookout_name, "", sets[3][0]);
SavePanel(west_other_name1, sets[3][1], sets[3][1]);
SavePanel(west_other_name2, sets[3][2], sets[3][2]);
SavePanel(west_other_name3, sets[3][3], sets[3][3]);
}
void Generator::SavePanel(std::string name, std::string question, std::string answer, GenerateOptions options) {
if (options.save_for_later) {
reusable_.push_back(answer);
}
if (!options.unique_pool.empty()) {
pools_[options.unique_pool].insert(answer);
}
if (options.obscure_hint) {
int numToObscure = (question.size()/3 > 0) ? std::uniform_int_distribution<int>(1, question.size()/3)(rng_) : 1;
std::vector<int> indicies(question.size());
std::iota(indicies.begin(), indicies.end(), 0);
std::shuffle(indicies.begin(), indicies.end(), rng_);
for (int i=0; i<numToObscure; i++) {
if (question[indicies[i]] != ' ' && question[indicies[i]] != '-') {
question[indicies[i]] = '?';
}
}
}
panels_[name] = std::make_tuple(question, answer);
std::cout << name << ": " << question << "? " << answer << "!" << std::endl;
}
verbly::filter Generator::GetWordFilter(FilterDirection dir, GenerateOptions options) const {
verbly::filter wordFilter =
(verbly::form::proper == false);
wordFilter &= (
!(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)) // ethnic slurs
&& !(verbly::notion::wnid == 110630093) // "spastic"
&& !(verbly::notion::fullHypernyms %= (verbly::notion::wnid == 100844254))); // sexual activity
if (options.exact_len > 0) {
wordFilter &= (verbly::form::length == options.exact_len);
} else if (dir == kTowardSolution && options.max_answer_len > 0) {
wordFilter &= (verbly::form::length <= options.max_answer_len);
} else if (dir == kTowardQuestion && options.max_hint_len > 0) {
wordFilter &= (verbly::form::length <= options.max_hint_len);
} else {
wordFilter &= (verbly::form::length <= 11);
}
if (options.exact_len == 0) {
wordFilter &= (verbly::form::length >= 3);
}
if (!options.multiword) {
if (options.force_two_words) {
wordFilter &= (verbly::form::complexity == 2);
} else {
wordFilter &= (verbly::form::complexity == 1);
wordFilter &= (verbly::form::frequency > 2000000);
}
} else {
wordFilter &= ((verbly::form::complexity > 1) || (verbly::form::frequency > 2000000));
}
return wordFilter;
}
bool isDigitWrapper(unsigned char ch) {
return std::isdigit(ch);
}
bool Generator::IsClueTrivial(Height height, Colour colour, const verbly::form& clue, const verbly::form& solution, GenerateOptions options) const
{
if (!options.allow_top_expansion && height == kTop) {
verbly::filter questionFilter = MakeHintFilter(solution, kMiddle, colour, kTowardQuestion) && clue;
if (!database_->forms(questionFilter).all().empty()) {
return true;
}
}
if (height == kTop && colour == kWhite)
{
return !database_->forms((verbly::filter)clue && (verbly::word::synonyms %= solution)).all().empty();
} else if (height == kBottom && colour == kWhite)
{
return !database_->forms((verbly::filter)clue && (verbly::form::pronunciations %= solution)).all().empty();
} else if (height == kBottom && colour == kBlack)
{
return !database_->forms((verbly::filter)clue && (verbly::form::merographs %= solution)).all().empty()
|| !database_->forms((verbly::filter)clue && (verbly::form::holographs %= solution)).all().empty();
} else if ((height == kMiddle || height == kTop) && colour == kPurple)
{
return (clue.getId() == solution.getId())
|| !database_->forms((verbly::filter)clue && (verbly::form::merographs %= solution)).all().empty()
|| !database_->forms((verbly::filter)clue && (verbly::form::holographs %= solution)).all().empty();
} else if (height == kMiddle && colour == kRed) {
if (clue.getComplexity() == 2 && solution.getComplexity() == 1) {
auto words = hatkirby::split<std::vector<std::string>>(clue.getText(), " ");
for (const auto& word : words) {
if (word == solution.getText()) {
return true;
}
}
}
} else if (height == kMiddle && colour == kYellow) {
if (clue.getComplexity() == solution.getComplexity()) {
auto clueWords = hatkirby::split<std::vector<std::string>>(clue.getText(), " ");
auto solutionWords = hatkirby::split<std::vector<std::string>>(solution.getText(), " ");
std::sort(clueWords.begin(), clueWords.end());
std::sort(solutionWords.begin(), solutionWords.end());
if (clueWords == solutionWords) {
return true;
}
}
} else if (height == kTop && colour == kYellow) {
std::set<std::string> hint_stressless;
for (const verbly::pronunciation& pronunciation : clue.getPronunciations()) {
std::string stressed = hatkirby::implode(pronunciation.getPhonemes(), " ");
std::string stressless;
std::remove_copy_if(stressed.begin(), stressed.end(), std::back_inserter(stressless), &isDigitWrapper);
hint_stressless.insert(stressless);
}
for (const verbly::pronunciation& pronunciation : solution.getPronunciations()) {
std::string stressed = hatkirby::implode(pronunciation.getPhonemes(), " ");
std::string stressless;
std::remove_copy_if(stressed.begin(), stressed.end(), std::back_inserter(stressless), &isDigitWrapper);
if (hint_stressless.count(stressless)) {
return true;
}
}
}
return false;
}
|