/* * cocos2d for iPhone: http://www.cocos2d-iphone.org * * Copyright (c) 2008-2010 Ricardo Quesada * Copyright (c) 2011 Zynga Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #import "CCMenu.h" #import "CCDirector.h" #import "Support/CGPointExtension.h" #import "ccMacros.h" #import #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED #import "Platforms/iOS/CCDirectorIOS.h" #import "Platforms/iOS/CCTouchDispatcher.h" #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) #import "Platforms/Mac/MacGLView.h" #import "Platforms/Mac/CCDirectorMac.h" #endif enum { kDefaultPadding = 5, }; @implementation CCMenu @synthesize opacity = opacity_, color = color_; - (id) init { NSAssert(NO, @"CCMenu: Init not supported."); [self release]; return nil; } +(id) menuWithItems: (CCMenuItem*) item, ... { va_list args; va_start(args,item); id s = [[[self alloc] initWithItems: item vaList:args] autorelease]; va_end(args); return s; } -(id) initWithItems: (CCMenuItem*) item vaList: (va_list) args { if( (self=[super init]) ) { #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED self.isTouchEnabled = YES; #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) self.isMouseEnabled = YES; #endif // menu in the center of the screen CGSize s = [[CCDirector sharedDirector] winSize]; self.isRelativeAnchorPoint = NO; anchorPoint_ = ccp(0.5f, 0.5f); [self setContentSize:s]; // XXX: in v0.7, winSize should return the visible size // XXX: so the bar calculation should be done there #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED CGRect r = [[UIApplication sharedApplication] statusBarFrame]; ccDeviceOrientation orientation = [[CCDirector sharedDirector] deviceOrientation]; if( orientation == CCDeviceOrientationLandscapeLeft || orientation == CCDeviceOrientationLandscapeRight ) s.height -= r.size.width; else s.height -= r.size.height; #endif self.position = ccp(s.width/2, s.height/2); int z=0; if (item) { [self addChild: item z:z]; CCMenuItem *i = va_arg(args, CCMenuItem*); while(i) { z++; [self addChild: i z:z]; i = va_arg(args, CCMenuItem*); } } // [self alignItemsVertically]; selectedItem_ = nil; state_ = kCCMenuStateWaiting; } return self; } -(void) dealloc { [super dealloc]; } /* * override add: */ -(void) addChild:(CCMenuItem*)child z:(NSInteger)z tag:(NSInteger) aTag { NSAssert( [child isKindOfClass:[CCMenuItem class]], @"Menu only supports MenuItem objects as children"); [super addChild:child z:z tag:aTag]; } - (void) onExit { if(state_ == kCCMenuStateTrackingTouch) { [selectedItem_ unselected]; state_ = kCCMenuStateWaiting; selectedItem_ = nil; } [super onExit]; } #pragma mark Menu - Touches #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED -(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES]; } -(CCMenuItem *) itemForTouch: (UITouch *) touch { CGPoint touchLocation = [touch locationInView: [touch view]]; touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; CCMenuItem* item; CCARRAY_FOREACH(children_, item){ // ignore invisible and disabled items: issue #779, #866 if ( [item visible] && [item isEnabled] ) { CGPoint local = [item convertToNodeSpace:touchLocation]; CGRect r = [item rect]; r.origin = CGPointZero; if( CGRectContainsPoint( r, local ) ) return item; } } return nil; } -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { if( state_ != kCCMenuStateWaiting || !visible_ ) return NO; selectedItem_ = [self itemForTouch:touch]; [selectedItem_ selected]; if( selectedItem_ ) { state_ = kCCMenuStateTrackingTouch; return YES; } return NO; } -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { NSAssert(state_ == kCCMenuStateTrackingTouch, @"[Menu ccTouchEnded] -- invalid state"); [selectedItem_ unselected]; [selectedItem_ activate]; state_ = kCCMenuStateWaiting; } -(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event { NSAssert(state_ == kCCMenuStateTrackingTouch, @"[Menu ccTouchCancelled] -- invalid state"); [selectedItem_ unselected]; state_ = kCCMenuStateWaiting; } -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { NSAssert(state_ == kCCMenuStateTrackingTouch, @"[Menu ccTouchMoved] -- invalid state"); CCMenuItem *currentItem = [self itemForTouch:touch]; if (currentItem != selectedItem_) { [selectedItem_ unselected]; selectedItem_ = currentItem; [selectedItem_ selected]; } } #pragma mark Menu - Mouse #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) -(NSInteger) mouseDelegatePriority { return kCCMenuMousePriority+1; } -(CCMenuItem *) itemForMouseEvent: (NSEvent *) event { CGPoint location = [(CCDirectorMac*)[CCDirector sharedDirector] convertEventToGL:event]; CCMenuItem* item; CCARRAY_FOREACH(children_, item){ // ignore invisible and disabled items: issue #779, #866 if ( [item visible] && [item isEnabled] ) { CGPoint local = [item convertToNodeSpace:location]; CGRect r = [item rect]; r.origin = CGPointZero; if( CGRectContainsPoint( r, local ) ) return item; } } return nil; } -(BOOL) ccMouseUp:(NSEvent *)event { if( ! visible_ ) return NO; if(state_ == kCCMenuStateTrackingTouch) { if( selectedItem_ ) { [selectedItem_ unselected]; [selectedItem_ activate]; } state_ = kCCMenuStateWaiting; return YES; } return NO; } -(BOOL) ccMouseDown:(NSEvent *)event { if( ! visible_ ) return NO; selectedItem_ = [self itemForMouseEvent:event]; [selectedItem_ selected]; if( selectedItem_ ) { state_ = kCCMenuStateTrackingTouch; return YES; } return NO; } -(BOOL) ccMouseDragged:(NSEvent *)event { if( ! visible_ ) return NO; if(state_ == kCCMenuStateTrackingTouch) { CCMenuItem *currentItem = [self itemForMouseEvent:event]; if (currentItem != selectedItem_) { [selectedItem_ unselected]; selectedItem_ = currentItem; [selectedItem_ selected]; } return YES; } return NO; } #endif // Mac Mouse support #pragma mark Menu - Alignment -(void) alignItemsVertically { [self alignItemsVerticallyWithPadding:kDefaultPadding]; } -(void) alignItemsVerticallyWithPadding:(float)padding { float height = -padding; CCMenuItem *item; CCARRAY_FOREACH(children_, item) height += item.contentSize.height * item.scaleY + padding; float y = height / 2.0f; CCARRAY_FOREACH(children_, item) { CGSize itemSize = item.contentSize; [item setPosition:ccp(0, y - itemSize.height * item.scaleY / 2.0f)]; y -= itemSize.height * item.scaleY + padding; } } -(void) alignItemsHorizontally { [self alignItemsHorizontallyWithPadding:kDefaultPadding]; } -(void) alignItemsHorizontallyWithPadding:(float)padding { float width = -padding; CCMenuItem *item; CCARRAY_FOREACH(children_, item) width += item.contentSize.width * item.scaleX + padding; float x = -width / 2.0f; CCARRAY_FOREACH(children_, item){ CGSize itemSize = item.contentSize; [item setPosition:ccp(x + itemSize.width * item.scaleX / 2.0f, 0)]; x += itemSize.width * item.scaleX + padding; } } -(void) alignItemsInColumns: (NSNumber *) columns, ... { va_list args; va_start(args, columns); [self alignItemsInColumns:columns vaList:args]; va_end(args); } -(void) alignItemsInColumns: (NSNumber *) columns vaList: (va_list) args { NSMutableArray *rows = [[NSMutableArray alloc] initWithObjects:columns, nil]; columns = va_arg(args, NSNumber*); while(columns) { [rows addObject:columns]; columns = va_arg(args, NSNumber*); } int height = -5; NSUInteger row = 0, rowHeight = 0, columnsOccupied = 0, rowColumns; CCMenuItem *item; CCARRAY_FOREACH(children_, item){ NSAssert( row < [rows count], @"Too many menu items for the amount of rows/columns."); rowColumns = [(NSNumber *) [rows objectAtIndex:row] unsignedIntegerValue]; NSAssert( rowColumns, @"Can't have zero columns on a row"); rowHeight = fmaxf(rowHeight, item.contentSize.height); ++columnsOccupied; if(columnsOccupied >= rowColumns) { height += rowHeight + 5; columnsOccupied = 0; rowHeight = 0; ++row; } } NSAssert( !columnsOccupied, @"Too many rows/columns for available menu items." ); CGSize winSize = [[CCDirector sharedDirector] winSize]; row = 0; rowHeight = 0; rowColumns = 0; float w, x, y = height / 2; CCARRAY_FOREACH(children_, item) { if(rowColumns == 0) { rowColumns = [(NSNumber *) [rows objectAtIndex:row] unsignedIntegerValue]; w = winSize.width / (1 + rowColumns); x = w; } CGSize itemSize = item.contentSize; rowHeight = fmaxf(rowHeight, itemSize.height); [item setPosition:ccp(x - winSize.width / 2, y - itemSize.height / 2)]; x += w; ++columnsOccupied; if(columnsOccupied >= rowColumns) { y -= rowHeight + 5; columnsOccupied = 0; rowColumns = 0; rowHeight = 0; ++row; } } [rows release]; } -(void) alignItemsInRows: (NSNumber *) rows, ... { va_list args; va_start(args, rows); [self alignItemsInRows:rows vaList:args]; va_end(args); } -(void) alignItemsInRows: (NSNumber *) rows vaList: (va_list) args { NSMutableArray *columns = [[NSMutableArray alloc] initWithObjects:rows, nil]; rows = va_arg(args, NSNumber*); while(rows) { [columns addObject:rows]; rows = va_arg(args, NSNumber*); } NSMutableArray *columnWidths = [[NSMutableArray alloc] init]; NSMutableArray *columnHeights = [[NSMutableArray alloc] init]; int width = -10, columnHeight = -5; NSUInteger column = 0, columnWidth = 0, rowsOccupied = 0, columnRows; CCMenuItem *item; CCARRAY_FOREACH(children_, item){ NSAssert( column < [columns count], @"Too many menu items for the amount of rows/columns."); columnRows = [(NSNumber *) [columns objectAtIndex:column] unsignedIntegerValue]; NSAssert( columnRows, @"Can't have zero rows on a column"); CGSize itemSize = item.contentSize; columnWidth = fmaxf(columnWidth, itemSize.width); columnHeight += itemSize.height + 5; ++rowsOccupied; if(rowsOccupied >= columnRows) { [columnWidths addObject:[NSNumber numberWithUnsignedInteger:columnWidth]]; [columnHeights addObject:[NSNumber numberWithUnsignedInteger:columnHeight]]; width += columnWidth + 10; rowsOccupied = 0; columnWidth = 0; columnHeight = -5; ++column; } } NSAssert( !rowsOccupied, @"Too many rows/columns for available menu items."); CGSize winSize = [[CCDirector sharedDirector] winSize]; column = 0; columnWidth = 0; columnRows = 0; float x = -width / 2, y; CCARRAY_FOREACH(children_, item){ if(columnRows == 0) { columnRows = [(NSNumber *) [columns objectAtIndex:column] unsignedIntegerValue]; y = ([(NSNumber *) [columnHeights objectAtIndex:column] intValue] + winSize.height) / 2; } CGSize itemSize = item.contentSize; columnWidth = fmaxf(columnWidth, itemSize.width); [item setPosition:ccp(x + [(NSNumber *) [columnWidths objectAtIndex:column] unsignedIntegerValue] / 2, y - winSize.height / 2)]; y -= itemSize.height + 10; ++rowsOccupied; if(rowsOccupied >= columnRows) { x += columnWidth + 5; rowsOccupied = 0; columnRows = 0; columnWidth = 0; ++column; } } [columns release]; [columnWidths release]; [columnHeights release]; } #pragma mark Menu - Opacity Protocol /** Override synthesized setOpacity to recurse items */ - (void) setOpacity:(GLubyte)newOpacity { opacity_ = newOpacity; id item; CCARRAY_FOREACH(children_, item) [item setOpacity:opacity_]; } -(void) setColor:(ccColor3B)color { color_ = color; id item; CCARRAY_FOREACH(children_, item) [item setColor:color_]; } @end n292'>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 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
#include "generator.h"

