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