-
Notifications
You must be signed in to change notification settings - Fork 5
/
to_treebank.py
192 lines (160 loc) · 5.36 KB
/
to_treebank.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
# coding=utf-8
""" Process a Ancora style xml into CONLL 2011 format
"""
from __future__ import unicode_literals
__author__ = 'Josu Bermudez <josu.bermudez@deusto.es>'
from xml import sax
import sys
import re
FALSE = False
TRUE = "yes"
FILL = "-"
ARTICLE = "article"
SENTENCE = "sentence"
POS = "pos"
FORM = "wd"
LEMMA = "lem"
PARSE_ROW = 5
ELLIPTIC = "elliptic"
ELLIPTIC_POS = "e"
ELLIPTIC_FORM = ""
ELLIPTIC_LEMMA = ""
NAMED_ENTITY = "ne"
NAMED_ENTITY_ROW = 11
NO_NAMED_ENTITY = False
ENTITY = "entity"
COREF_ROW = 13
NO_ENTITY = False
COREF_TYPE = "coreftype"
VALID_COREF_TYPE = ("", "ident")
MISSING = "missing"
CORPUS = "corpus"
class AncoraToConll(sax.ContentHandler):
""" Process a Ancora type XML into a Conll format.
"""
def __init__(self, name="file"):
sax.ContentHandler.__init__(self)
self.name = name
self._conll = []
self._part = 0
self._sentence = 0
self._token = 0
self._tree = ""
self._last_is_leaf = False
self._ne_stack = []
self._ne = ""
self._coref_stack = []
self._coref = ""
self._entity_list = []
self.all_tree = []
self._skip = 0
def get_conll(self):
"""Generate a string of the conll result of the document.
"""
return " ".join(((unicode(element) for line in self._conll for element in line))).replace("\n ", "\n")
def startElement(self, name, attrs):
""" SAX parser API: Called each element start.
:param name: The tag name
:param attrs: The attributes of the element
"""
if name in ("spec",):
return None
if attrs.get(MISSING, False) or attrs.get(ELLIPTIC, FALSE):
self._elliptic(name, attrs)
return None
try:
if name == CORPUS:
return None
elif name == ARTICLE:
self._start_article(attrs)
elif name == SENTENCE:
self._start_sentence(attrs)
else:
if FORM in attrs:
self._last_is_leaf = True
self._start_leaf(name, attrs)
else:
self._start_constituent(name, attrs)
except Exception as ex:
print "ERROR", self._sentence, self._token, ex
def endElement(self, name):
""" SAX parser API: Called each element end.
:param name: The tag name
"""
if self._skip:
self._skip -= 1
return None
elif name in ("spec",):
return None
try:
if name == CORPUS:
return None
elif name == ARTICLE:
self._end_article()
elif name == SENTENCE:
self._end_sentence()
else:
if self._last_is_leaf:
self._last_is_leaf = False
self._end_leaf()
else:
self._end_constituent()
except Exception as ex:
print "ERROR", self._sentence, self._token, ex
def _start_article(self, attrs):
"""Mark the start of a part and reset inter sentence trackers and counters.
:param attrs: The attributes of the element
"""
pass
def _start_sentence(self, attrs):
"""Reset all intra sentence trackers and counters,
:param attrs: The attributes of the element.
"""
self.all_tree.append("(TOP")
def _start_constituent(self, name, attrs):
""" Create the marks of the constituent start token for NE and entities. Also,
create the tracking for NE and entities.
:param name: The tag name
:param attrs: The attributes of the element
"""
self.all_tree[-1] += " (" + name.upper()
def _start_leaf(self, name, attrs):
""" Create the token registry in the document
:param name: The tag name
:param attrs: The attributes of the element
"""
form = attrs.get(FORM).replace("(", "-LRB-")\
.replace(")", "-RRB-").replace("{", "-LCB-").replace("}", "-RCB-").replace("[", "-LSB-").replace("]", "-RSB-")
pos = (attrs.get(POS, False) or name).upper()
self.all_tree[-1] += " ({0} {1})" .format(pos, form)
def _end_leaf(self):
"""Increment the token counter.
"""
pass
def _end_constituent(self):
"""Set the constituent last token attributes:
NE closing parenthesis and entity closing values.
"""
self.all_tree[-1] += ")"
def _end_sentence(self):
"""Close the sentence with a sentence mark, increment the counter.
"""
self.all_tree[-1] += ")"
if self.all_tree[-1] == "(TOP)":
self.all_tree.pop(-1)
def _end_article(self):
"""Close the article and increment the part counter
"""
pass
def _elliptic(self, name, attrs):
""" A elliptic element is encountered. Create a artificial token for it.
:param name: The tag name
:param attrs: The attributes of the element
"""
self._skip = 1
#self.all_tree[-1] = re.sub("\((\s|\w)+$", "", self.all_tree[-1])
if __name__ == "__main__":
input_stream = sys.stdin
content_handler = AncoraToConll()
sax.parse(input_stream, content_handler)
sys.stdout.write("\n".join(content_handler.all_tree).encode("utf-8"))