/
spacy_to_naf.py
323 lines (268 loc) · 11.7 KB
/
spacy_to_naf.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
316
317
318
319
320
321
322
323
# coding: utf-8
import re
from lxml import etree
from collections import namedtuple
from datetime import datetime
# Define Entity object:
Entity = namedtuple('Entity',['start', 'end', 'entity_type'])
WfElement = namedtuple('WfElement',['sent', 'wid', 'length', 'wordform', 'offset'])
TermElement = namedtuple('TermElement', ['tid', 'lemma', 'pos', 'morphofeat', 'targets', 'text'])
EntityElement = namedtuple('EntityElement', ['eid', 'entity_type', 'targets', 'text'])
DependencyRelation = namedtuple('DependencyRelation', ['from_term', 'to_term', 'rfunc', 'from_orth', 'to_orth'])
ChunkElement = namedtuple('ChunkElement', ['cid', 'head', 'phrase', 'text', 'targets'])
# Only allow legal strings in XML:
# http://stackoverflow.com/a/25920392/2899924
illegal_pattern = re.compile('[^\u0020-\uD7FF\u0009\u000A\u000D\uE000-\uFFFD\u10000-\u10FFFF]+')
def remove_illegal_chars(text):
return re.sub(illegal_pattern, '', text)
def normalize_token_orth(orth):
if '\n' in orth:
return 'NEWLINE'
else:
return remove_illegal_chars(orth)
def get_entity_type(span):
"Function to get the entity type of an entity span."
ent_type_set = {tok.ent_type_ for tok in span if tok.ent_type_ != ''}
return ent_type_set.pop()
def entities(doc):
"Generator that returns Entity objects for a given document."
for ent in doc.ents:
yield Entity(start = ent.start,
end = ent.end -1,
entity_type = get_entity_type(ent))
def add_wf_element(text_layer, wf_data):
"""
Function that adds a wf element to the text layer.
"""
wf_el = etree.SubElement(text_layer, "wf")
wf_el.set("sent", wf_data.sent)
wf_el.set("id", wf_data.wid)
wf_el.set("length", wf_data.length)
wf_el.set("offset", wf_data.offset)
wf_el.text = wf_data.wordform
def add_term_element(terms_layer, term_data):
"""
Function that adds a term element to the text layer.
"""
term_el = etree.SubElement(terms_layer, "term")
term_el.set("id", term_data.tid)
term_el.set("lemma", term_data.lemma)
term_el.set("pos", term_data.pos)
term_el.set("morphofeat", term_data.morphofeat)
span = etree.SubElement(term_el, "span")
#span.append(etree.Comment(' '.join(term_data.text)))
for target in term_data.targets:
target_el = etree.SubElement(span, "target")
target_el.set("id", target)
def add_entity_element(entities_layer, entity_data):
"""
Function that adds an entity element to the entity layer.
"""
entity_el = etree.SubElement(entities_layer, "entity")
entity_el.set("id", entity_data.eid)
entity_el.set("type", entity_data.entity_type)
references_el = etree.SubElement(entity_el, "references")
span = etree.SubElement(references_el, "span")
#span.append(etree.Comment(' '.join(entity_data.text)))
for target in entity_data.targets:
target_el = etree.SubElement(span, "target")
target_el.set("id", target)
def chunks_for_doc(doc):
"""
Generator function that yields NP and PP chunks with their phrase label.
"""
for chunk in doc.noun_chunks:
if chunk.root.head.pos_ == 'ADP':
span = doc[chunk.start-1:chunk.end]
yield (span, 'PP')
yield (chunk, 'NP')
def chunk_tuples_for_doc(doc):
"""
Generator function that takes a doc and yields ChunkElement tuples.
"""
for i, (chunk, phrase) in enumerate(chunks_for_doc(doc)):
yield ChunkElement(cid = 'c' + str(i),
head = 't' + str(chunk.root.i),
phrase = phrase,
text = remove_illegal_chars(chunk.orth_.replace('\n',' ')),
targets = ['t' + str(tok.i) for tok in chunk])
def add_chunk_element(chunks_layer, chunk_data):
"""
Function that adds a chunk element to the chunks layer.
"""
chunk_el = etree.SubElement(chunks_layer, "chunk")
chunk_el.set("id", chunk_data.cid)
chunk_el.set("head", chunk_data.head)
chunk_el.set("phrase", chunk_data.phrase)
span = etree.SubElement(chunk_el, "span")
#span.append(etree.Comment(chunk_data.text))
for target in chunk_data.targets:
target_el = etree.SubElement(span, "target")
target_el.set("id", target)
def add_dependency_element(dependency_layer, dep_data):
"""
Function that adds dependency elements to the deps layer.
"""
#comment = dep_data.rfunc + '(' + dep_data.from_orth + ',' + dep_data.to_orth + ')'
#dependency_layer.append(etree.Comment(comment))
dep_el = etree.SubElement(dependency_layer, "dep")
dep_el.set("from", dep_data.from_term)
dep_el.set("to", dep_data.to_term)
dep_el.set("rfunc", dep_data.rfunc)
def dependencies_to_add(token):
"""
Walk up the tree, creating a DependencyRelation for each label.
The relation is then passed to the
"""
deps = []
while token.head is not token:
dep_data = DependencyRelation(from_term = 't' + str(token.head.i),
to_term = 't' + str(token.i),
rfunc = token.dep_,
from_orth = normalize_token_orth(token.head.orth_),
to_orth = normalize_token_orth(token.orth_))
deps.append(dep_data)
token = token.head
return deps
def naf_from_doc(doc, time=None, language='en'):
"""
Function that takes a document and returns an ElementTree
object that corresponds to the root of the NAF structure.
"""
# NAF:
# ---------------------
# Create NAF root.
root = etree.Element("NAF")
root.set('{http://www.w3.org/XML/1998/namespace}lang',language)
root.set('version', "v3.naf")
# Create text and terms layers.
naf_header = etree.SubElement(root, "nafHeader")
ling_proc = etree.SubElement(naf_header, "linguisticProcessors")
ling_proc.set("layer", "text")
lp = etree.SubElement(ling_proc, "lp")
lp.set("name", "SpaCy")
if time:
lp.set("timestamp", time)
ling_proc = etree.SubElement(naf_header, "linguisticProcessors")
lp = etree.SubElement(ling_proc, "lp")
lp.set("name", "SpaCy")
if time:
lp.set("timestamp", time)
ling_proc.set("layer", "terms")
text_layer = etree.SubElement(root, "text")
terms_layer = etree.SubElement(root, "terms")
entities_layer = etree.SubElement(root, "entities")
dependency_layer = etree.SubElement(root, "deps")
chunks_layer = etree.SubElement(root, "chunks")
# Initialize variables:
# ---------------------
# - Use a generator for entity awareness.
entity_gen = entities(doc)
try:
next_entity = next(entity_gen)
except StopIteration:
next_entity = Entity(start=None, end=None, entity_type=None)
# - Bookkeeping variables.
current_term = [] # Use a list for multiword expressions.
current_term_orth = [] # id.
current_entity = [] # Use a list for multiword entities.
current_entity_orth = [] # id.
current_token = 0 # Keep track of the token number.
term_number = 0 # Keep track of the term number.
entity_number = 0 # Keep track of the entity number.
parsing_entity = False # State change: are we working on a term or not?
for sentence_number, sentence in enumerate(doc.sents, start = 1):
dependencies_for_sentence = []
for token_number, token in enumerate(sentence, start = current_token):
# Do we need a state change?
if token_number == next_entity.start:
parsing_entity = True
wid = 'w' + str(token_number)
tid = 't' + str(term_number)
current_term.append(wid)
current_term_orth.append(normalize_token_orth(token.orth_))
if parsing_entity:
current_entity.append(tid)
current_entity_orth.append(normalize_token_orth(token.orth_))
# Create WfElement data:
wf_data = WfElement(sent = str(sentence_number),
wid = wid,
length = str(len(token.text)),
wordform = normalize_token_orth(token.text),
offset = str(token.idx))
# Create TermElement data:
term_data = TermElement(tid = tid,
lemma = remove_illegal_chars(token.lemma_),
pos = token.pos_,
morphofeat = token.tag_,
targets = current_term,
text = current_term_orth)
add_wf_element(text_layer, wf_data)
add_term_element(terms_layer, term_data)
# Move to the next term
term_number += 1
current_term = []
current_term_orth = []
if parsing_entity and token_number == next_entity.end:
# Create new entity ID.
eid = 'e' + str(entity_number)
# Create Entity data:
entity_data = EntityElement(eid = eid,
entity_type = next_entity.entity_type,
targets = current_entity,
text = current_entity_orth)
# Add data to XML:
add_entity_element(entities_layer, entity_data)
# Move to the next entity:
entity_number += 1
current_entity = []
current_entity_orth = []
# Move to the next entity
parsing_entity = False
try:
next_entity = next(entity_gen)
except StopIteration:
# No more entities...
next_entity = Entity(start=None, end=None, entity_type=None)
# Add dependencies for the current token to the list.
for dep_data in dependencies_to_add(token):
if not dep_data in dependencies_for_sentence:
dependencies_for_sentence.append(dep_data)
# At the end of the sentence, add all the dependencies to the XML structure.
for dep_data in dependencies_for_sentence:
add_dependency_element(dependency_layer, dep_data)
current_token = token_number + 1
# Add chunk layer after adding all other layers.
for chunk_data in chunk_tuples_for_doc(doc):
add_chunk_element(chunks_layer, chunk_data)
return root
def current_time():
"Function that returns the current time (UTC)"
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SUTC")
def text_to_NAF(text, nlp, language='en'):
"""
Function that takes a text and returns an xml object containing the NAF.
"""
doc = nlp(text)
time = current_time()
return naf_from_doc(doc, time=time, language=language)
def NAF_to_string(NAF, byte=False):
"""
Function that takes an XML object containing NAF, and returns it as a string.
If byte is True, then the output is a bytestring.
"""
xml_string = etree.tostring(NAF, pretty_print=True, xml_declaration=True, encoding='utf-8')
if byte:
return xml_string
else:
return xml_string.decode('utf-8')
# Command line functionality: given name of a file, process the file contents and
# print the NAF to stdout.
if __name__ == '__main__':
import sys
from spacy.en import English
nlp = English()
with open(sys.argv[1]) as f:
text = f.read()
NAF = text_to_NAF(text, nlp)
print(NAF_to_string(NAF))