斯坦福JavaNLP中如何获取父节点?
假设我有这样的一句话:
(NP
(NP (DT A) (JJ single) (NN page))
(PP (IN in)
(NP (DT a) (NN wiki) (NN website))))
在某个时刻我有一个对 (JJ single)
的引用,并且我想获得 NP
节点绑定 单页
。如果我猜对了,NP
是节点的父节点,A
和 page
是它的兄弟节点,并且它没有子节点(?) 。当我尝试使用树的 .parent()
方法时,我总是得到 null。 API 说这是因为实现不知道如何确定父节点。另一个感兴趣的方法是 .ancestor(int height, Tree root)
,但我不知道如何获取节点的根。在这两种情况下,由于解析器知道如何缩进和分组树,它必须知道“父”树,对吧?我怎样才能得到它?谢谢
Suppose I have such chunk of a sentence:
(NP
(NP (DT A) (JJ single) (NN page))
(PP (IN in)
(NP (DT a) (NN wiki) (NN website))))
At a certain moment of time I have a reference to (JJ single)
and I want to get the NP
node binding A single page
. If I get it right, that NP
is the parent of the node, A
and page
are its siblings and it has no children (?). When I try to use the .parent()
method of a tree, I always get null. The API says that's because the implementation doesn't know how to determine the parent node. Another method of interest is .ancestor(int height, Tree root)
, but I don't know how to get the root of the node. In both cases, since the parser knows how to indent and group trees, it must know the "parent" tree, right? How can I get it? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
Tree
本身永远不会返回节点的父节点,因为它是硬编码在Tree
源中的。同时TreeGraphNode
覆盖该方法并且运行良好。将树更改为 TreeGraphNode 非常简单It looks like the
Tree
itself will never return the parent of the node, since it's hardcoded in theTree
sources. MeanwhileTreeGraphNode
overrides that method and works well. Changing a Tree to a TreeGraphNode is as easy as如果保留对树根的引用,则可以使用方法
parent(Tree root)
来获取树中任何节点的父节点。假设树根称为root
,JJ节点称为jjTree
,则jjTree.parent(root)
将返回父 NP 节点。If you keep a reference to the root of the tree, then you can use method
parent(Tree root)
to get the parent of any node in the tree. Assume that the tree root is referred to asroot
, and that the JJ node is referred to asjjTree
, thenjjTree.parent(root)
will return the parent NP node.