#include <fmt/core.h>
#include <hkutil/progress.h>
#include <hkutil/string.h>

#include <algorithm>
#include <filesystem>
#include <fstream>
#include <list>
#include <regex>
#include <set>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

constexpr int MIN_FREQUENCY = 2000000;

namespace {

std::list<std::string> readFile(std::string path, bool uniq = false) {
  std::ifstream file(path);
  if (!file) {
    throw std::invalid_argument("Could not find file " + path);
  }

  std::list<std::string> lines;
  std::string line;
  while (std::getline(file, line)) {
    if (line.back() == '\r') {
      line.pop_back();
    }

    lines.push_back(line);
  }

  if (uniq) {
    std::vector<std::string> uniq(std::begin(lines), std::end(lines));
    lines.clear();

    std::sort(std::begin(uniq), std::end(uniq));
    std::unique_copy(std::begin(uniq), std::end(uniq),
                     std::back_inserter(lines));
  }

  return lines;
}

}  // namespace

generator::generator(std::string agidPath, std::string wordNetPath,
                     std::string cmudictPath, std::string wordfreqPath,
                     std::string datadirPath, std::string outputPath)
    : agidPath_(agidPath),
      wordNetPath_(wordNetPath),
      cmudictPath_(cmudictPath),
      wordfreqPath_(wordfreqPath),
      datadirPath_(datadirPath),
      outputPath_(outputPath) {
  // Ensure AGID infl.txt exists
  if (!std::ifstream(agidPath_)) {
    throw std::invalid_argument("AGID infl.txt file not found");
  }

  // Add directory separator to WordNet path
  if ((wordNetPath_.back() != '/') && (wordNetPath_.back() != '\\')) {
    wordNetPath_ += '/';
  }

  // Ensure WordNet tables exist
  for (std::string table : {"s", "sk", "ant", "at", "cls", "hyp", "ins", "mm",
                            "mp", "ms", "per", "sa", "sim", "syntax"}) {
    if (!std::ifstream(wordNetPath_ + "wn_" + table + ".pl")) {
      throw std::invalid_argument("WordNet " + table + " table not found");
    }
  }

  // Ensure CMUDICT file exists
  if (!std::ifstream(cmudictPath_)) {
    throw std::invalid_argument("CMUDICT file not found");
  }
}

