-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcrossword_generator.py
executable file
·91 lines (74 loc) · 3.17 KB
/
crossword_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3
""" Crossword Generator
This script takes a list of words and creates a new latex table representing a
crossword puzzle, which is then printed to PDF. You can then print it to actual
paper, if you're one of those people.
"""
# Standard imports
import argparse
# Custom imports
import file_ops
import grid_generator
from grid_generator import GridGenerator
def parse_cmdline_args():
""" Uses argparse to get commands line args.
"""
parser = argparse.ArgumentParser(description='Generate a crossword puzzle.')
parser.add_argument('-f', type=str,
default="words.txt",
dest="word_file",
help="A file containing words, one word per line.")
parser.add_argument('-d', type=int,
nargs="+",
default=[20, 20],
dest="dim",
help="Dimensions of the grid to build.")
parser.add_argument('-n', type=int,
default=1,
dest="n_loops",
help="NUmber of execution loops to run.")
parser.add_argument('-t', type=int,
default=10,
dest="timeout",
help="Maximum execution time, in seconds, per execution loop.")
parser.add_argument('-o', type=float,
default=1.0,
dest="target_occ",
help="Desired occupancy of the final grid. Default is 1.0, which just uses all of the allotted time.")
parser.add_argument('-p', type=str,
default="out.pdf",
dest="out_pdf",
help="Name of the output pdf file.")
parser.add_argument('-a', type=str,
default="basic",
dest="algorithm",
help="The algorithm to use.")
return parser.parse_args()
def create_generator(algorithm, word_list, dimensions, n_loops, timeout, target_occupancy):
""" Constructs the generator object for the given algorithm.
"""
algorithm_class_map = {"basic": GridGenerator}
try:
return algorithm_class_map[algorithm](word_list, dimensions, n_loops, timeout, target_occupancy)
except KeyError:
print("Could not create generator object for unknown algorithm: {}.".format(algorithm))
def main():
# Parse args
args = parse_cmdline_args()
# Read words from file
words = file_ops.read_word_list(args.word_file)
print("Read {} words from file.".format(len(words)))
# Construct the generator object
dim = args.dim if len(args.dim)==2 else [args.dim[0], args.dim[0]]
generator = create_generator(args.algorithm, words, dim, args.n_loops, args.timeout, args.target_occ)
if not generator:
return
# Generate the grid
generator.generate_grid()
# Write it out
grid = generator.get_grid()
words_in_grid = generator.get_words_in_grid()
file_ops.write_grid_to_file(grid, words=[x["word"] for x in words_in_grid], out_pdf=args.out_pdf)
file_ops.write_grid_to_screen(grid, words_in_grid)
if __name__ == "__main__":
main()