Skip to content

Commit c28dfd3

Browse files
committed
refactor corefud.Delete
It may be subjective, but I find this easier to understand the code.
1 parent ea33c78 commit c28dfd3

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

udapi/block/corefud/delete.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ def is_root_reachable_by_deps(self, node, parents_to_ignore=None):
2525
proc_node, path = stack.pop()
2626
# root is reachable
2727
if proc_node == node.root:
28-
break
28+
return True
2929
# 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
4137

4238
def _deps_ignore_nodes(self, node, parents_to_ignore):
4339
""" Retrieve deps from the node, recursively ignoring specified parents.
@@ -46,18 +42,16 @@ def _deps_ignore_nodes(self, node, parents_to_ignore):
4642
stack = [(node, [])]
4743
while stack:
4844
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'}]
6155

6256
def process_document(self, doc):
6357
# This block should work both with coreference loaded (deserialized) and not.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy