-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_build.py
executable file
·56 lines (41 loc) · 1.14 KB
/
05_build.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
#!/usr/bin/env python3
import json
import os
import re
import sys
def render(f, tree):
render_r(f, tree)
f.write('\n')
def render_r(f, tree):
if isinstance(tree, str):
f.write(tree)
f.write(' ')
elif isinstance(tree, list):
tag = tree[0]
assert isinstance(tag, str)
for child in tree[1:]:
render_r(f, child)
def enter(map, tree):
if isinstance(tree, str):
return
elif isinstance(tree, list):
# key each tree by its part of speech plus the first word (only) of its content.
words = []
for child in tree:
if isinstance(child, str):
words.append(child)
key = '-'.join(words[:2])
map.setdefault(key, []).append(tree)
for child in tree[1:]:
enter(map, child)
def main():
map = {}
with open('data/trees.json', 'r') as f:
data = json.loads(f.read())
# for tree in data['trees']:
# render(sys.stdout, tree)
for tree in data['trees']:
enter(map, tree)
with open('data/productions.json', 'w') as f:
f.write(json.dumps(map, indent=4))
main()