summary refs log tree commit diff stats
path: root/generator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'generator.cpp')
-rw-r--r--generator.cpp810
1 files changed, 0 insertions, 810 deletions
diff --git a/generator.cpp b/generator.cpp deleted file mode 100644 index 8334904..0000000 --- a/generator.cpp +++ /dev/null
@@ -1,810 +0,0 @@
1#include "generator.h"
2#include <algorithm>
3#include <iostream>
4#include <cstdlib>
5#include <cctype>
6#include <hkutil/string.h>
7
8verbly::filter Generator::MakeHintFilter(verbly::filter subfilter, Height height, Colour colour, FilterDirection filter_direction) const
9{
10 switch (colour) {
11 case kWhite: {
12 switch (height) {
13 case kBottom: {
14 return (verbly::word::synonyms %= subfilter);
15 }
16 case kTop: {
17 return (verbly::form::pronunciations %=
18 verbly::filter("homophones", false,
19 (verbly::pronunciation::forms %= (subfilter && verbly::filter(
20 verbly::form::id,
21 verbly::filter::comparison::field_does_not_equal,
22 verbly::form::id)))));
23 }
24 case kMiddle: {
25 return subfilter;
26 }
27 default: break; // Not supported yet.
28 }
29 break;
30 }
31 case kBlack: {
32 switch (height) {
33 case kBottom: {
34 return (verbly::word::antonyms %= subfilter);
35 }
36 case kMiddle: {
37 return (verbly::form::antogram %= subfilter);
38 }
39 case kTop: {
40 return (verbly::pronunciation::antophone %= subfilter);
41 }
42 default: break; // Not supported yet.
43 }
44 break;
45 }
46 case kBrown: {
47 break; // Not supported yet.
48 }
49 case kRed: {
50 switch (height) {
51 case kTop: {
52 if (filter_direction == kTowardSolution)
53 {
54 return (verbly::pronunciation::merophones %= subfilter);
55 } else {
56 return (verbly::pronunciation::holophones %= subfilter);
57 }
58 }
59 case kMiddle: {
60 if (filter_direction == kTowardSolution)
61 {
62 return (verbly::form::merographs %= subfilter);
63 } else {
64 return (verbly::form::holographs %= subfilter);
65 }
66 }
67 case kBottom: {
68 if (filter_direction == kTowardSolution)
69 {
70 return (verbly::notion::partMeronyms %=
71 verbly::filter("partMeronyms", false,
72 subfilter && verbly::filter(
73 verbly::form::id,
74 verbly::filter::comparison::field_does_not_equal,
75 verbly::form::id)));
76 } else {
77 return (verbly::notion::partHolonyms %=
78 verbly::filter("partHolonyms", false,
79 subfilter && verbly::filter(
80 verbly::form::id,
81 verbly::filter::comparison::field_does_not_equal,
82 verbly::form::id)));
83 }
84 }
85 default: break; // Not supported yet.
86 }
87 break;
88 }
89 case kBlue: {
90 switch (height) {
91 case kTop: {
92 if (filter_direction == kTowardSolution)
93 {
94 return (verbly::pronunciation::holophones %= subfilter);
95 } else {
96 return (verbly::pronunciation::merophones %= subfilter);
97 }
98 }
99 case kMiddle: {
100 if (filter_direction == kTowardSolution)
101 {
102 return (verbly::form::holographs %= subfilter);
103 } else {
104 return (verbly::form::merographs %= subfilter);
105 }
106 }
107 case kBottom: {
108 if (filter_direction == kTowardSolution)
109 {
110 return (verbly::notion::partHolonyms %=
111 verbly::filter("partHolonyms", false,
112 subfilter && verbly::filter(
113 verbly::form::id,
114 verbly::filter::comparison::field_does_not_equal,
115 verbly::form::id)));
116 } else {
117 return (verbly::notion::partMeronyms %=
118 verbly::filter("partMeronyms", false,
119 subfilter && verbly::filter(
120 verbly::form::id,
121 verbly::filter::comparison::field_does_not_equal,
122 verbly::form::id)));
123 }
124 }
125 default: break; // Not supported yet.
126 }
127 break;
128 }
129 case kPurple: {
130 switch (height) {
131 case kMiddle: {
132 return (verbly::form::holographs %=
133 verbly::filter("midpurp", false,
134 (verbly::form::length >= 4 && (verbly::form::merographs %=
135 (subfilter && verbly::filter(
136 verbly::form::id,
137 verbly::filter::comparison::field_does_not_equal,
138 verbly::form::id))))));
139 }
140 case kTop: {
141 return (verbly::pronunciation::rhymes %= subfilter);
142 }
143 default: break; // Not supported yet.
144 }
145 break;
146 }
147 case kYellow: {
148 switch (height) {
149 case kTop: {
150 return (verbly::pronunciation::anaphones %= (subfilter && verbly::filter(
151 verbly::pronunciation::id,
152 verbly::filter::comparison::field_does_not_equal,
153 verbly::pronunciation::id)));
154 }
155 case kMiddle: {
156 return (verbly::form::anagrams %= (subfilter && verbly::filter(
157 verbly::form::id,
158 verbly::filter::comparison::field_does_not_equal,
159 verbly::form::id)));
160 }
161 default: break; // Not supported yet.
162 }
163 break;
164 }
165 case kGreen: {
166 if (filter_direction == kTowardSolution)
167 {
168 switch (height) {
169 case kBottom: {
170 verbly::filter whitelist =
171 (verbly::notion::wnid == 109287968) // Geological formations
172 || (verbly::notion::wnid == 109208496) // Asterisms (collections of stars)
173 || (verbly::notion::wnid == 109239740) // Celestial bodies
174 || (verbly::notion::wnid == 109277686) // Exterrestrial objects (comets and meteroids)
175 || (verbly::notion::wnid == 109403211) // Radiators (supposedly natural radiators but actually these are just pictures of radiators)
176 || (verbly::notion::wnid == 109416076) // Rocks
177 || (verbly::notion::wnid == 105442131) // Chromosomes
178 || (verbly::notion::wnid == 100324978) // Tightrope walking
179 || (verbly::notion::wnid == 100326094) // Rock climbing
180 || (verbly::notion::wnid == 100433458) // Contact sports
181 || (verbly::notion::wnid == 100433802) // Gymnastics
182 || (verbly::notion::wnid == 100439826) // Track and field
183 || (verbly::notion::wnid == 100440747) // Skiing
184 || (verbly::notion::wnid == 100441824) // Water sport
185 || (verbly::notion::wnid == 100445351) // Rowing
186 || (verbly::notion::wnid == 100446980) // Archery
187 // TODO: add more sports
188 || (verbly::notion::wnid == 100021939) // Artifacts
189 || (verbly::notion::wnid == 101471682) // Vertebrates
190 ;
191
192 verbly::filter blacklist =
193 (verbly::notion::wnid == 106883725) // swastika
194 || (verbly::notion::wnid == 104416901) // tetraskele
195 || (verbly::notion::wnid == 102512053) // fish
196 || (verbly::notion::wnid == 103575691) // instrument of execution
197 || (verbly::notion::wnid == 103829563) // noose
198 || (verbly::notion::wnid == 103663910) // life support
199 ;
200
201 return subfilter
202 && (verbly::notion::fullHypernyms %= whitelist)
203 && !(verbly::notion::fullHypernyms %= blacklist)
204 && (verbly::notion::partOfSpeech == verbly::part_of_speech::noun)
205 && (verbly::notion::numOfImages >= 1);
206 }
207 case kMiddle: {
208 return subfilter;
209 }
210 default: break; // Never supported.
211 }
212 } else {
213 return (verbly::form::text == "picture");
214 }
215 break;
216 }
217 default: break; // Not supported yet.
218 }
219 return {};
220}
221
222std::string ApplyWanderlust(const std::string& word) {
223 std::string result;
224 for (char ch : word) {
225 if (ch == 'w') {
226 result += '1';
227 } else if (ch == 'a') {
228 result += '2';
229 } else if (ch == 'n') {
230 result += '3';
231 } else if (ch == 'd') {
232 result += '4';
233 } else if (ch == 'e') {
234 result += '5';
235 } else if (ch == 'r') {
236 result += '6';
237 } else if (ch == 'l') {
238 result += '7';
239 } else if (ch == 'u') {
240 result += '8';
241 } else if (ch == 's') {
242 result += '9';
243 } else if (ch == 't') {
244 result += '0';
245 } else if (ch == ' ') {
246 result += ' ';
247 }
248 }
249 return result;
250}
251
252void Generator::GenerateStaticPanel(std::string name, std::string question, std::string answer) {
253 SavePanel(name, question, answer.empty() ? question : answer, {});
254}
255
256void Generator::GenerateSinglePanel(std::string name, Height height, Colour colour, GenerateOptions options) {
257 while (!GenerateSinglePanelImpl(name, height, colour, options));
258}
259
260bool Generator::GenerateSinglePanelImpl(std::string name, Height height, Colour colour, GenerateOptions options) {
261 verbly::form solution;
262 if (options.reuse_solution) {
263 const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
264 if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
265 solution = database_->forms(verbly::form::text == word).first();
266 } else {
267 verbly::filter forward = MakeHintFilter({}, height, colour, kTowardSolution);
268 if (options.must_be_broad) {
269 forward &= MakeHintFilter({}, kBottom, kWhite, kTowardSolution);
270 forward &= MakeHintFilter({}, kBottom, kRed, kTowardSolution);
271 forward &= MakeHintFilter({}, kBottom, kBlue, kTowardSolution);
272 }
273 std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
274 solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
275 }
276
277 if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
278 return false;
279 }
280 if (options.palindrome != kPalindromeUnspecified) {
281 std::string reversed = solution.getText();
282 std::reverse(reversed.begin(), reversed.end());
283
284 if ((options.palindrome == kForcePalindrome && reversed != solution.getText()) ||
285 (options.palindrome == kRejectPalindrome && reversed == solution.getText())) {
286 return false;
287 }
288 }
289
290 // Finish early if this is a middle white.
291 if (height == kMiddle && colour == kWhite) {
292 SavePanel(name, solution.getText(), solution.getText(), options);
293 if (!options.copy_to.empty()) {
294 SavePanel(options.copy_to, solution.getText(), solution.getText(), options);
295 }
296 if (!options.copy_hidden.empty()) {
297 SavePanel(options.copy_hidden, std::string(solution.getText().size(), '?'), solution.getText(), options);
298 }
299 return true;
300 }
301
302 verbly::filter questionFilter = MakeHintFilter(solution, height, colour, kTowardQuestion);
303 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
304 if (questions.size() < 1) return false;
305 verbly::form question = questions.front();// questions.at(std::uniform_int_distribution<int>(0, questions.size())(rng_));
306
307 if (IsClueTrivial(height, colour, question, solution, options)) {
308 return false;
309 }
310 if (options.max_len_diff >= 0 && std::abs(static_cast<int>(question.getText().size() - solution.getText().size())) > options.max_len_diff) {
311 return false;
312 }
313
314 SavePanel(name, question.getText(), solution.getText(), options);
315 if (!options.copy_to.empty()) {
316 SavePanel(options.copy_to, question.getText(), solution.getText(), options);
317 }
318
319 return true;
320}
321
322void Generator::GenerateDoublePanel(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
323 while (!GenerateDoublePanelImpl(name1, name2, height, colour, options));
324}
325
326bool Generator::GenerateDoublePanelImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
327 verbly::form solution;
328 if (options.reuse_solution) {
329 const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
330 if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
331 solution = database_->forms(verbly::form::text == word).first();
332 } else {
333 verbly::filter forward = MakeHintFilter({}, height, colour, kTowardSolution);
334 if (options.must_be_broad) {
335 forward &= MakeHintFilter({}, kBottom, kWhite, kTowardSolution);
336 forward &= MakeHintFilter({}, kBottom, kRed, kTowardSolution);
337 forward &= MakeHintFilter({}, kBottom, kBlue, kTowardSolution);
338 }
339 std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
340 solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
341 }
342
343 if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
344 return false;
345 }
346
347 // Finish early if this is a middle white.
348 if (height == kMiddle && colour == kWhite) {
349 SavePanel(name1, solution.getText(), solution.getText(), options);
350 SavePanel(name2, solution.getText(), solution.getText(), options);
351 return true;
352 }
353
354 verbly::filter questionFilter = MakeHintFilter(solution, height, colour, kTowardQuestion);
355 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 2).all();
356 if (questions.size() < 2) return false;
357
358 //std::shuffle(questions.begin(), questions.end(), rng_);
359
360 if (IsClueTrivial(height, colour, questions[0], solution, options) || IsClueTrivial(height, colour, questions[1], solution, options)) {
361 return false;
362 }
363 if (options.max_len_diff >= 0) {
364 if (std::abs(static_cast<int>(questions[0].getText().size() - solution.getText().size())) > options.max_len_diff
365 || std::abs(static_cast<int>(questions[1].getText().size() - solution.getText().size())) > options.max_len_diff) {
366 return false;
367 }
368 }
369
370 SavePanel(name1, questions[0].getText(), solution.getText(), options);
371 SavePanel(name2, questions[1].getText(), solution.getText(), options);
372
373 return true;
374}
375
376void Generator::GenerateCohintedPanels(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
377 while (!GenerateCohintedPanelsImpl(name1, name2, height, colour, options));
378}
379
380bool Generator::GenerateCohintedPanelsImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
381 verbly::filter backward = MakeHintFilter({}, height, colour, kTowardQuestion);
382 std::vector<verbly::form> questions = database_->forms(backward && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
383 verbly::form question = questions.front();// solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
384
385 // Finish early if this is a middle white.
386 if (height == kMiddle && colour == kWhite) {
387 SavePanel(name1, question.getText(), question.getText(), options);
388 SavePanel(name2, question.getText(), question.getText(), options);
389 return true;
390 }
391
392 verbly::filter solutionFilter = MakeHintFilter(question, height, colour, kTowardSolution);
393 std::vector<verbly::form> solutions = database_->forms(solutionFilter && GetWordFilter(kTowardSolution, options), {}, 2).all();
394 if (solutions.size() < 2) return false;
395
396 //std::shuffle(questions.begin(), questions.end(), rng_);
397
398 if (IsClueTrivial(height, colour, question, solutions[0], options)
399 || IsClueTrivial(height, colour, question, solutions[1], options)) {
400 return false;
401 }
402 if (options.max_len_diff >= 0) {
403 if (std::abs(static_cast<int>(question.getText().size() - solutions[0].getText().size())) > options.max_len_diff
404 || std::abs(static_cast<int>(question.getText().size() - solutions[1].getText().size())) > options.max_len_diff) {
405 return false;
406 }
407 }
408
409 SavePanel(name1, question.getText(), solutions[0].getText(), options);
410 SavePanel(name2, question.getText(), solutions[1].getText(), options);
411
412 return true;
413}
414
415void Generator::GeneratePairedPanels(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
416 while (!GeneratePairedPanelsImpl(name1, name2, height, colour, options));
417}
418
419bool Generator::GeneratePairedPanelsImpl(std::string name1, std::string name2, Height height, Colour colour, GenerateOptions options) {
420 Colour effectiveColour = (height == kMiddle && colour == kWhite) ? kBlack : colour;
421 Height effectiveHeight = (height == kMiddle && colour == kWhite) ? kBottom : height;
422
423 verbly::filter forward = MakeHintFilter({}, effectiveHeight, effectiveColour, kTowardSolution);
424 std::vector<verbly::form> solutions = database_->forms(forward && GetWordFilter(kTowardSolution, options)).all();
425 verbly::form solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
426
427 verbly::filter questionFilter = MakeHintFilter(solution, effectiveHeight, effectiveColour, kTowardQuestion);
428 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options)).all();//, verbly::order(verbly::form::id), 0).all();
429 if (questions.size() < 1) return false;
430 verbly::form question = questions.front();// questions.at(std::uniform_int_distribution<int>(0, questions.size())(rng_));
431
432 if (IsClueTrivial(height, effectiveColour, question, solution, options)) {
433 return false;
434 }
435
436 if (options.palindrome != kPalindromeUnspecified) {
437 std::string reversed = question.getText();
438 std::reverse(reversed.begin(), reversed.end());
439
440 if ((options.palindrome == kForcePalindrome && reversed != question.getText()) ||
441 (options.palindrome == kRejectPalindrome && reversed == question.getText())) {
442 return false;
443 }
444 }
445
446 if (options.max_len_diff >= 0) {
447 if (std::abs(static_cast<int>(question.getText().size() - solution.getText().size())) > options.max_len_diff) {
448 return false;
449 }
450 }
451
452 if (height == kMiddle && colour == kWhite) {
453 SavePanel(name1, question.getText(), question.getText(), options);
454 SavePanel(name2, solution.getText(), solution.getText(), options);
455 if (!options.copy_to.empty() && !options.copy_to2.empty()) {
456 SavePanel(options.copy_to, question.getText(), question.getText(), options);
457 SavePanel(options.copy_to2, solution.getText(), solution.getText(), options);
458 }
459 } else {
460 SavePanel(name1, question.getText(), solution.getText(), options);
461 SavePanel(name2, solution.getText(), question.getText(), options);
462 if (!options.copy_to.empty() && !options.copy_to2.empty()) {
463 SavePanel(options.copy_to, question.getText(), solution.getText(), options);
464 SavePanel(options.copy_to2, solution.getText(), question.getText(), options);
465 }
466 }
467
468 return true;
469}
470
471void 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) {
472 while (!GeneratePanelStackImpl(top_name, top_colour, middle_name, middle_colour, bottom_name, bottom_colour, options));
473}
474
475bool 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) {
476 verbly::form solution;
477 if (options.reuse_solution) {
478 const std::string& word = reusable_.at(std::uniform_int_distribution<int>(0, reusable_.size()-1)(rng_));
479 if (options.max_answer_len > 0 && word.size() > options.max_answer_len) return false;
480 solution = database_->forms(verbly::form::text == word).first();
481 } else {
482 verbly::filter forward = GetWordFilter(kTowardSolution, options);
483 if (!top_name.empty()) {
484 forward &= MakeHintFilter({}, kTop, top_colour, kTowardSolution);
485 }
486 if (!middle_name.empty()) {
487 forward &= MakeHintFilter({}, kMiddle, middle_colour, kTowardSolution);
488 }
489 if (!bottom_name.empty()) {
490 forward &= MakeHintFilter({}, kBottom, bottom_colour, kTowardSolution);
491 }
492 std::vector<verbly::form> solutions = database_->forms(forward).all();
493 solution = solutions.front();//solutions.at(std::uniform_int_distribution<int>(0, solutions.size())(rng_));
494 }
495
496 if (!options.unique_pool.empty() && pools_[options.unique_pool].count(solution.getText())) {
497 return false;
498 }
499
500 std::string top_hint;
501 std::string middle_hint;
502 std::string bottom_hint;
503
504 if (!top_name.empty()) {
505 verbly::filter questionFilter = MakeHintFilter(solution, kTop, top_colour, kTowardQuestion);
506 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
507 if (questions.empty()) return false;
508 top_hint = questions.front().getText();
509
510 if (IsClueTrivial(kTop, top_colour, questions.front(), solution, options)) {
511 return false;
512 }
513 }
514
515 if (!middle_name.empty()) {
516 verbly::filter questionFilter = MakeHintFilter(solution, kMiddle, middle_colour, kTowardQuestion);
517 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
518 if (questions.empty()) return false;
519 middle_hint = questions.front().getText();
520
521 if (IsClueTrivial(kMiddle, middle_colour, questions.front(), solution, options)) {
522 return false;
523 }
524 }
525
526 if (!bottom_name.empty()) {
527 verbly::filter questionFilter = MakeHintFilter(solution, kBottom, bottom_colour, kTowardQuestion);
528 std::vector<verbly::form> questions = database_->forms(questionFilter && GetWordFilter(kTowardQuestion, options), {}, 1).all();
529 if (questions.empty()) return false;
530 bottom_hint = questions.front().getText();
531
532 if (IsClueTrivial(kBottom, bottom_colour, questions.front(), solution, options)) {
533 return false;
534 }
535 }
536
537 if (!top_name.empty()) {
538 SavePanel(top_name, top_hint, solution.getText(), options);
539 }
540 if (!middle_name.empty()) {
541 SavePanel(middle_name, middle_hint, solution.getText(), options);
542 }
543 if (!bottom_name.empty()) {
544 SavePanel(bottom_name, bottom_hint, solution.getText(), options);
545 }
546
547 return true;
548}
549
550void Generator::GenerateOrangeNumberPanel(std::string name) {
551 std::string solution = wanderlust_->GetWord(rng_);
552 std::string question = ApplyWanderlust(solution);
553
554 SavePanel(name, question, solution);
555}
556
557void Generator::GenerateOrangeWordPanel(std::string name) {
558 std::string question = wanderlust_->GetWord(rng_);
559 std::string solution = ApplyWanderlust(question);
560
561 SavePanel(name, question, solution);
562}
563
564void Generator::GenerateOrangeAdditionPanel(std::string name) {
565 auto [question, solution] = wanderlust_->GetPuzzle(rng_);
566
567 SavePanel(name, question, solution);
568}
569
570void Generator::GenerateOneRoadManyTurns(std::string order_name, std::string part1_name, std::string part2_name, std::string part3_name, std::string part4_name) {
571 const auto& [part1_q, part1_a] = panels_.at(part1_name);
572 const auto& [part2_q, part2_a] = panels_.at(part2_name);
573 const auto& [part3_q, part3_a] = panels_.at(part3_name);
574 const auto& [part4_q, part4_a] = panels_.at(part4_name);
575
576 SavePanel(order_name, "order", part1_a + " " + part2_a + " " + part3_a + " " + part4_a);
577}
578
579void Generator::GenerateComboPanel(std::string name, Height left_height, Colour left_colour, Height right_height, Colour right_colour, GenerateOptions options) {
580 while (!GenerateComboPanelImpl(name, left_height, left_colour, right_height, right_colour, options));
581}
582
583bool Generator::GenerateComboPanelImpl(std::string name, Height left_height, Colour left_colour, Height right_height, Colour right_colour, GenerateOptions options) {
584 options.force_two_words = true;
585
586 verbly::form solution = database_->forms(GetWordFilter(kTowardSolution, options)).first();
587 std::string soltext = solution.getText();
588 int spacepos = soltext.find(" ");
589 std::string leftword = soltext.substr(0, spacepos);
590 std::string rightword = soltext.substr(spacepos+1);
591
592 options.force_two_words = false;
593 verbly::filter left_filter = MakeHintFilter(verbly::form::text == leftword, left_height, left_colour, kTowardQuestion);
594 std::vector<verbly::form> left_questions = database_->forms(left_filter && GetWordFilter(kTowardQuestion, options)).all();
595 if (left_questions.size() < 1) return false;
596 verbly::form left_question = left_questions.front();
597
598 verbly::filter right_filter = MakeHintFilter(verbly::form::text == rightword, right_height, right_colour, kTowardQuestion);
599 std::vector<verbly::form> right_questions = database_->forms(right_filter && GetWordFilter(kTowardQuestion, options)).all();
600 if (right_questions.size() < 1) return false;
601 verbly::form right_question = right_questions.front();
602
603 SavePanel(name, left_question.getText() + " " + right_question.getText(), soltext, options);
604
605 return true;
606}
607
608void Generator::GenerateCrossTower(
609 std::string north_tower_name,
610 std::string south_tower_name,
611 std::string east_tower_name,
612 std::string west_tower_name,
613 std::string north_lookout_name,
614 std::string south_lookout_name,
615 std::string east_lookout_name,
616 std::string west_lookout_name,
617 std::string north_other_name1,
618 std::string north_other_name2,
619 std::string north_other_name3,
620 std::string south_other_name1,
621 std::string south_other_name2,
622 std::string south_other_name3,
623 std::string east_other_name1,
624 std::string east_other_name2,
625 std::string east_other_name3,
626 std::string west_other_name1,
627 std::string west_other_name2,
628 std::string west_other_name3)
629{
630 std::vector<std::vector<std::string>> sets = cross_tower_->GetPuzzleSet(rng_);
631
632 SavePanel(north_tower_name, "", sets[0][0]);
633 SavePanel(north_lookout_name, "", sets[0][0]);
634 SavePanel(north_other_name1, sets[0][1], sets[0][1]);
635 SavePanel(north_other_name2, sets[0][2], sets[0][2]);
636 SavePanel(north_other_name3, sets[0][3], sets[0][3]);
637
638 SavePanel(south_tower_name, "", sets[1][0]);
639 SavePanel(south_lookout_name, "", sets[1][0]);
640 SavePanel(south_other_name1, sets[1][1], sets[1][1]);
641 SavePanel(south_other_name2, sets[1][2], sets[1][2]);
642 SavePanel(south_other_name3, sets[1][3], sets[1][3]);
643
644 SavePanel(east_tower_name, "", sets[2][0]);
645 SavePanel(east_lookout_name, "", sets[2][0]);
646 SavePanel(east_other_name1, sets[2][1], sets[2][1]);
647 SavePanel(east_other_name2, sets[2][2], sets[2][2]);
648 SavePanel(east_other_name3, sets[2][3], sets[2][3]);
649
650 SavePanel(west_tower_name, "", sets[3][0]);
651 SavePanel(west_lookout_name, "", sets[3][0]);
652 SavePanel(west_other_name1, sets[3][1], sets[3][1]);
653 SavePanel(west_other_name2, sets[3][2], sets[3][2]);
654 SavePanel(west_other_name3, sets[3][3], sets[3][3]);
655}
656
657void Generator::GeneratePaintingPuzzle(std::string panel_name, std::string painting_name) {
658 std::string node_name;
659 std::string answer;
660 int resource_id = 0;
661
662 for (;;) {
663 std::tie(node_name, answer, resource_id) = paintings_->GetPainting(rng_);
664 if (!used_paintings_.count(node_name)) {
665 break;
666 }
667 }
668
669 used_paintings_.insert(node_name);
670
671 SavePanel(panel_name, "painting", answer, {});
672
673 std::string resource_path = std::string("res://nodes/paintings/") + node_name + ".tscn";
674 if (resource_id == 0) {
675 resources_.emplace_back(resource_path, "PackedScene");
676 }
677
678 replace_nodes_[painting_name] = {resource_path, resource_id};
679}
680
681void Generator::SavePanel(std::string name, std::string question, std::string answer, GenerateOptions options) {
682 if (options.save_for_later) {
683 reusable_.push_back(answer);
684 }
685
686 if (!options.unique_pool.empty()) {
687 pools_[options.unique_pool].insert(answer);
688 }
689
690 if (options.obscure_hint) {
691 int numToObscure = (question.size()/3 > 0) ? std::uniform_int_distribution<int>(1, question.size()/3)(rng_) : 1;
692 std::vector<int> indicies(question.size());
693 std::iota(indicies.begin(), indicies.end(), 0);
694 std::shuffle(indicies.begin(), indicies.end(), rng_);
695
696 for (int i=0; i<numToObscure; i++) {
697 if (question[indicies[i]] != ' ' && question[indicies[i]] != '-') {
698 question[indicies[i]] = '?';
699 }
700 }
701 }
702
703 panels_[name] = std::make_tuple(question, answer);
704
705 std::cout << name << ": " << question << "? " << answer << "!" << std::endl;
706}
707
708verbly::filter Generator::GetWordFilter(FilterDirection dir, GenerateOptions options) const {
709 verbly::filter wordFilter =
710 (verbly::form::proper == false);
711
712 wordFilter &= (
713 !(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)) // ethnic slurs
714 && !(verbly::notion::wnid == 110630093) // "spastic"
715 && !(verbly::notion::fullHypernyms %= (verbly::notion::wnid == 100844254))); // sexual activity
716
717 if (options.exact_len > 0) {
718 wordFilter &= (verbly::form::length == options.exact_len);
719 } else if (dir == kTowardSolution && options.max_answer_len > 0) {
720 wordFilter &= (verbly::form::length <= options.max_answer_len);
721 } else if (dir == kTowardQuestion && options.max_hint_len > 0) {
722 wordFilter &= (verbly::form::length <= options.max_hint_len);
723 } else {
724 wordFilter &= (verbly::form::length <= 11);
725 }
726
727 if (options.exact_len == 0) {
728 wordFilter &= (verbly::form::length >= 3);
729 }
730
731 if (!options.multiword) {
732 if (options.force_two_words) {
733 wordFilter &= (verbly::form::complexity == 2);
734 } else {
735 wordFilter &= (verbly::form::complexity == 1);
736 wordFilter &= (verbly::form::frequency > 2000000);
737 }
738 } else {
739 wordFilter &= ((verbly::form::complexity > 1) || (verbly::form::frequency > 2000000));
740 }
741
742 return wordFilter;
743}
744
745bool isDigitWrapper(unsigned char ch) {
746 return std::isdigit(ch);
747}
748
749bool Generator::IsClueTrivial(Height height, Colour colour, const verbly::form& clue, const verbly::form& solution, GenerateOptions options) const
750{
751 if (!options.allow_top_expansion && height == kTop) {
752 verbly::filter questionFilter = MakeHintFilter(solution, kMiddle, colour, kTowardQuestion) && clue;
753 if (!database_->forms(questionFilter).all().empty()) {
754 return true;
755 }
756 }
757
758 if (height == kTop && colour == kWhite)
759 {
760 return !database_->forms((verbly::filter)clue && (verbly::word::synonyms %= solution)).all().empty();
761 } else if (height == kBottom && colour == kWhite)
762 {
763 return !database_->forms((verbly::filter)clue && (verbly::form::pronunciations %= solution)).all().empty();
764 } else if (height == kBottom && colour == kBlack)
765 {
766 return !database_->forms((verbly::filter)clue && (verbly::form::merographs %= solution)).all().empty()
767 || !database_->forms((verbly::filter)clue && (verbly::form::holographs %= solution)).all().empty();
768 } else if ((height == kMiddle || height == kTop) && colour == kPurple)
769 {
770 return (clue.getId() == solution.getId())
771 || !database_->forms((verbly::filter)clue && (verbly::form::merographs %= solution)).all().empty()
772 || !database_->forms((verbly::filter)clue && (verbly::form::holographs %= solution)).all().empty();
773 } else if (height == kMiddle && colour == kRed) {
774 if (clue.getComplexity() == 2 && solution.getComplexity() == 1) {
775 auto words = hatkirby::split<std::vector<std::string>>(clue.getText(), " ");
776 for (const auto& word : words) {
777 if (word == solution.getText()) {
778 return true;
779 }
780 }
781 }
782 } else if (height == kMiddle && colour == kYellow) {
783 if (clue.getComplexity() == solution.getComplexity()) {
784 auto clueWords = hatkirby::split<std::vector<std::string>>(clue.getText(), " ");
785 auto solutionWords = hatkirby::split<std::vector<std::string>>(solution.getText(), " ");
786 std::sort(clueWords.begin(), clueWords.end());
787 std::sort(solutionWords.begin(), solutionWords.end());
788 if (clueWords == solutionWords) {
789 return true;
790 }
791 }
792 } else if (height == kTop && colour == kYellow) {
793 std::set<std::string> hint_stressless;
794 for (const verbly::pronunciation& pronunciation : clue.getPronunciations()) {
795 std::string stressed = hatkirby::implode(pronunciation.getPhonemes(), " ");
796 std::string stressless;
797 std::remove_copy_if(stressed.begin(), stressed.end(), std::back_inserter(stressless), &isDigitWrapper);
798 hint_stressless.insert(stressless);
799 }
800 for (const verbly::pronunciation& pronunciation : solution.getPronunciations()) {
801 std::string stressed = hatkirby::implode(pronunciation.getPhonemes(), " ");
802 std::string stressless;
803 std::remove_copy_if(stressed.begin(), stressed.end(), std::back_inserter(stressless), &isDigitWrapper);
804 if (hint_stressless.count(stressless)) {
805 return true;
806 }
807 }
808 }
809 return false;
810}