void generator::run() {
  std::unordered_map<std::string, int> word_frequencies;
  {
    std::list<std::string> lines(readFile(wordfreqPath_));

    hatkirby::progress ppgs("Reading word frequencies...", lines.size());

    for (std::string line : lines) {
      ppgs.update();

      std::regex freqline("([a-z]+),([0-9]+)");
      std::smatch freqline_data;
      if (std::regex_search(line, freqline_data, freqline)) {
        std::string text = freqline_data[1];
        std::string freqnumstr = freqline_data[2];
        long long freqnumnum = std::atoll(freqnumstr.c_str());
        word_frequencies[text] = freqnumnum > std::numeric_limits<int>::max()
                                     ? std::numeric_limits<int>::max()
                                     : freqnumnum;
      }
    }
  }

  std::unordered_set<std::string> profane;
  {
    std::list<std::string> lines(readFile(datadirPath_ / "profane.txt"));
    for (const std::string& line : lines) {
      profane.insert(line);
    }
  }

  {
    std::list<std::string> lines(readFile(wordNetPath_ + "wn_s.pl"));
    hatkirby::progress ppgs("Reading synsets from WordNet...", lines.size());

    for (std::string line : lines) {
      ppgs.update();

      std::regex relation(
          "^s\\(([1234]\\d{8}),(\\d+),'(.+)',\\w,\\d+,(\\d+)\\)\\.$");

      std::smatch relation_data;
      if (!std::regex_search(line, relation_data, relation)) {
        continue;
      }

      int synset_id = std::stoi(relation_data[1]);
      int wnum = std::stoi(relation_data[2]);
      std::string text = relation_data[3];
      int tag_count = std::stoi(relation_data[4]);
      size_t word_it;
      while ((word_it = text.find("''")) != std::string::npos) {
        text.erase(word_it, 1);
      }

      // The word must be common enough.
      if (word_frequencies[text] < MIN_FREQUENCY) {
        continue;
      }

      // We are looking for single words.
      if (std::count(std::begin(text), std::end(text), ' ') > 0) {
        continue;
      }

      // This should filter our proper nouns.
      if (std::any_of(std::begin(text), std::end(text), ::isupper)) {
        continue;
      }

      // Ignore any profane words.
      if (profane.count(text)) {
        continue;
      }

      // The WordNet data does contain duplicates, so we need to check that we
      // haven't already created this word.
      std::pair<int, int> lookup(synset_id, wnum);
      if (word_by_wnid_and_wnum_.count(lookup)) {
        continue;
      }

      size_t word_id = LookupOrCreateWord(text);
      word_by_wnid_and_wnum_[lookup] = word_id;
      AddWordToSynset(word_id, synset_id);
    }
  }

  {
    std::list<std::string> lines(readFile(agidPath_));
    hatkirby::progress ppgs("Reading inflections from AGID...", lines.size());

    for (std::string line : lines) {
      ppgs.update();

      int divider = line.find_first_of(" ");
      std::string infinitive = line.substr(0, divider);
      line = line.substr(divider + 1);
      char type = line[0];

      if (line[1] == '?') {
        line.erase(0, 4);
      } else {
        line.erase(0, 3);
      }

      if (!words_by_base_.count(infinitive)) {
        continue;
      }

      auto inflWordList = hatkirby::split<std::list<std::string>>(line, " | ");

      std::vector<std::list<std::string>> agidForms;
      for (std::string inflForms : inflWordList) {
        auto inflFormList =
            hatkirby::split<std::list<std::string>>(std::move(inflForms), ", ");

        std::list<std::string> forms;
        for (std::string inflForm : inflFormList) {
          int sympos = inflForm.find_first_of("~<!? ");
          if (sympos != std::string::npos) {
            inflForm = inflForm.substr(0, sympos);
          }

          forms.push_back(std::move(inflForm));
        }

        agidForms.push_back(std::move(forms));
      }

      std::vector<std::list<std::string>> inflections;
      switch (type) {
        case 'V': {
          if (agidForms.size() == 4) {
            inflections.push_back(agidForms[0]);
            inflections.push_back(agidForms[1]);
            inflections.push_back(agidForms[2]);
            inflections.push_back(agidForms[3]);
          } else if (agidForms.size() == 3) {
            inflections.push_back(agidForms[0]);
            inflections.push_back(agidForms[1]);
            inflections.push_back(agidForms[2]);
          } else if (agidForms.size() == 8) {
            // As of AGID 2014.08.11, this is only "to be"
            inflections.push_back(agidForms[0]);
            inflections.push_back(agidForms[2]);
            inflections.push_back(agidForms[3]);
            inflections.push_back(agidForms[4]);
          } else {
            // Words that don't fit the cases above as of AGID 2014.08.11:
            // - may and shall do not conjugate the way we want them to
            // - methinks only has a past tense and is an outlier
            // - wit has five forms, and is archaic/obscure enough that we can
            // ignore it for now
            std::cout << " Ignoring verb \"" << infinitive
                      << "\" due to non-standard number of forms." << std::endl;
          }

          break;
        }

        case 'A': {
          if (agidForms.size() == 2) {
            inflections.push_back(agidForms[0]);
            inflections.push_back(agidForms[1]);
          } else {
            // As of AGID 2014.08.11, this is only "only", which has only the
            // form "onliest"
            std::cout << " Ignoring adjective/adverb \"" << infinitive
                      << "\" due to non-standard number of forms." << std::endl;
          }

          break;
        }

        case 'N': {
          if (agidForms.size() == 1) {
            inflections.push_back(agidForms[0]);
          } else {
            // As of AGID 2014.08.11, this is non-existent.
            std::cout << " Ignoring noun \"" << infinitive
                      << "\" due to non-standard number of forms." << std::endl;
          }

          break;
        }
      }

      // Compile the forms we have mapped.
      for (size_t word_id : words_by_base_.at(infinitive)) {
        for (const std::list<std::string>& infl_list : inflections) {
          for (const std::string& infl : infl_list) {
            if (!profane.count(infl)) {
              size_t form_id = LookupOrCreateForm(infl);
              AddFormToWord(form_id, word_id);
            }
          }
        }
      }
    }
  }

  word_frequencies.clear();  // Not needed anymore.

  {
    std::list<std::string> lines(readFile(cmudictPath_));

    hatkirby::progress ppgs("Reading pronunciations from CMUDICT...",
                            lines.size());

    for (std::string line : lines) {
      ppgs.update();

      std::regex phoneme("([A-Z][^ \\(]*)(?:\\(\\d+\\))?  ([A-Z 0-9]+)");
      std::smatch phoneme_data;
      if (std::regex_search(line, phoneme_data, phoneme)) {
        std::string canonical = hatkirby::lowercase(phoneme_data[1]);

        if (!form_by_text_.count(canonical)) {
          continue;
        }

        std::string phonemes = phoneme_data[2];
        size_t pronunciation_id = LookupOrCreatePronunciation(phonemes);
        AddPronunciationToForm(pronunciation_id, form_by_text_[canonical]);
      }
    }
  }

  std::cout << "Words: " << words_.size() << std::endl;
  std::cout << "Forms: " << forms_.size() << std::endl;
  std::cout << "Pronunciations: " << pronunciations_.size() << std::endl;

  // White Top
  {
    hatkirby::progress ppgs("Generating white top puzzles...", forms_.size());

    for (Form& form : forms_) {
      ppgs.update();

      for (size_t p_id : form.pronunciation_ids) {
        const Pronunciation& pronunciation = pronunciations_.at(p_id);
        for (size_t other_form_id : pronunciation.form_ids) {
          if (other_form_id != form.id) {
            form.puzzles[kWhiteTop].insert(other_form_id);
          }
        }
      }
    }
  }

  // White Bottom
  {
    hatkirby::progress ppgs("Generating white bottom puzzles...",
                            words_.size());

    for (const Word& word : words_) {
      ppgs.update();

      Form& form = forms_.at(word.base_form_id);
      for (size_t synset_id : word.synsets) {
        for (size_t other_word_id : synsets_.at(synset_id)) {
          if (other_word_id != word.id) {
            const Word& other_word = words_.at(other_word_id);
            form.puzzles[kWhiteBottom].insert(other_word.base_form_id);
          }
        }
      }
    }
  }

  // Yellow Top
  {
    hatkirby::progress ppgs("Generating yellow top puzzles...",
                            anaphone_sets_.size());

    for (const std::vector<size_t>& anaphone_set : anaphone_sets_) {
      ppgs.update();

      std::set<size_t> all_forms;
      for (size_t p_id : anaphone_set) {
        const Pronunciation& pronunciation = pronunciations_.at(p_id);
        for (size_t form_id : pronunciation.form_ids) {
          all_forms.insert(form_id);
        }
      }

      for (size_t f_id1 : all_forms) {
        for (size_t f_id2 : all_forms) {
          if (f_id1 != f_id2) {
            Form& form = forms_.at(f_id1);
            Form& form2 = forms_.at(f_id2);
            // Top yellow should not be mid yellow.
            if (form.anagram_set_id == form2.anagram_set_id) {
              continue;
            }
            // Top yellow should not be top white.
            if (std::any_of(
                    form.pronunciation_ids.begin(),
                    form.pronunciation_ids.end(), [&form2](size_t p_id) {
                      return std::find(form2.pronunciation_ids.begin(),
                                       form2.pronunciation_ids.end(),
                                       p_id) == form2.pronunciation_ids.end();
                    })) {
              continue;
            }
            form.puzzles[kYellowTop].insert(f_id2);
          }
        }
      }
    }
  }

  // Yellow Middle
  {
    hatkirby::progress ppgs("Generating yellow middle puzzles...",
                            anagram_sets_.size());

    for (const std::vector<size_t>& anagram_set : anagram_sets_) {
      ppgs.update();

      for (size_t f_id1 : anagram_set) {
        for (size_t f_id2 : anagram_set) {
          if (f_id1 != f_id2) {
            Form& form = forms_.at(f_id1);
            form.puzzles[kYellowMiddle].insert(f_id2);
          }
        }
      }
    }
  }

  // Black Top
  {
    hatkirby::progress ppgs("Generating black top puzzles...",
                            pronunciations_.size());

    for (const Pronunciation& pronunciation : pronunciations_) {
      ppgs.update();

      auto reversed_list = hatkirby::split<std::vector<std::string>>(
          pronunciation.stressless_phonemes, " ");
      std::reverse(reversed_list.begin(), reversed_list.end());
      std::string reversed_phonemes =
          hatkirby::implode(reversed_list.begin(), reversed_list.end(), " ");
      if (pronunciations_by_blank_phonemes_.count(reversed_phonemes)) {
        std::set<size_t> all_forms;

        for (size_t p_id :
             pronunciations_by_blank_phonemes_.at(reversed_phonemes)) {
          const Pronunciation& other_pronunciation = pronunciations_.at(p_id);
          for (size_t form_id : other_pronunciation.form_ids) {
            all_forms.insert(form_id);
          }
        }

        for (size_t f_id1 : pronunciation.form_ids) {
          for (size_t f_id2 : all_forms) {
            Form& form = forms_.at(f_id1);
            if (form.reverse_form_id == f_id2) {
              continue;
            }
            form.puzzles[kBlackTop].insert(f_id2);
          }
        }
      }
    }
  }

  // Black Middle
  {
    hatkirby::progress ppgs("Generating black middle puzzles...",
                            forms_.size());

    for (Form& form : forms_) {
      ppgs.update();

      std::string reversed_text = form.text;
      std::reverse(reversed_text.begin(), reversed_text.end());

      if (form_by_text_.count(reversed_text)) {
        form.puzzles[kBlackMiddle].insert(form_by_text_.at(reversed_text));
      }
    }
  }

  // Black Bottom
  std::unordered_map<size_t, std::set<size_t>> antonyms;
  {
    std::list<std::string> lines(readFile(wordNetPath_ + "wn_ant.pl", true));

    hatkirby::progress ppgs("Generating black bottom puzzles...", lines.size());
    for (const std::string& line : lines) {
      ppgs.update();

      std::regex relation(
          "^ant\\(([134]\\d{8}),(\\d+),([134]\\d{8}),(\\d+)\\)\\.");

      std::smatch relation_data;
      if (!std::regex_search(line, relation_data, relation)) {
        continue;
      }

      std::pair<int, int> lookup1(std::stoi(relation_data[1]),
                                  std::stoi(relation_data[2]));

      std::pair<int, int> lookup2(std::stoi(relation_data[3]),
                                  std::stoi(relation_data[4]));

      if (word_by_wnid_and_wnum_.count(lookup1) &&
          word_by_wnid_and_wnum_.count(lookup2)) {
        const Word& word1 = words_.at(word_by_wnid_and_wnum_.at(lookup1));
        const Word& word2 = words_.at(word_by_wnid_and_wnum_.at(lookup2));

        Form& form1 = forms_.at(word1.base_form_id);
        form1.puzzles[kBlackBottom].insert(word2.base_form_id);

        antonyms[word1.id].insert(word2.id);
      }
    }
  }

  // Black Double Bottom
  {
    hatkirby::progress ppgs("Generating black double bottom puzzles...",
                            antonyms.size());
    for (const auto& [word1, ant_words] : antonyms) {
      ppgs.update();

      for (size_t word2 : ant_words) {
        const Word& word2_obj = words_.at(word2);
        const Form& form2 = forms_.at(word2_obj.base_form_id);

        for (size_t word25 : form2.word_ids) {
          if (word25 == word2) {
            continue;
          }

          const auto& double_ant_words = antonyms[word25];

          for (size_t word3 : double_ant_words) {
            const Word& word1_obj = words_.at(word1);
            const Word& word3_obj = words_.at(word3);

            bool synset_overlap = false;
            for (size_t synset1 : word1_obj.synsets) {
              for (size_t synset3 : word3_obj.synsets) {
                if (synset1 == synset3) {
                  synset_overlap = true;
                  break;
                }
              }
              if (synset_overlap) {
                break;
              }
            }
            if (!synset_overlap) {
              Form& form1 = forms_.at(word1_obj.base_form_id);
              form1.puzzles[kDoubleBlackBottom].insert(word3_obj.base_form_id);
            }
          }
        }
      }
    }
  }

  // Red/Blue Top
  {
    std::map<std::list<std::string>, std::vector<size_t>> tokenized;
    for (const auto& [phonemes, pronunciations] :
         pronunciations_by_blank_phonemes_) {
      tokenized[hatkirby::split<std::list<std::string>>(phonemes, " ")] =
          pronunciations;
    }

    hatkirby::progress ppgs("Generating top red/blue puzzles...",
                            tokenized.size());
    for (const auto& [phonemes, pronunciations] : tokenized) {
      ppgs.update();

      std::set<std::list<std::string>> visited;
      for (int i = 0; i < phonemes.size(); i++) {
        for (int l = 2; l <= phonemes.size() - i; l++) {
          if (i == 0 && l == phonemes.size()) {
            continue;
          }

          std::list<std::string> sublist;
          for (auto j = std::next(phonemes.begin(), i);
               j != std::next(phonemes.begin(), i + l); j++) {
            sublist.push_back(*j);
          }

          if (tokenized.count(sublist) && !visited.count(sublist)) {
            visited.insert(sublist);

            for (size_t holophone_id : pronunciations) {
              for (size_t merophone_id : tokenized[sublist]) {
                const Pronunciation& holophone =
                    pronunciations_.at(holophone_id);
                const Pronunciation& merophone =
                    pronunciations_.at(merophone_id);

                for (size_t holo_form_id : holophone.form_ids) {
                  Form& holo_form = forms_.at(holo_form_id);
                  for (size_t mero_form_id : merophone.form_ids) {
                    Form& mero_form = forms_.at(mero_form_id);

                    if (holo_form.text.find(mero_form.text) !=
                        std::string::npos) {
                      // We don't want top puzzles that are also middle puzzles.
                      continue;
                    }

                    bool word_overlap = false;
                    for (size_t holo_word_id : holo_form.word_ids) {
                      for (size_t mero_word_id : mero_form.word_ids) {
                        if (holo_word_id == mero_word_id) {
                          word_overlap = true;
                          break;
                        }
                      }
                      if (word_overlap) {
                        break;
                      }
                    }

                    if (!word_overlap) {
                      if (holo_form.text.size() <= mero_form.text.size() + 5) {
                        holo_form.puzzles[kBlueTop].insert(mero_form_id);
                      }
                      mero_form.puzzles[kRedTop].insert(holo_form_id);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  // Red/Blue Middle
  std::unordered_map<size_t, std::set<size_t>> left_shorter_by_longer;
  std::unordered_map<size_t, std::set<size_t>> left_longer_by_shorter;
  std::unordered_map<size_t, std::set<size_t>> right_shorter_by_longer;
  std::unordered_map<size_t, std::set<size_t>> right_longer_by_shorter;
  {
    hatkirby::progress ppgs("Generating red/blue middle puzzles...",
                            form_by_text_.size());
    for (const auto& [text, form_id] : form_by_text_) {
      ppgs.update();

      Form& holograph = forms_.at(form_id);
      std::unordered_set<std::string> visited;
      for (int i = 0; i < text.size(); i++) {
        for (int l = 3; l <= text.size() - i; l++) {
          if (i == 0 && l == text.size()) {
            continue;
          }

          std::string substr = text.substr(i, l);
          if (form_by_text_.count(substr) && !visited.count(substr)) {
            visited.insert(substr);

            Form& merograph = forms_.at(form_by_text_.at(substr));

            bool word_overlap = false;
            for (size_t holo_word_id : holograph.word_ids) {
              for (size_t mero_word_id : merograph.word_ids) {
                if (holo_word_id == mero_word_id) {
                  word_overlap = true;
                  break;
                }
              }
              if (word_overlap) {
                break;
              }
            }

            if (!word_overlap) {
              if (holograph.text.size() <= merograph.text.size() + 4) {
                holograph.puzzles[kBlueMiddle].insert(merograph.id);

                if (i == 0) {
                  left_shorter_by_longer[form_id].insert(merograph.id);
                  left_longer_by_shorter[merograph.id].insert(form_id);
                } else if (i + l == text.size()) {
                  right_shorter_by_longer[form_id].insert(merograph.id);
                  right_longer_by_shorter[merograph.id].insert(form_id);
                }
              }
              merograph.puzzles[kRedMiddle].insert(form_id);
            }
          }
        }
      }
    }
  }

  // Purple Middle
  {
    hatkirby::progress ppgs(
        "Generating purple middle puzzles...",
        left_shorter_by_longer.size() + right_shorter_by_longer.size());

    for (const auto& [holograph_id, merograph_ids] : left_shorter_by_longer) {
      ppgs.update();

      Form& holograph = forms_.at(holograph_id);
      for (size_t merograph_id : merograph_ids) {
        const Form& merograph = forms_.at(merograph_id);
        for (size_t other_id : left_longer_by_shorter[merograph_id]) {
          if (other_id != holograph_id) {
            const Form& other_form = forms_.at(other_id);

            if (holograph.text[merograph.text.size()] !=
                other_form.text[merograph.text.size()]) {
              holograph.puzzles[kPurpleMiddle].insert(other_id);
            }
          }
        }
      }
    }

    for (const auto& [holograph_id, merograph_ids] : right_shorter_by_longer) {
      ppgs.update();

      Form& holograph = forms_.at(holograph_id);
      for (size_t merograph_id : merograph_ids) {
        const Form& merograph = forms_.at(merograph_id);
        for (size_t other_id : right_longer_by_shorter[merograph_id]) {
          if (other_id != holograph_id) {
            const Form& other_form = forms_.at(other_id);

            if (holograph
                    .text[holograph.text.size() - merograph.text.size() - 1] !=
                other_form
                    .text[other_form.text.size() - merograph.text.size() - 1]) {
              holograph.puzzles[kPurpleMiddle].insert(other_id);
            }
          }
        }
      }
    }
  }

  // Red/Blue Bottom
  std::unordered_map<size_t, std::set<size_t>> meronyms_by_holonym;
  {
    std::list<std::string> lines(readFile(wordNetPath_ + "wn_mm.pl"));
    hatkirby::progress ppgs("Reading member meronymy...", lines.size());
    for (auto line : lines) {
      ppgs.update();

      std::regex relation("^mm\\((1\\d{8}),(1\\d{8})\\)\\.");
      std::smatch relation_data;
      if (!std::regex_search(line, relation_data, relation)) {
        continue;
      }

      int lookup1 = std::stoi(relation_data[1]);
      int lookup2 = std::stoi(relation_data[2]);

      if (synset_by_wnid_.count(lookup1) && synset_by_wnid_.count(lookup2)) {
        for (size_t word_id1 : synsets_.at(synset_by_wnid_.at(lookup1))) {
          for (size_t word_id2 : synsets_.at(synset_by_wnid_.at(lookup2))) {
            meronyms_by_holonym[word_id1].insert(word_id2);
          }
        }
      }
    }
  }

  {
    std::list<std::string> lines(readFile(wordNetPath_ + "wn_mp.pl"));
    hatkirby::progress ppgs("Reading part meronymy...", lines.size());
    for (auto line : lines) {
      ppgs.update();

      std::regex relation("^mp\\((1\\d{8}),(1\\d{8})\\)\\.");
      std::smatch relation_data;
      if (!std::regex_search(line, relation_data, relation)) {
        continue;
      }

      int lookup1 = std::stoi(relation_data[1]);
      int lookup2 = std::stoi(relation_data[2]);

      if (synset_by_wnid_.count(lookup1) && synset_by_wnid_.count(lookup2)) {
        for (size_t word_id1 : synsets_.at(synset_by_wnid_.at(lookup1))) {
          for (size_t word_id2 : synsets_.at(synset_by_wnid_.at(lookup2))) {
            meronyms_by_holonym[word_id1].insert(word_id2);
          }
        }
      }
    }
  }

  {
    std::list<std::string> lines(readFile(wordNetPath_ + "wn_ms.pl"));
    hatkirby::progress ppgs("Reading substance meronymy...", lines.size());
    for (auto line : lines) {
      ppgs.update();

      std::regex relation("^ms\\((1\\d{8}),(1\\d{8})\\)\\.");
      std::smatch relation_data;
      if (!std::regex_search(line, relation_data, relation)) {
        continue;
      }

      int lookup1 = std::stoi(relation_data[1]);
      int lookup2 = std::stoi(relation_data[2]);

      if (synset_by_wnid_.count(lookup1) && synset_by_wnid_.count(lookup2)) {
        for (size_t word_id1 : synsets_.at(synset_by_wnid_.at(lookup1))) {
          for (size_t word_id2 : synsets_.at(synset_by_wnid_.at(lookup2))) {
            meronyms_by_holonym[word_id1].insert(word_id2);
          }
        }
      }
    }
  }

  {
    hatkirby::progress ppgs("Generating red/blue bottom puzzles...",
                            meronyms_by_holonym.size());

    for (const auto& [holonym_id, meronym_ids] : meronyms_by_holonym) {
      ppgs.update();

      for (size_t meronym_id : meronym_ids) {
        const Word& holonym_word = words_.at(holonym_id);
        const Word& meronym_word = words_.at(meronym_id);

        Form& holonym_form = forms_.at(holonym_word.base_form_id);
        Form& meronym_form = forms_.at(meronym_word.base_form_id);

        // There must be some mis-naming somewhere, because things are reversed
        // if we do red with holonym and blue with meronym.
        holonym_form.puzzles[kRedBottom].insert(meronym_form.id);
        meronym_form.puzzles[kBlueBottom].insert(holonym_form.id);
      }
    }
  }

  // Purple Top
  {
    hatkirby::progress ppgs("Generating purple top puzzles...",
                            pronunciations_by_rhyme_.size());

    for (const auto& [rhyme, pronunciation_ids] : pronunciations_by_rhyme_) {
      ppgs.update();

      for (size_t p_id1 : pronunciation_ids) {
        const Pronunciation& p1 = pronunciations_.at(p_id1);
        if (p1.prerhyme.empty()) {
          continue;
        }

        for (size_t p_id2 : pronunciation_ids) {
          const Pronunciation& p2 = pronunciations_.at(p_id2);
          if (p2.prerhyme.empty()) {
            continue;
          }

          if (p1.prerhyme != p2.prerhyme) {
            for (size_t f_id1 : p1.form_ids) {
              for (size_t f_id2 : p2.form_ids) {
                if (f_id1 != f_id2) {
                  Form& form1 = forms_.at(f_id1);
                  const Form& form2 = forms_.at(f_id2);

                  // Top purple should not be mid red/blue.
                  if (form1.text.find(form2.text) != std::string::npos ||
                      form2.text.find(form1.text) != std::string::npos) {
                    continue;
                  }

                  if (std::abs(static_cast<int>(form1.text.size()) -
                               static_cast<int>(form2.text.size())) <= 4) {
                    form1.puzzles[kPurpleTop].insert(f_id2);
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  // Orange addition
  std::vector<std::string> orange_addition;
  {
    hatkirby::progress ppgs("Generating orange addition puzzles...",
                            wanderlust_.size());

    for (const auto& [cipher, form_id] : wanderlust_) {
      if (cipher < 100) {
        continue;
      }
      for (const auto& [cipher2, form_id2] : wanderlust_) {
        if (cipher2 >= cipher) {
          break;
        }
        if (cipher2 < 100) {
          continue;
        }
        if (wanderlust_.count(cipher - cipher2)) {
          orange_addition.push_back(fmt::format(
              "[{},{},{}]", form_id2, wanderlust_[cipher - cipher2], form_id));
        }
      }
    }
  }

  FindComboPuzzles("Generating purple middle red middle combo puzzles...",
                   kPurpleMiddle, kRedMiddle);
  FindComboPuzzles("Generating purple top purple top combo puzzles...",
                   kPurpleTop, kPurpleTop);
  FindComboPuzzles("Generating black middle black bottom combo puzzles...",
                   kBlackMiddle, kBlackBottom);
  FindComboPuzzles("Generating white bottom purple middle combo puzzles...",
                   kWhiteBottom, kPurpleMiddle);
  FindComboPuzzles("Generating black bottom white bottom combo puzzles...",
                   kBlackBottom, kWhiteBottom);
  FindComboPuzzles("Generating blue middle red middle combo puzzles...",
                   kBlueMiddle, kRedMiddle);
  FindComboPuzzles("Generating white bottom white bottom combo puzzles...",
                   kWhiteBottom, kWhiteBottom);
  FindComboPuzzles("Generating blue middle yellow middle combo puzzles...",
                   kBlueMiddle, kYellowMiddle);
  FindComboPuzzles("Generating black bottom blue middle combo puzzles...",
                   kBlackBottom, kBlueMiddle);
  FindComboPuzzles("Generating yellow top yellow middle combo puzzles...",
                   kYellowTop, kYellowMiddle);

  // Count up all of the generated puzzles.
  int total_puzzles = 0;
  int reusable_words = 0;
  std::unordered_map<PuzzleType, int> per_puzzle_type;
  for (const Form& form : forms_) {
    for (const auto& [puzzle_type, puzzles] : form.puzzles) {
      total_puzzles += puzzles.size();
      per_puzzle_type[puzzle_type]++;
    }
    if (form.puzzles.size() > 1) {
      reusable_words++;
    }
  }
  std::cout << "Puzzles: " << total_puzzles << std::endl;
  std::cout << "Reusable words: " << reusable_words << std::endl;
  std::cout << "White tops: " << per_puzzle_type[kWhiteTop] << std::endl;
  std::cout << "White bottom: " << per_puzzle_type[kWhiteBottom] << std::endl;
  std::cout << "Yellow tops: " << per_puzzle_type[kYellowTop] << std::endl;
  std::cout << "Yellow middles: " << per_puzzle_type[kYellowMiddle]
            << std::endl;
  std::cout << "Black tops: " << per_puzzle_type[kBlackTop] << std::endl;
  std::cout << "Black middles: " << per_puzzle_type[kBlackMiddle] << std::endl;
  std::cout << "Black bottoms: " << per_puzzle_type[kBlackBottom] << std::endl;
  std::cout << "Black double bottoms: " << per_puzzle_type[kDoubleBlackBottom]
            << std::endl;
  std::cout << "Red tops: " << per_puzzle_type[kRedTop] << std::endl;
  std::cout << "Red middles: " << per_puzzle_type[kRedMiddle] << std::endl;
  std::cout << "Red bottoms: " << per_puzzle_type[kRedBottom] << std::endl;
  std::cout << "Blue tops: " << per_puzzle_type[kBlueTop] << std::endl;
  std::cout << "Blue middles: " << per_puzzle_type[kBlueMiddle] << std::endl;
  std::cout << "Blue bottoms: " << per_puzzle_type[kBlueBottom] << std::endl;
  std::cout << "Purple tops: " << per_puzzle_type[kPurpleTop] << std::endl;
  std::cout << "Purple middles: " << per_puzzle_type[kPurpleMiddle]
            << std::endl;
  std::cout << "Purple middle red middle combos: "
            << combos_[kPurpleMiddle][kRedMiddle].size() << std::endl;
  std::cout << "Purple top purple top combos: "
            << combos_[kPurpleTop][kPurpleTop].size() << std::endl;
  std::cout << "Black middle black bottom combos: "
            << combos_[kBlackMiddle][kBlackBottom].size() << std::endl;
  std::cout << "White bottom purple middle combos: "
            << combos_[kWhiteBottom][kPurpleMiddle].size() << std::endl;
  std::cout << "Black bottom white bottom combos: "
            << combos_[kBlackBottom][kWhiteBottom].size() << std::endl;
  std::cout << "Blue middle red middle combos: "
            << combos_[kBlueMiddle][kRedMiddle].size() << std::endl;
  std::cout << "White bottom white bottom combos: "
            << combos_[kWhiteBottom][kWhiteBottom].size() << std::endl;
  std::cout << "Blue middle yellow middle combos: "
            << combos_[kBlueMiddle][kYellowMiddle].size() << std::endl;
  std::cout << "Black bottom blue middle combos: "
            << combos_[kBlackBottom][kBlueMiddle].size() << std::endl;
  std::cout << "Yellow top yellow middle combos: "
            << combos_[kYellowTop][kYellowMiddle].size() << std::endl;

  std::vector<std::string> form_entry;
  form_entry.reserve(forms_.size());
  for (const Form& form : forms_) {
    if (form.puzzles.empty()) {
      form_entry.push_back(fmt::format("\"{}\"", form.text));
    } else {
      std::vector<std::string> entry_per_type;
      for (const auto& [puzzle_type, puzzles] : form.puzzles) {
        std::vector<std::string> entry_per_puzzle;
        for (size_t puzzle : puzzles) {
          entry_per_puzzle.push_back(std::to_string(puzzle));
        }
        entry_per_type.push_back(
            fmt::format("{}:[{}]", static_cast<int>(puzzle_type),
                        hatkirby::implode(entry_per_puzzle, ",")));
      }
      form_entry.push_back(fmt::format("[\"{}\",{{{}}}]", form.text,
                                       hatkirby::implode(entry_per_type, ",")));
    }
  }

  std::ofstream output_file(outputPath_);
  output_file << "extends Node\n\nvar forms = ["
              << hatkirby::implode(form_entry, ",") << "]" << std::endl;

  std::vector<std::string> painting_entries;
  {
    std::list<std::string> paintings(readFile(datadirPath_ / "paintings.txt"));
    for (const std::string& line : paintings) {
      auto parts = hatkirby::split<std::vector<std::string>>(line, ",");
      painting_entries.push_back(
          fmt::format("[\"{}\",\"{}\"]", parts[0], parts[1]));
    }
  }
  output_file << "var paintings = [" << hatkirby::implode(painting_entries, ",")
              << "]" << std::endl;

  std::vector<std::string> cipher_lines;
  for (const auto& [cipher, form_id] : wanderlust_) {
    cipher_lines.push_back(std::to_string(form_id));
  }
  output_file << "var wanderlust = [" << hatkirby::implode(cipher_lines, ",")
              << "]" << std::endl;
  output_file << "var addition = [" << hatkirby::implode(orange_addition, ",")
              << "]" << std::endl;

  std::vector<std::string> walls_entries;
  {
    std::list<std::string> walls(readFile(datadirPath_ / "walls.txt"));
    for (const std::string& line : walls) {
      auto parts = hatkirby::split<std::vector<std::string>>(line, ",");
      walls_entries.push_back(
          fmt::format("[\"{}\",\"{}\"]", parts[0], parts[1]));
    }
  }
  output_file << "var walls_puzzles = ["
              << hatkirby::implode(walls_entries, ",") << "]" << std::endl;

  std::vector<std::string> combo_entries;
  for (const auto& [left_type, left_join] : combos_) {
    std::vector<std::string> left_entries;
    for (const auto& [right_type, choices] : left_join) {
      std::vector<std::string> choice_entries;
      for (const auto& [hint1, hint2, answer] : choices) {
        choice_entries.push_back(
            fmt::format("[{},{},{}]", hint1, hint2, answer));
      }
      left_entries.push_back(
          fmt::format("{}:[{}]", static_cast<int>(right_type),
                      hatkirby::implode(choice_entries, ",")));
    }
    combo_entries.push_back(fmt::format("{}:{{{}}}",
                                        static_cast<int>(left_type),
                                        hatkirby::implode(left_entries, ",")));
  }
  output_file << "var combos = {" << hatkirby::implode(combo_entries, ",")
              << "}" << std::endl;
}

size_t generator::LookupOrCreatePronunciation(const std::string& phonemes) {
  if (pronunciation_by_phonemes_.count(phonemes)) {
    return pronunciation_by_phonemes_[phonemes];
  } else {
    size_t pronunciation_id = pronunciations_.size();

    auto phonemeList = hatkirby::split<std::list<std::string>>(phonemes, " ");

    std::list<std::string>::iterator rhymeStart =
        std::find_if(std::begin(phonemeList), std::end(phonemeList),
                     [](std::string phoneme) {
                       return phoneme.find("1") != std::string::npos;
                     });

    // Rhyme detection
    std::string prerhyme = "";
    std::string rhyme = "";
    if (rhymeStart != std::end(phonemeList)) {
      std::list<std::string> rhymePhonemes;

      std::transform(
          rhymeStart, std::end(phonemeList), std::back_inserter(rhymePhonemes),
          [](std::string phoneme) {
            std::string naked;

            std::remove_copy_if(std::begin(phoneme), std::end(phoneme),
                                std::back_inserter(naked),
                                [](char ch) { return std::isdigit(ch); });

            return naked;
          });

      rhyme = hatkirby::implode(std::begin(rhymePhonemes),
                                std::end(rhymePhonemes), " ");

      if (rhymeStart != std::begin(phonemeList)) {
        prerhyme = *std::prev(rhymeStart);
      }

      pronunciations_by_rhyme_[rhyme].push_back(pronunciation_id);
    }

    std::string stressless;
    for (int i = 0; i < phonemes.size(); i++) {
      if (!std::isdigit(phonemes[i])) {
        stressless.push_back(phonemes[i]);
      }
    }
    auto stresslessList =
        hatkirby::split<std::vector<std::string>>(stressless, " ");
    std::string stresslessPhonemes =
        hatkirby::implode(stresslessList.begin(), stresslessList.end(), " ");
    std::sort(stresslessList.begin(), stresslessList.end());
    std::string sortedPhonemes =
        hatkirby::implode(stresslessList.begin(), stresslessList.end(), " ");

    pronunciations_.push_back({.id = pronunciation_id,
                               .phonemes = phonemes,
                               .prerhyme = prerhyme,
                               .rhyme = rhyme,
                               .stressless_phonemes = stresslessPhonemes});

    AddPronunciationToAnaphoneSet(pronunciation_id, sortedPhonemes);

    pronunciation_by_phonemes_[phonemes] = pronunciation_id;
    pronunciations_by_blank_phonemes_[stresslessPhonemes].push_back(
        pronunciation_id);

    return pronunciation_id;
  }
}

size_t generator::LookupOrCreateForm(const std::string& word) {
  if (form_by_text_.count(word)) {
    return form_by_text_[word];
  } else {
    size_t form_id = forms_.size();
    form_by_text_[word] = form_id;
    std::optional<int> ciphered;
    if (std::all_of(word.begin(), word.end(), [](char ch) {
          return ch == 'w' || ch == 'a' || ch == 'n' || ch == 'd' ||
                 ch == 'e' || ch == 'r' || ch == 'l' || ch == 'u' ||
                 ch == 's' || ch == 't';
        })) {
      ciphered = 0;
      std::string to_cipher = word;
      std::reverse(to_cipher.begin(), to_cipher.end());
      while (!to_cipher.empty()) {
        *ciphered *= 10;
        switch (to_cipher.back()) {
          case 'w': {
            *ciphered += 1;
            break;
          }
          case 'a': {
            *ciphered += 2;
            break;
          }
          case 'n': {
            *ciphered += 3;
            break;
          }
          case 'd': {
            *ciphered += 4;
            break;
          }
          case 'e': {
            *ciphered += 5;
            break;
          }
          case 'r': {
            *ciphered += 6;
            break;
          }
          case 'l': {
            *ciphered += 7;
            break;
          }
          case 'u': {
            *ciphered += 8;
            break;
          }
          case 's': {
            *ciphered += 9;
            break;
          }
        }
        to_cipher.pop_back();
      }
      wanderlust_[*ciphered] = form_id;
    }
    forms_.push_back({.id = form_id, .text = word, .ciphered = ciphered});

    std::string sortedText = word;
    std::sort(sortedText.begin(), sortedText.end());
    AddFormToAnagramSet(form_id, sortedText);

    return form_id;
  }
}

size_t generator::LookupOrCreateWord(const std::string& word) {
  size_t word_id = words_.size();
  words_by_base_[word].push_back(word_id);
  size_t form_id = LookupOrCreateForm(word);
  words_.push_back({.id = word_id, .base_form_id = form_id});
  AddFormToWord(form_id, word_id);
  forms_[form_id].is_base_form = true;
  return word_id;
}

void generator::AddPronunciationToForm(size_t pronunciation_id,
                                       size_t form_id) {
  pronunciations_[pronunciation_id].form_ids.push_back(form_id);
  forms_[form_id].pronunciation_ids.push_back(pronunciation_id);
}

void generator::AddFormToWord(size_t form_id, size_t word_id) {
  words_[word_id].form_ids.push_back(form_id);
  forms_[form_id].word_ids.push_back(word_id);
}

void generator::AddWordToSynset(size_t word_id, int wnid) {
  if (!synset_by_wnid_.count(wnid)) {
    synset_by_wnid_[wnid] = synsets_.size();
    synsets_.push_back({word_id});
    words_[word_id].synsets.push_back(synsets_.size() - 1);
  } else {
    size_t synset_id = synset_by_wnid_[wnid];
    synsets_[synset_id].push_back(word_id);
    words_[word_id].synsets.push_back(synset_id);
  }
}

void generator::AddFormToAnagramSet(size_t form_id,
                                    const std::string& sorted_letters) {
  if (!anagram_set_by_sorted_letters_.count(sorted_letters)) {
    anagram_set_by_sorted_letters_[sorted_letters] = anagram_sets_.size();
    anagram_sets_.push_back({form_id});
    forms_[form_id].anagram_set_id = anagram_sets_.size() - 1;
  } else {
    size_t anagram_set_id = anagram_set_by_sorted_letters_[sorted_letters];
    anagram_sets_[anagram_set_id].push_back(form_id);
    forms_[form_id].anagram_set_id = anagram_set_id;
  }
}

void generator::AddPronunciationToAnaphoneSet(
    size_t pronunciation_id, const std::string& sorted_phonemes) {
  if (!anaphone_set_by_sorted_phonemes_.count(sorted_phonemes)) {
    anaphone_set_by_sorted_phonemes_[sorted_phonemes] = anaphone_sets_.size();
    anaphone_sets_.push_back({pronunciation_id});
    pronunciations_[pronunciation_id].anaphone_set_id =
        anaphone_sets_.size() - 1;
  } else {
    size_t anaphone_set_id = anaphone_set_by_sorted_phonemes_[sorted_phonemes];
    anaphone_sets_[anaphone_set_id].push_back(pronunciation_id);
    pronunciations_[pronunciation_id].anaphone_set_id = anaphone_set_id;
  }
}

void generator::FindComboPuzzles(std::string text, PuzzleType left_type,
                                 PuzzleType right_type) {
  hatkirby::progress ppgs(text, forms_.size());

  for (Form& left_form : forms_) {
    ppgs.update();

    if (left_form.text.size() < 3 || !left_form.puzzles.count(left_type))
      continue;
    if (left_type == kWhiteBottom && left_form.puzzles[left_type].size() > 3)
      continue;

    for (Form& right_form : forms_) {
      if (right_form.text.size() >= 3 && right_form.puzzles.count(right_type) &&
          form_by_text_.count(left_form.text + right_form.text) &&
          !(right_type == kWhiteBottom &&
            right_form.puzzles[right_type].size() > 3)) {
        for (size_t left_hint_id : left_form.puzzles[left_type]) {
          Form& left_hint = forms_[left_hint_id];
          for (size_t right_hint_id : right_form.puzzles[right_type]) {
            Form& right_hint = forms_[right_hint_id];

            if (left_hint.text.size() + right_hint.text.size() > 15) continue;

            if (left_type == kPurpleMiddle &&
                left_hint.text.size() != left_form.text.size())
              continue;
            if (right_type == kPurpleMiddle &&
                right_hint.text.size() != right_form.text.size())
              continue;
            if (right_type == kRedMiddle &&
                right_hint.text.size() - right_form.text.size() > 3)
              continue;

            if (form_by_text_.count(left_hint.text + right_hint.text)) {
              combos_[left_type][right_type].emplace_back(
                  form_by_text_[left_hint.text + right_hint.text], -1,
                  form_by_text_[left_form.text + right_form.text]);
            } else if (left_hint.is_base_form && right_hint.is_base_form &&
                       !(left_type == kPurpleTop && right_type == kPurpleTop)) {
              combos_[left_type][right_type].emplace_back(
                  left_hint_id, right_hint_id,
                  form_by_text_[left_form.text + right_form.text]);
            }
          }
        }
      }
    }
  }
}