Skip to content

Commit bd89be0

Browse files
committed
bugfix in read.Conllu fix_cycles=1
plus fixing also HEAD index out of range
1 parent e6db7b3 commit bd89be0

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

udapi/block/read/conllu.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, strict=False, empty_parent='warn', fix_cycles=False, **kwargs
2727
strict: raise an exception if errors found (default=False, i.e. a robust mode)
2828
empty_parent: What to do if HEAD is _? Default=warn: issue a warning and attach to the root
2929
or if strict=1 issue an exception. With `empty_parent=ignore` no warning is issued.
30-
fix_cycles: fix cycles by attaching a node in the cycle to the root
30+
fix_cycles: fix cycles by attaching a node in the cycle to the root; fix also HEAD index out of range
3131
"""
3232
super().__init__(**kwargs)
3333
self.strict = strict
@@ -193,12 +193,15 @@ def read_tree_from_lines(self, lines):
193193
try:
194194
parent = nodes[parents[node_ord]]
195195
except IndexError:
196-
raise ValueError("Node %s HEAD is out of range (%d)" % (node, parents[node_ord]))
196+
if self.fix_cycles:
197+
logging.warning(f"Ignoring out-of-range HEAD (attaching to the root instead): {node} HEAD={parents[node_ord]}")
198+
parent = root
199+
else:
200+
raise ValueError("Node %s HEAD is out of range (%d)" % (node, parents[node_ord]))
197201
if node is parent:
198202
if self.fix_cycles:
199-
logging.warning("Ignoring a cycle (attaching to the root instead):\n%s", node)
200-
node._parent = root
201-
root._children.append(node)
203+
logging.warning("Ignoring a self-cycle (attaching to the root instead):\n%s", node)
204+
parent = root
202205
else:
203206
raise ValueError(f"Detected a cycle: {node} attached to itself")
204207
elif node._children:

0 commit comments

Comments
 (0)