git @ Cat's Eye Technologies Dissociated-Parse / c12ec1d
Capitalize generated sentences. Allow novel to be given a title. Chris Pressey 3 years ago
2 changed file(s) with 12 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
77
88
99 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))
1114 f.write('.\n\n')
1215
13 def render_r(f, productions, tree):
16 def render_r(segments, productions, tree):
1417 if isinstance(tree, str):
15 f.write(tree)
16 f.write(' ')
18 segments.append(tree)
1719 elif isinstance(tree, list):
1820
1921 # key each tree by its part of speech plus the first word (only) of its content.
2931 tree = random.choice(productions[key])
3032
3133 for child in tree[1:]:
32 render_r(f, productions, child)
34 render_r(segments, productions, child)
3335
3436
3537 def main():
38 title = sys.argv[1]
3639 map = {}
3740
3841 with open('data/productions.json', 'r') as f:
3942 productions = json.loads(f.read())
4043
44 sys.stdout.write('# {}\n\n'.format(title))
4145 for c in range(1, 37):
42 sys.stdout.write('# Chapter {}\n\n'.format(c))
46 sys.stdout.write('## Chapter {}\n\n'.format(c))
4347 for i in range(100):
4448 tree = random.choice(productions['S'])
4549 render(sys.stdout, productions, tree)
3636 ./03_sentencify.py
3737 ./04_parse.py # this one will take a while
3838 ./05_build.py
39 ./06_traverse.py
39 ./06_traverse.py 'Give your Novel a Title Here' >out.md
40 wc -w out.md