-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalFG.py
More file actions
389 lines (358 loc) · 11.5 KB
/
Copy pathevalFG.py
File metadata and controls
389 lines (358 loc) · 11.5 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#python evalFG.py [--collapsed] --test testFile --gold goldFile > outputFile
#testFile: output from scripts/testFG.py
#goldFile: a file with gold labellings to eval against
#
#OPTS:
# --collapsed: evals against collapsed arg labels (labels >= 1 := 1)
from __future__ import division
import sys
import string
import ast
OPTS = {}
for aix in range(1,len(sys.argv)):
if len(sys.argv[aix]) < 2 or sys.argv[aix][:2] != '--':
#filename or malformed arg
continue
elif (aix < len(sys.argv) - 1 and len(sys.argv[aix+1]) > 2 and sys.argv[aix+1][:2] == '--') or aix == len(sys.argv)-1:
#missing filename
OPTS[sys.argv[aix][2:]] = True
continue
else:
OPTS[sys.argv[aix][2:]] = sys.argv[aix+1]
COLLAPSE_OBJ = False
if 'collapsed' in OPTS:
COLLAPSE_OBJ = True
THET = False
wfuncwords = ['who','what','which']
if THET:
tfuncwords = ['thet']
else:
tfuncwords = ['that']
def parseLine(line):
#sys.stderr.write(str(line)+'\n')
lineout = '['
comma = False
word = ''
first = True
for c in line.strip():
if word != '' and c not in string.letters+"_":
lineout = lineout + '\''+word+'\''
word = ''
if c == ',':
comma = True
elif not comma and c == ' ':
lineout = lineout + ','
elif comma and c != ' ':
comma = False
if c in string.letters+"_":
word = word + c
elif c == '\'':
continue
elif c == ',' and lineout[-2] == ',':
#if there's a comma in the text just skip it to avoid confusing literal_eval
continue
else:
lineout = lineout + c
if word != '':
#flush buffer if no punctuation at end of sentence
lineout = lineout + '\''+word+'\''
if lineout[-1] in '.?!':
#sys.stderr.write(str(lineout[:-1]+'\''+lineout[-1]+'\'')+'\n')
return ast.literal_eval(lineout[:-1]+'\''+lineout[-1]+'\']')
elif lineout[-2] in '.?!':
#account for stupid assignment of roles to punctuation
#sys.stderr.write(str(lineout[:-1]+'\''+lineout[-1]+'\'')+'\n')
return ast.literal_eval(lineout[:-2]+'\''+lineout[-2]+'\')]')
else:
#sys.stderr.write(lineout+'\n')
return ast.literal_eval(lineout+']')
#########################
#
# Load Test File
#
#########################
testCorpus = []
testFile = open(OPTS['test'],'r')
for line in testFile.readlines():
testSent = []
for w in parseLine(line):
testSent.append(w)
testCorpus.append(testSent)
testFile.close()
#########################
#
# Load Gold File
#
#########################
goldCorpus = []
goldFile = open(OPTS['gold'],'r')
for line in goldFile.readlines():
goldSent = []
for w in parseLine(line):
goldSent.append(w)
goldCorpus.append(goldSent)
goldFile.close()
#########################
#
# Run Eval
#
#########################
if COLLAPSE_OBJ:
sys.stderr.write('Collapsing A1-A4 into A1\n')
sys.stdout.write('Collapsing A1-A4 into A1\n')
accuracy = []
atts = []
sacc = [[0,0],[0,0]]
oacc = [[0,0],[0,0]]
tacc = [[0,0],[0,0]]
wacc = [[0,0],[0,0]]
agentacc = [[0,0],[0,0]] #accuracy of recalling agent in intrans and trans conditions
txacc = [[0,0],[0,0]]
wxacc = [[0,0],[0,0]]
precision = [0, 0] #correct hits , total hits
recall = [0, 0]
wronglabel = 0
####
#
# Actual eval
#
####
for i,ts in enumerate(testCorpus):
latts = []
lprecision = [0, 0]
lrecall = [0, 0]
arecall = [0, 0]
first = ''
func = {'T':0,'W':0}
THAT = False
WH = False
TRANS = False
gmod = 0 #mod-counter to align gold and test sentences
### DEBUG
#sys.stderr.write('ts: '+str(ts)+'\n'+'gold: '+str(goldCorpus[i])+'\n')
### \DEBUG
for j,tw in enumerate(ts):
if type(tw) == type(()):
testword = tw[-1]
else:
testword = tw
while (type(goldCorpus[i][j+gmod]) == type(()) and testword != goldCorpus[i][j+gmod][0].strip("'")) or \
(type(goldCorpus[i][j+gmod]) != type(()) and testword != goldCorpus[i][j+gmod]):
### DEBUG
#sys.stderr.write(str(tw)+'?='+str(goldCorpus[i][j+gmod])+' i: '+str(i)+'/'+str(len(goldCorpus))+' j: '+str(j+gmod)+'/'+str(len(goldCorpus[i]))+'\n')
###\DEBUG
gmod += 1 #keep gold and test in sync
if testword == 'cannot' and goldCorpus[i][j+gmod-1] == 'can' and goldCorpus[i][j+gmod] == 'not':
break #standardize 'cannot' across both the test and gold corpora
if type(goldCorpus[i][j+gmod]) == type(()):
if goldCorpus[i][j+gmod][0] in tfuncwords:
THAT = True
elif goldCorpus[i][j+gmod][0] in wfuncwords:
WH = True
else:
if goldCorpus[i][j+gmod] in tfuncwords:
THAT = True
elif goldCorpus[i][j+gmod] in wfuncwords:
WH = True
if type(tw) == type(()) and type(tw[0]) != type(1):
#hit
lprecision[1] += 1
if type(goldCorpus[i][j+gmod]) == type(()):
#possible true hit
lrecall[1] += 1
if first == "" and goldCorpus[i][j+gmod][-1] != 'V':
first = goldCorpus[i][j+gmod][-1]
if goldCorpus[i][j+gmod][-1] == 1:
TRANS = True
elif goldCorpus[i][j+gmod][-1] == 0:
arecall[1] = 1
if goldCorpus[i][j+gmod][-1] == 'V':
#verbs don't count against us
lrecall[1] -= 1
if tw[0] == 'V' or tw[0][0] == 'F':
#verbs and function words don't count against us
lprecision[1] -= 1
elif tw[0] == 'V' or tw[0][0] == 'F':
#verbs don't count for or against us
lprecision[1] -= 1
elif tw[0][0] == goldCorpus[i][j+gmod][-1]:
#true hit
lprecision[0] += 1
lrecall[0] += 1
if tw[0][0] == 0:
arecall[0] = 1
elif COLLAPSE_OBJ and goldCorpus[i][j+gmod][-1] >= 1 and tw[0][0] == 1:
#collapsed arg true hit
lprecision[0] += 1
lrecall[0] += 1
else:
#chose wrong label
wronglabel += 1
elif tw[0] == 'V' or tw[0][0] == 'F':
#function words aren't silver annotated, so don't eval those
# they are indirectly being evaluated since they modify the other arg labels
lprecision[1] -= 1
if goldCorpus[i][j+gmod][-1] == 1:
TRANS = True
if goldCorpus[i][j+gmod] in tfuncwords:
func['T'] += 1
elif goldCorpus[i][j+gmod] in wfuncwords:
func['W'] += 1
elif goldCorpus[i][j+gmod][-1] == 0:
arecall[1] = 1
else:
#false hit
if goldCorpus[i][j+gmod][-1] == 1:
TRANS = True
if goldCorpus[i][j+gmod] in tfuncwords:
func['T'] += 1
elif goldCorpus[i][j+gmod] in wfuncwords:
func['W'] += 1
elif goldCorpus[i][j+gmod][-1] == 0:
arecall[1] = 1
else:
#possible miss
if type(goldCorpus[i][j+gmod]) == type(()) and goldCorpus[i][j+gmod][-1] != 'V':
#false negative (don't penalize for unlabelled verbs...)
if first == "":
first = goldCorpus[i][j+gmod][-1]
lrecall[1] += 1
if goldCorpus[i][j+gmod][-1] == 1:
TRANS = True
elif goldCorpus[i][j+gmod][-1] == 0:
arecall[1] = 1
else:
#true negative
if goldCorpus[i][j+gmod] in tfuncwords:
func['T'] += 1
elif goldCorpus[i][j+gmod] in wfuncwords:
func['W'] += 1
# sys.stdout.write('P: '+str(lprecision)+' R: '+str(lrecall)+'\n')
# sys.stdout.write(str(func)+'\n')
if first == 0:
sacc[0][0] += lprecision[0]
sacc[0][1] += lprecision[1]
sacc[1][0] += lrecall[0]
sacc[1][1] += lrecall[1]
latts.append('S')
else:
oacc[0][0] += lprecision[0]
oacc[0][1] += lprecision[1]
oacc[1][0] += lrecall[0]
oacc[1][1] += lrecall[1]
latts.append('O')
if func['W'] > 0 or func['T'] > 0:
#sentence involves a functional use/relativizer
if func['W'] > func['T']:
#wh- sentence
wacc[0][0] += lprecision[0]
wacc[0][1] += lprecision[1]
wacc[1][0] += lrecall[0]
wacc[1][1] += lrecall[1]
latts.append('FW')
else:
#that sentence
tacc[0][0] += lprecision[0]
tacc[0][1] += lprecision[1]
tacc[1][0] += lrecall[0]
tacc[1][1] += lrecall[1]
latts.append('FT')
if THAT:
#that sentence
txacc[0][0] += lprecision[0]
txacc[0][1] += lprecision[1]
txacc[1][0] += lrecall[0]
txacc[1][1] += lrecall[1]
latts.append('T')
if WH:
#wh- sentence
wxacc[0][0] += lprecision[0]
wxacc[0][1] += lprecision[1]
wxacc[1][0] += lrecall[0]
wxacc[1][1] += lrecall[1]
latts.append('W')
if TRANS:
#transitive sentence
agentacc[1][0] += arecall[0]
agentacc[1][1] += arecall[1]
latts.append('Trans')
else:
#intransitive sentence
agentacc[0][0] += arecall[0]
agentacc[0][1] += arecall[1]
latts.append('Intrans')
accuracy.append( (lprecision,lrecall) )
precision[0] += lprecision[0]
precision[1] += lprecision[1]
recall[0] += lrecall[0]
recall[1] += lrecall[1]
atts.append(latts)
P = precision[0]/precision[1]
R = recall[0]/recall[1]
F = 2*P*R/(P+R)
SP = sacc[0][0]/sacc[0][1]
SR = sacc[1][0]/sacc[1][1]
SF = 2*SP*SR/(SP+SR)
if oacc[0][1] > 0 and oacc[1][1] > 0:
#we have to guess at least one obj-extract and there must be at least one, or we don't report this
OP = oacc[0][0]/oacc[0][1]
OR = oacc[1][0]/oacc[1][1]
OF = 2*OP*OR/(OP+OR)
else:
OP = float('-inf')
OR = float('-inf')
OF = float('-inf')
try:
IAg = agentacc[0][0] / agentacc[0][1]
except:
IAg = float('-inf')
try:
TAg = agentacc[1][0] / agentacc[1][1]
except:
TAg = float('-inf')
try:
WP = wacc[0][0]/wacc[0][1]
WR = wacc[1][0]/wacc[1][1]
WF = 2*WP*WR/(WP+WR)
except:
WP = 0
WR = 0
WF = 0
if tacc[0][1] > 0 and tacc[1][1] > 0:
TP = tacc[0][0]/tacc[0][1]
TR = tacc[1][0]/tacc[1][1]
TF = 2*TP*TR/(TP+TR)
else:
TP = float('-inf')
TR = float('-inf')
TF = float('-inf')
if wxacc[0][1] > 0 and wxacc[1][1] > 0:
WXP = wxacc[0][0]/wxacc[0][1]
WXR = wxacc[1][0]/wxacc[1][1]
WXF = 2*WXP*WXR/(WXP+WXR)
else:
WXP = float('-inf')
WXR = float('-inf')
WXF = float('-inf')
if txacc[0][1] > 0 and txacc[1][1] > 0:
TXP = txacc[0][0]/txacc[0][1]
TXR = txacc[1][0]/txacc[1][1]
TXF = 2*TXP*TXR/(TXP+TXR)
else:
TXP = float('-inf')
TXR = float('-inf')
TXF = float('-inf')
W = wronglabel/precision[1]
sys.stdout.write('Precision: %d/%d (%f) Recall: %d/%d (%f) F-Score: %f\n' % (precision[0],precision[1],P,recall[0],recall[1],R,F))
sys.stdout.write('SubjPrec: %d/%d (%f) SubjRecall: %d/%d (%f) SubjF-Score: %f -- ObjPrec: %d/%d (%f) ObjRecall: %d/%d (%f) ObjF-Score: %f\n' % (sacc[0][0],sacc[0][1],SP,sacc[1][0],sacc[1][1],SR,SF,oacc[0][0],oacc[0][1],OP,oacc[1][0],oacc[1][1],OR,OF))
sys.stdout.write('Functional:\n')
sys.stdout.write('WhPrec: %d/%d (%f) WhRecall: %d/%d (%f) WhF-Score: %f -- ThatPrec: %d/%d (%f) ThatRecall: %d/%d (%f) ThatF-Score: %f\n' % (wacc[0][0],wacc[0][1],WP,wacc[1][0],wacc[1][1],WR,WF,tacc[0][0],tacc[0][1],TP,tacc[1][0],tacc[1][1],TR,TF))
sys.stdout.write('Observed:\n')
sys.stdout.write('WhPrec: %d/%d (%f) WhRecall: %d/%d (%f) WhF-Score: %f -- ThatPrec: %d/%d (%f) ThatRecall: %d/%d (%f) ThatF-Score: %f\n' % (wxacc[0][0],wxacc[0][1],WXP,wxacc[1][0],wxacc[1][1],WXR,WXF,txacc[0][0],txacc[0][1],TXP,txacc[1][0],txacc[1][1],TXR,TXF))
sys.stdout.write('WrongLabel: %d/%d (%f)\n' % (wronglabel,precision[1],W))
sys.stdout.write('Intransitive Agent Recall: %d/%d (%f) -- Transitive Agent Recall: %d/%d (%f)\n\n' % (agentacc[0][0],agentacc[0][1],IAg,agentacc[1][0],agentacc[1][1],TAg))
for i in range(len(testCorpus)):
sys.stdout.write('atts: %s\n' % (str(atts[i])))
sys.stdout.write('a: P(%d/%d) R(%d/%d)\n' % (accuracy[i][0][0],accuracy[i][0][1],accuracy[i][1][0],accuracy[i][1][1]))
sys.stdout.write('t: '+str(testCorpus[i])+'\n')
sys.stdout.write('g: '+str(goldCorpus[i])+'\n')