如何导航nltk.tree.Tree?

编程入门 行业动态 更新时间:2024-10-26 00:32:25
本文介绍了如何导航nltk.tree.Tree?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下方法对句子进行了分块:

I've chunked a sentence using:

grammar = ''' NP: {<DT>*(<NN.*>|<JJ.*>)*<NN.*>} NVN: {<NP><VB.*><NP>} ''' chunker = nltk.chunk.RegexpParser(grammar) tree = chunker.parse(tagged) print tree

结果如下:

(S (NVN (NP The_Pigs/NNS) are/VBP (NP a/DT Bristol-based/JJ punk/NN rock/NN band/NN)) that/WDT formed/VBN in/IN 1977/CD ./.)

但是现在我被困在试图弄清楚如何进行导航.我希望能够找到NVN子树,并访问左侧名词短语("The_Pigs"),动词("are")和右侧名词短语(基于Bristol的朋克摇滚乐队") .我该怎么办?

But now I'm stuck trying to figure out how to navigate that. I want to be able to find the NVN subtree, and access the left-side noun phrase ("The_Pigs"), the verb ("are") and the right-side noun phrase ("a Bristol-based punk rock band"). How do I do that?

推荐答案

尝试:

ROOT = 'ROOT' tree = ... def getNodes(parent): for node in parent: if type(node) is nltk.Tree: if node.label() == ROOT: print "======== Sentence =========" print "Sentence:", " ".join(node.leaves()) else: print "Label:", node.label() print "Leaves:", node.leaves() getNodes(node) else: print "Word:", node getNodes(tree)

更多推荐

如何导航nltk.tree.Tree?

本文发布于:2023-11-30 05:19:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:nltk   tree   Tree

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!