Skip to content

Commit a4a2770

Browse files
committed
A couple optimizations - checking against None for deps saves on multiple steps in the non-enhanced graph case, and checking for any Space in misc optimizes the processing of Spaces in words which don't have Space (which is the most common case by far in most treebanks)
1 parent 7d5725e commit a4a2770

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

‎stanza/models/common/doc.py‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,15 @@ def consolidate_whitespace(self):
12501250
"""
12511251
Remove whitespace misc annotations from the Words and mark the whitespace on the Tokens
12521252
"""
1253+
# most tokens have no whitespace annotation at all, and searching the
1254+
# misc strings for one is much cheaper than splitting each of them
1255+
# into pieces. every prefix this method looks for - SpaceAfter,
1256+
# SpacesAfter, SpacesBefore - begins with "Space", and every test it
1257+
# makes on them is case sensitive, so this skips exactly the tokens
1258+
# which would have fallen through all of those tests anyway
1259+
if not (self._misc and "Space" in self._misc) and not any(word._misc and "Space" in word._misc for word in self.words):
1260+
return
1261+
12531262
found_after = False
12541263
found_before = False
12551264
num_words = len(self.words)
@@ -1498,9 +1507,33 @@ def __init__(self, sentence, word_entry):
14981507
if self._misc is not None:
14991508
init_from_misc(self)
15001509

1501-
# use the setter, which will go up to the sentence and set the
1502-
# dependencies on that graph
1503-
self.deps = word_entry.get(DEPS, None)
1510+
# a Word being constructed cannot have any existing in-edges to clear,
1511+
# so we skip the general setter and go straight to adding the edges
1512+
deps = word_entry.get(DEPS, None)
1513+
if deps is not None:
1514+
self._init_deps(deps)
1515+
1516+
def _init_deps(self, value):
1517+
"""Add the enhanced dependencies of a freshly built Word
1518+
1519+
The deps setter has to clear the Word's existing parents first, which
1520+
costs a has_node and an in_edges lookup per Word. Neither can find
1521+
anything during construction, so this skips them
1522+
"""
1523+
graph = self._sent._enhanced_dependencies
1524+
if graph is None:
1525+
graph = nx.MultiDiGraph()
1526+
self._sent._enhanced_dependencies = graph
1527+
1528+
if isinstance(value, str):
1529+
value = value.split("|")
1530+
if all(isinstance(x, str) for x in value):
1531+
value = [x.split(":", maxsplit=1) for x in value]
1532+
for parent, dep in value:
1533+
parent = tuple(map(int, parent.split(".", maxsplit=1)))
1534+
if len(parent) == 1:
1535+
parent = parent[0]
1536+
graph.add_edge(parent, self.id, dep)
15041537

15051538
@property
15061539
def manual_expansion(self):

0 commit comments

Comments
 (0)