-
Notifications
You must be signed in to change notification settings - Fork 2
/
syn-agreement.py
executable file
·315 lines (261 loc) · 9.95 KB
/
syn-agreement.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
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
#!/usr/bin/env pypy
try:
import numpypy
except:
pass
from alpha import krippendorff_alpha as alpha
import codecs
import conll
from conll import ConllCorpus
from getopt import getopt
import nltk
from nltk.corpus.reader.bracket_parse import BracketParseCorpusReader
from nltk.tree import Tree
from numpy import empty
import os
import sys
from zss.compare import simple_distance as distance
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
class Error(Exception):
def __init__(self, str):
self.msg = str
def __str__(self): return self.msg
def die(message): raise Error(message)
def strdist(x, y):
if x == y:
return 0
else:
return 1
def delta_conll(a, b):
return float(distance(a.tokens[0], b.tokens[0],
get_label=lambda t: t.deprel or "",
get_children=lambda t: t.children,
label_dist=strdist))
def delta_tree(a, b):
return float(distance(a, b,
get_label=lambda t: t.label(),
get_children=lambda t: t,
label_dist=strdist))
def read_conll(file):
return conll.read_corpus(file)
# This is a dummy subclass of list. We need to return one of these from
# read_tree because aggregate_tree needs to receive an object which it can set
# new attributes on, and you can't do that on instances of built-in classes
# (but you can on user-defined subclasses of them. Python internals
# weirdness.).
class FiddlyList(list): pass
def read_tree(file):
def munge(t):
if type(t) == Tree:
toks = t.leaves()
t = Tree(t.label(), [munge(child) for child in t])
setattr(t, "tokens", toks)
return t
else:
return Tree(t, [])
return FiddlyList(munge(t) for t in BracketParseCorpusReader(".", file).parsed_sents())
def aggregate_conll(*dirnames):
dirs = {dir: conlls(dir) for dir in dirnames}
filenames = set()
for dir in dirs.values():
filenames = filenames | dir
uniques = sorted(list(set(filenames)))
def reader(dir, file):
try:
#file = file+os.path.basename(dir)+".conll"
corpus = read_corpus(os.path.join(dir, file+os.path.basename(dir)+".conll"))
corpus.name = file
return corpus
except IOError:
return None
def same_lengths(*corpora):
n = 0
for sents in zip(*corpora):
l = len(sents[0].tokens)
for s in sents[1:]:
if len(s.tokens) != l: return n
n += 1
return n # This should never happen.
# 1) Create map of dir => ConllCorpus|None
data = {dir: [reader(dir, file) for file in uniques] for dir in dirs.keys()}
# Basic sanity check and build dict of corpus lengths
lengths = {}
for tuple in zip(*data.values()):
has_data = [c for c in tuple if c]
first = has_data[0]
length = len(first)
name = first.name
for c in has_data[1:]:
if len(c) != length:
die("Differing lengths for %s (first %d sentences are same length)" % (name, same_lengths(*has_data)))
lengths[name] = length
def conjoin(annotator, *corpora):
catted = ConllCorpus([], annotator)
for name, corpus in zip(uniques, corpora):
if corpus is None:
catted.sentences += [None] * lengths[name]
else:
catted.sentences += corpus.sentences
return catted
return [conjoin(name, *corpora) for name, corpora in data.items()]
def aggregate_tree(*dirnames):
dirs = {dir: trees(dir) for dir in dirnames}
filenames = set()
for dir in dirs.values():
filenames = filenames | dir
uniques = sorted(list(set(filenames)))
def reader(dir, file):
if os.path.exists(os.path.join(dir, file+os.path.basename(dir)+".tree")):
corpus = read_corpus(os.path.join(dir, file+os.path.basename(dir)+".tree"))
corpus.name = file
return corpus
else:
return None
def same_lengths(*corpora):
n = 0
for sents in zip(*corpora):
l = len(sents[0].tokens)
for s in sents[1:]:
if len(s.tokens) != l: return n
n += 1
return n # This should never happen.
# 1) Create map of dir => BracketParseCorpusReader|None
data = {dir: [reader(dir, file) for file in uniques] for dir in dirs.keys()}
# Basic sanity check and build dict of corpus lengths
lengths = {}
for tuple in zip(*data.values()):
has_data = [c for c in tuple if c]
first = has_data[0]
length = len(first)
name = first.name
for c in has_data[1:]:
if len(c) != length:
die("Differing lengths for %s (first %d sentences are same length)" % (name, same_lengths(*has_data)))
lengths[name] = length
def conjoin(annotator, *corpora):
catted = []
for name, corpus in zip(uniques, corpora):
if corpus is None:
catted += [None] * lengths[name]
else:
catted += corpus
return catted
return [conjoin(name, *corpora) for name, corpora in data.items()]
def conlls(dir):
return set(file[:-(len(os.path.basename(dir))+6)] for file in sorted(os.listdir(dir)) if file.endswith('.conll'))
def trees(dir):
return set(file[:-(len(os.path.basename(dir))+5)] for file in os.listdir(dir) if file.endswith('.tree'))
def jaccard(a, b):
def number(tree):
if len(tree) > 0:
return Tree(tree.label(), [number(child) for child in tree])
else:
r = i[0]
i[0] += 1
return Tree(tree.label(), [r])
def bracket_set(tree):
brackets = set()
for position in tree.treepositions():
t = tree[position]
if type(t) != Tree: continue
(l, r) = (t.leaves()[0], t.leaves()[-1])
brackets.add("%d,%d,%s"%(l, r, t.label()))
return brackets
# XXX: Silly hack to share state with number(), since I don't have time to
# refactor it properly.
i = [1] # List because Python 2 doesn't have nonlocal
a = number(a)
i = [1] # List because Python 2 doesn't have nonlocal
b = number(b)
bracket_a = bracket_set(a)
bracket_b = bracket_set(b)
intersection = bracket_a & bracket_b
union = bracket_a | bracket_b
return {'jaccard': float(len(intersection))/len(union), 'tokens': len(a.leaves())}
def pairwise_jaccard(*sentence_lists):
def same_length(*sentences):
l = len(sentences[0].leaves())
for s in sentences[1:]:
if l != len(s.leaves()):
return False
return True
def do_pairs(*sentences):
jacc = 0.0
for i in xrange(len(sentences)):
for j in xrange(i+1, len(sentences)):
cmp = jaccard(sentences[i], sentences[j])
jacc += cmp['jaccard']
n = len(sentences)
n = n*(n-1)/2
return {'jaccard': jacc/n, 'tokens': cmp['tokens']}
jacc = 0.0
ignored = 0
tokens = 0
for sentences in zip(*sentence_lists):
sentences = [sent for sent in sentences if sent]
if len(sentences) == 1: continue
if not same_length(*sentences):
ignored += 1
continue
cmp = do_pairs(*sentences)
l = cmp['tokens']
jacc += l*cmp['jaccard']
tokens += l
score = jacc/tokens
return {'UAS': score, 'LAS': score, 'lbl': score, 'ignored': ignored}
metrics = {'plain': lambda a, b: delta(a,b)**2,
'diff': lambda a, b: (delta(a,b)-abs(len(a.tokens)-len(b.tokens)))**2,
'norm': lambda a, b: (delta(a,b)/(len(a.tokens) + len(b.tokens)))**2}
# TODO: Labelled vs. unlabelled alpha
options, args = getopt(sys.argv[1:], 'x:', ['metric=', 'acc', 'dirs', 'tree', 'conll', 'help'])
options = {key: value for key, value in options}
if '--help' in options:
print "Usage: %s [--tree|--conll] [--acc] [--metric=plain|diff|norm|all] fileA fileB" % sys.argv[0]
print " %s [--tree|--conll] [--acc] [--metric=plain|diff|norm|all] --dirs dir..." % sys.argv[0]
sys.exit()
if '--tree' in options and '--conll' in options:
raise Error("--tree and --conll' are mutually exclusive")
elif '--tree' in options:
read_corpus = read_tree
delta = delta_tree
aggregate = aggregate_tree
# Because NLTK's trees aren't normally hashable and I'm too lazy to
# convert everything into an ImmutableTree (which is).
Tree.__hash__ = lambda t: hash( (t.label(), tuple(t)) )
else:
read_corpus = read_conll
delta = delta_conll
aggregate = aggregate_conll
metric = options.get('--metric', 'plain')
if metric != 'all' and metric not in metrics:
raise Error("Unknown metric: %s" % metric)
# TODO: Reading both CoNLL graphs and bracketed trees. Just rely on file
# extensions, or do something more clever to detect file types?
if '--dirs' not in options:
corpusA = read_corpus(args[0])
corpusB = read_corpus(args[1])
if len(corpusA) != len(corpusB):
print >> sys.stderr, "Corpora have different lengths"
sys.exit(1)
corpora = [corpusA, corpusB]
else:
corpora = aggregate(*[os.path.normpath(dir) for dir in args])
if '--acc' in options:
if '--tree' in options:
cmp = pairwise_jaccard(*corpora)
else:
cmp = conll.pairwise_compare(*corpora)
if metric == 'all':
for name, d in metrics.items():
print "\\alpha_%s=%f" % (name, alpha(corpora, metric=d, convert_items=lambda s: s))
if '--acc' in options:
print "UAS: ", cmp['UAS']
print "LAS: ", cmp['LAS']
print "lbl: ", cmp['lbl']
if cmp.get('ignored', 0) > 0:
print "Ignored %d sentences in accuracy computation" % cmp['ignored']
else:
print alpha(corpora, metric=metrics[metric], convert_items=lambda s: s),
if '--acc' in options:
print cmp['UAS'], cmp['LAS'], cmp['lbl'],
print ''