-
Notifications
You must be signed in to change notification settings - Fork 1
/
conllx_to_tikz_dep.py
executable file
·272 lines (221 loc) · 7.69 KB
/
conllx_to_tikz_dep.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
#!/usr/bin/env python
# Copyright 2012-2013 Tetsuo Kiso. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
"""
A simple CoNLL-X to tikz-dependency converter.
This converter aims to provide a funtion to visualize a simple
dependency tree through converting from the CoNLL-X data format to the tikz-dependency.
Usage:
$ ./conllx_to_tikz_dep.py [options] conllx_data
or
$ cat conllx_data | ./conllx_to_tikz_dep.py
For the details of the input data format, see the following link:
CoNLL-X Shared Task: Multi-lingual Dependency Parsing
http://ilk.uvt.nl/conll/#dataformat
"""
import optparse
import sys
matrix_separator = '\^'
special_chars = ['{', '}', '$', '&', '%']
correct_edge_color = 'red'
wrong_edge_color = 'blue!70!black'
class Token(object):
def __init__(self):
self.id = None
self.form = None
self.lemma = None
self.cpos = None
self.pos = None
self.feat = None
self.head = None
self.deprel = None
self.phead = None
self.pdeprel = None
def is_root(self):
return self.head == 0 or self.head == -1
class Sentence(object):
def __init__(self):
self.tokens = []
self.length = 0
def __getitem__(self, i):
return self.tokens[i]
def __setitem__(self, i, t):
self.tokens[i] = t
def add(self, t):
self.tokens.append(t)
self.length += 1
def length(self):
return self.length
def __str__(self):
return ' '.join([v.form for v in self.tokens])
def to_deptext(self):
base = ' %s ' % matrix_separator
s = base.join([v.form for v in self.tokens]) + ' \\\\'
return s
# returns a tuple of (head, dependent)
def iterate_edges(self):
for t in self.tokens:
if t.is_root():
continue
yield (t.id, t.head)
def iterate_labeled_edges(self):
for t in self.tokens:
if t.is_root():
continue
yield (t.id, t.head, t.deprel)
def zip(self, other):
for tup in zip(self.tokens, other.tokens):
if len(tup) != 2:
print 'Error: invalid input:', tup
sys.exit(1)
else:
if tup[0].is_root() and tup[1].is_root:
continue
yield ((tup[0].id, tup[0].head), (tup[1].id, tup[1].head))
def replace_special(s):
for c in special_chars:
s = s.replace(c, '\\' + c)
return s
def init_token(lis, is_conllx=True):
t = Token()
t.id = int(lis[0])
t.form = replace_special(lis[1])
t.lemma = lis[2]
t.cpos = lis[3]
t.pos = lis[4]
t.feat = lis[5]
t.head = int(lis[6])
if lis[7] == '_':
t.deprel = ''
else:
t.deprel = lis[7]
if is_conllx:
t.phead = lis[8]
t.pdeprel = lis[9]
return t
def open_conll(filename):
with open(filename) as f:
return read(f)
def read(f):
sents = []
s = Sentence()
for l in f:
if l.startswith('\n'):
sents.append(s)
s = Sentence()
continue
lis = l.rstrip().split('\t')
if len(lis) == 1:
lis = l.rstrip().split(' ')
if len(lis) == 0:
continue
elif len(lis) == 8:
s.add(init_token(lis, False))
elif len(lis) == 10:
s.add(init_token(lis))
else:
print 'Data format is broken!'
sys.exit(1)
return sents
# h: head, dependent.
def wrap_depedge(dep, h, opt=None):
if opt == None:
return '\depedge{%d}{%d}{}' % (h, dep)
else:
return '\depedge[%s]{%d}{%d}{}' % (opt, h, dep)
def wrap_depedge_with_label(dep, h, label, opt=None):
if opt == None:
return '\depedge{%d}{%d}{%s}' % (h, dep, label)
else:
return '\depedge[%s]{%d}{%d}{%s}' % (opt, h, dep, label)
def wrap_depedges(sent):
return '\n'.join([wrap_depedge(dep, h) for dep, h in sent.iterate_edges()])
def wrap_depedges_with_label(sent):
return '\n'.join([wrap_depedge_with_label(dep, h, l) for dep, h, l in sent.iterate_labeled_edges()])
class LaTeXFormatter(object):
def __init__(self, doc_opt, tikz_dep_opt, tikz_deptxt_opt, with_label=False):
self.doc_opt = doc_opt
self.tikz_dep_opt = tikz_dep_opt
self.tikz_deptxt_opt = tikz_deptxt_opt
self.with_label = with_label
self.dep_template = '''\\begin{dependency}[%s]
\\begin{deptext}[%s]
%s
\end{deptext}
%s
\end{dependency}'''
def latex_header(self):
return '''\documentclass{%s}
\usepackage{tikz-dependency}
\\begin{document}''' % self.doc_opt
def latex_footer(self):
return '''\end{document}'''
def print_tikz_dep(self, sent):
if self.with_label:
print self.dep_template % (self.tikz_dep_opt, self.tikz_deptxt_opt,
sent.to_deptext(), wrap_depedges_with_label(sent))
else:
print self.dep_template % (self.tikz_dep_opt, self.tikz_deptxt_opt,
sent.to_deptext(), wrap_depedges(sent))
def diff_depedges(self, gold, predict):
res = []
for correct, pre in gold.zip(predict):
if correct == pre:
res.append(wrap_depedge(*pre))
else:
res.append(wrap_depedge(*correct, opt='edge style={%s}' % correct_edge_color))
res.append(wrap_depedge(*pre, opt='edge style={%s}' % wrong_edge_color))
return '\n'.join(res)
def print_diff_dep(self, gold, predict):
print '''\\begin{dependency}[%s]
\\begin{deptext}[%s]
%s
\end{deptext}
%s
\end{dependency}''' % (self.tikz_dep_opt, self.tikz_deptxt_opt,
gold.to_deptext(), self.diff_depedges(gold, predict))
def parse_options():
parser = optparse.OptionParser(usage='%prog [options] data')
parser.add_option('--doc-option', dest='doc_opt', default='standalone',
help='the options of documentclass')
parser.add_option('--dep-option', dest='dep_opt', default='theme = simple',
help='the option of the dependency environment')
parser.add_option('--deptxt-option', dest='deptxt_opt',
default='column sep=.7em,ampersand replacement=%s' % matrix_separator,
help='the option of the deptext environment')
parser.add_option('--reverse-edge', dest='reverse_edge', action="store_true", default=False,
help='Flag to reverse the direction of edges [default: %default]')
parser.add_option('--with-label', dest='with_label', action="store_true", default=False,
help='Flag to enable label [default: %default]')
parser.add_option('--diff', dest='gold', default=None,
help='Diff mode. You need to specify a gold file annotated by human.')
(options, unused_args) = parser.parse_args()
if options.reverse_edge:
options.dep_opt += ',edge style={<-}'
return (options, unused_args)
def diff(opts, args, tex_formatter):
gold = open_conll(opts.gold)
predict = open_conll(args[0])
for g, p in zip(gold, predict):
print tex_formatter.latex_header()
tex_formatter.print_diff_dep(g, p)
print tex_formatter.latex_footer()
print
def main():
opts, unused_args = parse_options()
tex_formatter = LaTeXFormatter(opts.doc_opt, opts.dep_opt, opts.deptxt_opt, opts.with_label)
if opts.gold:
diff(opts, unused_args, tex_formatter)
else:
if len(unused_args) == 0:
sents = read(sys.stdin)
if len(unused_args) >= 1:
sents = open_conll(unused_args[0])
for i, s in enumerate(sents):
if i > 0: print
print tex_formatter.latex_header()
tex_formatter.print_tikz_dep(s)
print tex_formatter.latex_footer()
if __name__ == '__main__':
main()