【发布时间】:2017-12-14 12:24:57
【问题描述】:
我想在解析下面的句子时检索子树:
sentence = "All new medications must undergo testing before they can be
prescribed"
parser = stanford.StanfordParser()
tree_parse = parser.raw_parse(sentence)
for i, sub_tree in enumerate(tree_parse[0].subtrees()):
if sub_tree.label() in ["S"]:
sub_list = sub_tree
print(sub_list)
我期望的是单独访问标记为“S”的子树,如下所示:
第一个子树
(S
(NP (DT All) (JJ new) (NNS medications))
(VP
(MD must)
(VP
(VB undergo)
第二个子树
(S
(VP
(VBG testing)
(SBAR
(IN before)
第三个子树
(S
(NP (PRP they))
(VP (MD can) (VP (VB be) (VP (VBN prescribed)))))))))))
但实际输出如下:
(NP (DT All) (JJ new) (NNS medications))
(VP
(MD must)
(VP
(VB undergo)
(S
(VP
(VBG testing)
(SBAR
(IN before)
(S
(NP (PRP they))
(VP (MD can) (VP (VB be) (VP (VBN prescribed))))))))))
How to access the sub tress individually like accessing items in a list?
【问题讨论】:
标签: python parsing nlp nltk stanford-nlp