Skip to content

Commit c7138b3

Browse files
committed
Some minor optimizations in conll.py's doc reading
1 parent b9594d4 commit c7138b3

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

‎stanza/utils/conll.py‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
class CoNLLError(ValueError):
1717
pass
1818

19+
# the field -> column mapping never changes, so unpack it once rather than
20+
# calling .items() on every token in the corpus
21+
FIELD_ITEMS = tuple(FIELD_TO_IDX.items())
22+
TEXT_IDX = FIELD_TO_IDX[TEXT]
23+
LEMMA_IDX = FIELD_TO_IDX[LEMMA]
24+
1925
# MISC annotations which describe whitespace. These are consumed when
2026
# reconstructing the text of a document, then dropped from the MISC field
2127
# so that the Document can regenerate them from spaces_before / spaces_after
@@ -130,17 +136,19 @@ def load_conll(f, ignore_gapping=True, keep_line_numbers=False):
130136
# f is open() or io.StringIO()
131137
doc, sent = [], []
132138
doc_comments, sent_comments = [], []
139+
append_token = sent.append
133140
for line_idx, line in enumerate(f):
134141
# leave whitespace such as NBSP, in case it is meaningful in the conll-u doc
135142
line = line.lstrip().rstrip(' \n\r\t')
136-
if len(line) == 0:
137-
if len(sent) > 0:
143+
if not line:
144+
if sent:
138145
doc.append(sent)
139146
sent = []
147+
append_token = sent.append
140148
doc_comments.append(sent_comments)
141149
sent_comments = []
142150
else:
143-
if line.startswith('#'): # read comment line
151+
if line[0] == '#': # read comment line
144152
sent_comments.append(line)
145153
continue
146154
array = line.split('\t')
@@ -153,7 +161,7 @@ def load_conll(f, ignore_gapping=True, keep_line_numbers=False):
153161
array[-1] = "%s=%d" % (LINE_NUMBER, line_idx)
154162
else:
155163
array[-1] = "%s|%s=%d" % (array[-1], LINE_NUMBER, line_idx)
156-
sent += [array]
164+
append_token(array)
157165
if len(sent) > 0:
158166
doc.append(sent)
159167
doc_comments.append(sent_comments)
@@ -175,14 +183,20 @@ def convert_conll(doc_conll):
175183
token_dict = CoNLL.convert_conll_token(token_conll)
176184
except ValueError as e:
177185
raise CoNLLError("Could not process sentence %d token %d:\n%s\n%s" % (sent_idx, token_idx, token_conll, str(e))) from e
178-
if '.' in token_dict[ID]:
179-
token_dict[ID] = tuple(int(x) for x in token_dict[ID].split(".", maxsplit=1))
186+
token_id = token_dict[ID]
187+
if '.' in token_id:
188+
token_dict[ID] = tuple(int(x) for x in token_id.split(".", maxsplit=1))
180189
sent_empty.append(token_dict)
181190
else:
182191
try:
183-
token_dict[ID] = tuple(int(x) for x in token_dict[ID].split("-", maxsplit=1))
192+
# the overwhelming majority of ids are a plain integer,
193+
# which does not need a split or a generator to convert
194+
if '-' in token_id:
195+
token_dict[ID] = tuple(int(x) for x in token_id.split("-", maxsplit=1))
196+
else:
197+
token_dict[ID] = (int(token_id),)
184198
except ValueError as e:
185-
raise CoNLLError("Could not process ID %s at sent_idx %d, token_idx %d\nEntire token dict:\n%s" % (token_dict[ID], sent_idx, token_idx, token_dict)) from e
199+
raise CoNLLError("Could not process ID %s at sent_idx %d, token_idx %d\nEntire token dict:\n%s" % (token_id, sent_idx, token_idx, token_dict)) from e
186200
sent_dict.append(token_dict)
187201
doc_dict.append(sent_dict)
188202
doc_empty.append(sent_empty)
@@ -212,7 +226,7 @@ def convert_conll_token(token_conll):
212226
Output: a dictionary that maps from field name to value.
213227
"""
214228
token_dict = {}
215-
for field, field_idx in FIELD_TO_IDX.items():
229+
for field, field_idx in FIELD_ITEMS:
216230
value = token_conll[field_idx]
217231
if value == '' and field is FEATS:
218232
continue
@@ -222,9 +236,9 @@ def convert_conll_token(token_conll):
222236
else:
223237
token_dict[field] = value
224238
# special case if text is '_'
225-
if token_conll[FIELD_TO_IDX[TEXT]] == '_':
226-
token_dict[TEXT] = token_conll[FIELD_TO_IDX[TEXT]]
227-
token_dict[LEMMA] = token_conll[FIELD_TO_IDX[LEMMA]]
239+
if token_conll[TEXT_IDX] == '_':
240+
token_dict[TEXT] = token_conll[TEXT_IDX]
241+
token_dict[LEMMA] = token_conll[LEMMA_IDX]
228242
return token_dict
229243

230244
@staticmethod

0 commit comments

Comments
 (0)