Skip to content

Commit 16fc5e5

Browse files
committed
root.flatten()
1 parent c651b04 commit 16fc5e5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

udapi/core/root.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,17 @@ def steal_nodes(self, nodes):
300300
self.create_multiword_token(words=words, form=mwt.form, misc=mwt.misc)
301301
self._descendants += nodes
302302
# pylint: enable=protected-access
303+
304+
def flatten(self, deprel='root'):
305+
"""Flatten the tree (i.e. attach all nodes to the root) and reset all deprels.
306+
307+
This is equivalent to
308+
for node in root.descendants:
309+
node.parent = root
310+
node.deprel = 'root'
311+
but it is faster.
312+
"""
313+
self._children = self._descendants[:]
314+
for node in self._children:
315+
node._parent = self
316+
node._children.clear()

0 commit comments

Comments
 (0)