def traverse_tree(tree, pro):
""" Traverse a tree in a left-to-right, breadth-first manner,
proposing any NP encountered as an antecedent. Returns the
tree and the position of the first possible antecedent.
Args:
tree: the tree being searched
pro: the pronoun being resolved (string)
"""
# Initialize a queue and enqueue the root of the tree
queue = Queue.Queue()
queue.put(tree)
while not queue.empty():
node = queue.get()
# if the node is an NP, return it as a potential antecedent
if "NP" in node.label() and match(tree, get_pos(tree,node), pro):
return tree, get_pos(tree, node)
for child in node:
if isinstance(child, nltk.Tree):
queue.put(child)
# if no antecedent is found, return None
return None, None
评论列表
文章目录