我一直在尝试寻找如何使用 spaCy 获取依赖树,但我找不到任何有关如何获取树的信息,只能在如何导航树 https://spacy.io/usage/examples#subtrees.
如果有人想轻松查看 spacy 生成的依赖关系树,一种解决方案是将其转换为nltk.tree.Tree http://www.nltk.org/api/nltk.html#nltk.tree.Tree并使用nltk.tree.Tree.pretty_print http://www.nltk.org/api/nltk.html#nltk.tree.Tree.pretty_print方法。这是一个例子:
import spacy
from nltk import Tree
en_nlp = spacy.load('en')
doc = en_nlp("The quick brown fox jumps over the lazy dog.")
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])
else:
return node.orth_
[to_nltk_tree(sent.root).pretty_print() for sent in doc.sents]
Output:
jumps
________________|____________
| | | | | over
| | | | | |
| | | | | dog
| | | | | ___|____
The quick brown fox . the lazy
Edit:要更改令牌表示,您可以执行以下操作:
def tok_format(tok):
return "_".join([tok.orth_, tok.tag_])
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
return Tree(tok_format(node), [to_nltk_tree(child) for child in node.children])
else:
return tok_format(node)
结果是:
jumps_VBZ
__________________________|___________________
| | | | | over_IN
| | | | | |
| | | | | dog_NN
| | | | | _______|_______
The_DT quick_JJ brown_JJ fox_NN ._. the_DT lazy_JJ
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)