Capitalize generated sentences. Allow novel to be given a title.
Chris Pressey
3 years ago
7 | 7 | |
8 | 8 | |
9 | 9 | def render(f, productions, tree): |
10 | render_r(f, productions, tree) | |
10 | segments = [] | |
11 | render_r(segments, productions, tree) | |
12 | segments[0] = segments[0].title() | |
13 | f.write(' '.join(segments)) | |
11 | 14 | f.write('.\n\n') |
12 | 15 | |
13 | def render_r(f, productions, tree): | |
16 | def render_r(segments, productions, tree): | |
14 | 17 | if isinstance(tree, str): |
15 | f.write(tree) | |
16 | f.write(' ') | |
18 | segments.append(tree) | |
17 | 19 | elif isinstance(tree, list): |
18 | 20 | |
19 | 21 | # key each tree by its part of speech plus the first word (only) of its content. |
29 | 31 | tree = random.choice(productions[key]) |
30 | 32 | |
31 | 33 | for child in tree[1:]: |
32 | render_r(f, productions, child) | |
34 | render_r(segments, productions, child) | |
33 | 35 | |
34 | 36 | |
35 | 37 | def main(): |
38 | title = sys.argv[1] | |
36 | 39 | map = {} |
37 | 40 | |
38 | 41 | with open('data/productions.json', 'r') as f: |
39 | 42 | productions = json.loads(f.read()) |
40 | 43 | |
44 | sys.stdout.write('# {}\n\n'.format(title)) | |
41 | 45 | for c in range(1, 37): |
42 | sys.stdout.write('# Chapter {}\n\n'.format(c)) | |
46 | sys.stdout.write('## Chapter {}\n\n'.format(c)) | |
43 | 47 | for i in range(100): |
44 | 48 | tree = random.choice(productions['S']) |
45 | 49 | render(sys.stdout, productions, tree) |