【问题标题】:how to retrieve subtrees while parsing in nlp在 nlp 中解析时如何检索子树
【发布时间】: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


    【解决方案1】:

    您已经获得了子树:子树包含其根以下的所有内容,因此您显示的输出被正确检索为顶级S 下方的“子树”。然后,您将输出主导“在可以规定之前进行测试”的子树,最后是最底层的S,主导“可以规定”。

    顺便说一句,您可以通过指定filter 直接获取S 子树:

    for sub_tree in tree_parse[0].subtrees(lambda t: t.label() == "S"):
        print(sub_tree)
    

    【讨论】:

      猜你喜欢
      • 2014-04-20
      • 2014-01-25
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多