def to_nltk_tree_general(node, attr_list=("dep_", "pos_"), level=99999):
"""Tranforms a Spacy dependency tree into an NLTK tree, with certain spacy tree node attributes serving
as parts of the NLTK tree node label content for uniqueness.
Args:
node: The starting node from the tree in which the transformation will occur.
attr_list: Which attributes from the Spacy nodes will be included in the NLTK node label.
level: The maximum depth of the tree.
Returns:
A NLTK Tree (nltk.tree)
"""
# transforms attributes in a node representation
value_list = [getattr(node, attr) for attr in attr_list]
node_representation = "/".join(value_list)
if level == 0:
return node_representation
if node.n_lefts + node.n_rights > 0:
return Tree(node_representation, [to_nltk_tree_general(child, attr_list, level-1) for child in node.children])
else:
return node_representation
评论列表
文章目录