gpt4 book ai didi

tree - 如何导航 nltk.tree.Tree?

转载 作者:行者123 更新时间:2023-12-03 11:17:18 26 4
gpt4 key购买 nike

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

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”)和右侧名词短语(“a Bristol-based punk rock band”) .我怎么做?

最佳答案

尝试:

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)

关于tree - 如何导航 nltk.tree.Tree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com