-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertor.py
280 lines (218 loc) · 5.94 KB
/
insertor.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/local/bin/python
import os
import sys
"""
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
"""
from stream import Stream
labels = ["NML","JJP"]
class Node:
def __init__(self):
self.children = []
self.leaf = False
self.word = ""
def read(self, stream, start = 0):
self.name = stream.readWord('(')
#this is for sentences that begin with 2 open brackets with no space between, eg: ((SINV
if len(self.name) > 0:
if self.name[-1] == "(":
self.name = self.name[:-1]
stream.back()
self.start = start
length = 0
while True:
#read nextchar
nextchar = stream.get()
if nextchar.isspace():
continue
elif nextchar == ')':
break
elif nextchar == '(':
child = Node()
length += child.read(stream,start+length)
self.children.append(child)
else: #must be leaf
if not self.leaf:
self.leaf = True
length += 1
self.word += nextchar
self.end = start+length-1
return length
def show(self, level = 0):
if self.leaf:
print " "*level+"("+self.name,self.word+")"
else:
print " "*level+"("+self.name
for child in self.children:
child.show(level+1)
print " "*level+")"
def expand(self, num):
children = self.children[num].children
children.reverse() #so they go in the right way later
del self.children[num]
for child in children:
self.children.insert(num,child)
def factor(self, num1, num2, label):
newnode = Node()
newnode.name = label
newnode.start = self.children[num1].start
newnode.end = self.children[num2].end
for j in range(num1,num2+1):
newnode.children.append(self.children[num1])
del self.children[num1]
#the others will move down into num1's position
self.children.insert(num1,newnode)
def inorder(self, labels=[]):
if self.leaf:
return [self]
res = []
for child in self.children:
if labels == [] or child.leaf or child.name in labels:
res += child.inorder(labels)
return res
def diff(self, compnode, labels = []):
nolabels = len(labels) == 0
if self.name != compnode.name:
return True
if len(self.children) != len(compnode.children):
return True
for child, compchild in zip(self.children,compnode.children):
if child.name in labels or nolabels:
if child.diff(compchild, labels):
return True
return False
def length(self,labels):
if self.leaf:
return 1
length =0
for child in self.children:
if child.name in labels or labels == []:
length += child.length(labels)
else:
length += 1
return length
#tell the node where it starts, and it'll tell you where it ends, plus where its children do
def spans(self, start=0, labels = []):
resspans = {}
total = 0
for i,child in enumerate(self.children):
if child.leaf:
total += 1
else:
#print "recursing from",self.name
length, recspans = child.spans(start+total,labels)
if child.name in labels or labels == []:
for key in recspans:
value = recspans[key]
resspans[key] = value
#resspans += recspans
resspans[(start+total,start+total+length-1)] = child#.name
#resspans.append((start+total,start+total+length-1))
total += length
return total, resspans
def ptb_show(self, level = 0):
if self.leaf:
#result = "("+self.name+' '+self.word+'|'+str(self.start)+'|'+str(self.end)+")"+' '
result = "("+self.name+' '+self.word+")"+' '
else:
#result = "("+self.name+'|'+str(self.start)+'|'+str(self.end)+' '
result = "("+self.name+' '
newlining = False
for i,child in enumerate(self.children):
if child.leaf:
if child.name == "," or child.name == 'CC':
newlining = True
else:
if self.name != "":
newlining = True
if newlining:
result += '\n'+" "*(level+1)
result += child.ptb_show(level+1)
if child.leaf:
if child.name != ',':
newlining = False
result += ")"
return result
def insert(self,start,end,label,depth=0):
#print "\t"*depth, self.name
for child in self.children:
if not child.leaf:
if child.insert(start,end,label, depth+1):
return True
if self.start <= start and self.end >= end:
print "found at",self.start,self.end
#now just figure out which children
for i,child in enumerate(self.children):
#print "child s:",child.start,"e:",child.end
if child.start == start:
cstart = i
if child.end == end:
cend = i
self.factor(cstart,cend,label)
return True
return False
if len(sys.argv) != 4:
print "usage:"
print "\t./insertor.py <Treebank source directory> <output directory> <structure file>"
sys.exit()
indir = sys.argv[1]
outdir = sys.argv[2]
structure = sys.argv[3]
def finalslash(word):
if word[-1] != "/":
return word + "/"
else:
return word
indir = finalslash(indir)
outdir = finalslash(outdir)
files = os.listdir(indir)
files.sort()
try:
junk = os.listdir(outdir)
except OSError:
os.mkdir(outdir)
sfile = open(structure)
slines = sfile.readlines()
sfile.close()
supto = 0
for fname in files:
print "OPENING NEW FILE:",fname
f = open(indir+fname)
data = f.read()
f.close()
newf = open(outdir+fname,'w')
newf.write('\n')
stream = Stream(data)
while True:
#read '('
while True:
char = stream.get()
if char == '(' or stream.eof():
break
if stream.eof():
break
root = Node()
root.read(stream)
#insert the structure into standard PTB
sline = slines[supto].strip()
factors = []
while sline != "---":
print "inserting:",sline
label,start,end = sline.split()
start = int(start)
end = int(end)
diff = end-start
factors.append((diff,start,end,label))
supto+=1
sline = slines[supto].strip()
supto +=1
#sort by diff, so that the inner (smallest) nodes are inserted first
factors.sort()
#root.show()
for diff,start,end,label in factors:
root.insert(start,end,label)
#output
newf.write(root.ptb_show()+'\n')
newf.close()
print "FILE CLOSED:", fname
print "all done"