@@ -25,19 +25,15 @@ def is_root_reachable_by_deps(self, node, parents_to_ignore=None):
25
25
proc_node , path = stack .pop ()
26
26
# root is reachable
27
27
if proc_node == node .root :
28
- break
28
+ return True
29
29
# path forms a cycle, the root cannot be reached through this branch
30
- if proc_node in path :
31
- continue
32
- for dep in proc_node .deps :
33
- # the root cannot be reached through ignored nodes
34
- if dep ['parent' ] in parents_to_ignore :
35
- continue
36
- # process the parent recursively
37
- stack .append ((dep ['parent' ], path + [proc_node ]))
38
- else :
39
- return False
40
- return True
30
+ if proc_node not in path :
31
+ for dep in proc_node .deps :
32
+ # the root cannot be reached through ignored nodes
33
+ if dep ['parent' ] not in parents_to_ignore :
34
+ # process the parent recursively
35
+ stack .append ((dep ['parent' ], path + [proc_node ]))
36
+ return False
41
37
42
38
def _deps_ignore_nodes (self , node , parents_to_ignore ):
43
39
""" Retrieve deps from the node, recursively ignoring specified parents.
@@ -46,18 +42,16 @@ def _deps_ignore_nodes(self, node, parents_to_ignore):
46
42
stack = [(node , [])]
47
43
while stack :
48
44
proc_node , skipped_nodes = stack .pop ()
49
- # if there is a cycle of skipped nodes, ground the subtree to the root
50
- if proc_node in skipped_nodes :
51
- newdeps .append ({'parent' : node .root , 'deprel' : 'root' })
52
- continue
53
- for dep in proc_node .deps :
54
- # keep deps with a parent that shouldn't be ignored
55
- if not dep ['parent' ] in parents_to_ignore :
56
- newdeps .append (dep )
57
- continue
58
- # process the ignored parent recursively
59
- stack .append ((dep ['parent' ], skipped_nodes + [proc_node ]))
60
- return newdeps
45
+ if proc_node not in skipped_nodes :
46
+ for dep in proc_node .deps :
47
+ if dep ['parent' ] in parents_to_ignore :
48
+ # process the ignored parent recursively
49
+ stack .append ((dep ['parent' ], skipped_nodes + [proc_node ]))
50
+ else :
51
+ # keep deps with a parent that shouldn't be ignored
52
+ newdeps .append (dep )
53
+ # If no newdeps were found (because of a cycle), return the root.
54
+ return newdeps if newdeps else [{'parent' : node .root , 'deprel' : 'root' }]
61
55
62
56
def process_document (self , doc ):
63
57
# This block should work both with coreference loaded (deserialized) and not.
0 commit comments