-
Notifications
You must be signed in to change notification settings - Fork 1
/
head2pairs.py
88 lines (67 loc) · 1.87 KB
/
head2pairs.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
# Tocho Tchev's eval script
import sys
import pprint
from pyparsing import nestedExpr
pp = pprint.PrettyPrinter(indent=4)
def depgen(parse, parent, memory):
if not isinstance(parse[1], list):
return [(parse[0], memory, parent)]
else:
def determine_parent_memory(x):
if '-H' in x[0]:
return [parent, memory]
else:
return [memory, object()]
return sum([depgen(x,*determine_parent_memory(x))
for x in parse[1:]],[])
return []
def output(line):
txt = line.strip()
parse = nestedExpr('(',')').parseString(txt).asList()[0]
#print "\n\n"; pprint.pprint(parse)
depstruct = depgen(parse, None, object())
#pprint.pprint(depstruct)
parents = [x[2] for x in depstruct]
ids = [None] + [x[1] for x in depstruct]
try:
deps = [ids.index(p) for p in parents]
except:
pp.pprint(p)
pp.pprint(ids)
raise
#assert deps[1::2]==deps[::2], deps
#deps = [(d+1)/2 for d in deps[::2]]
for i in range(0, len(deps)):
if deps[i] > 0:
deps[i] = (deps[i]+1)/2
print ' '.join(map(str, deps[::2]))
"""
sentence = ([x[0] for x in depstruct])
print ' '.join(sentence)
for i,x in enumerate(sentence):
print '%s(%s)' % (x, (["ROOT"] + sentence)[deps[i]]) ,
else:
print
print
"""
def main():
# command line
if len(sys.argv) == 2:
in_path = sys.argv[1]
else:
in_path = '../parses'
fin = open(in_path, 'r')
while True:
s = fin.readline()
if len(s) == 0:
break
if s[0] != '(':
t = ''
s = s.split()
for i in range(0, len(s)-3):
t += '0 '
print t[::4]
else:
output(s)
if __name__ == '__main__':
main()