从python树中提取父节点和子节点 [英] Extract parent and child node from python tree

查看:683
本文介绍了从python树中提取父节点和子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nltk的Tree数据结构.下面是示例nltk.Tree.

I am using nltk's Tree data structure.Below is the sample nltk.Tree.

(S
  (S
    (ADVP (RB recently))
    (NP (NN someone))
    (VP
      (VBD mentioned)
      (NP (DT the) (NN word) (NN malaria))
      (PP (TO to) (NP (PRP me)))))
  (, ,)
  (CC and)
  (IN so)
  (S
    (NP
      (NP (CD one) (JJ whole) (NN flood))
      (PP (IN of) (NP (NNS memories))))
    (VP (VBD came) (S (VP (VBG pouring) (ADVP (RB back))))))
  (. .))

我不知道nltk.Tree数据结构.我想为每个叶节点提取父节点和超级父节点,例如我想要的是最近"(ADVP,RB),某人"的是(NP,NN).这是我想要的最终结果.早期的答案是使用eval()函数来实现的,这是我想要避免的.

I am not aware of nltk.Tree datastructure. I want to extract the parent and the super parent node for every leaf node e.g. for 'recently' I want (ADVP, RB), and for 'someone' it is (NP, NN)This is the final outcome i want.Earlier answer used eval() function to do so which i want to avoid.

[('ADVP', 'RB'), ('NP', 'NN'), ('VP', 'VBD'), ('NP', 'DT'), ('NP', 'NN'), ('NP', 'NN'), ('PP', 'TO'), ('NP', 'PRP'), ('S', 'CC'), ('S', 'IN'), ('NP', 'CD'), ('NP', 'JJ'), ('NP', 'NN'), ('PP', 'IN'), ('NP', 'NNS'), ('VP', 'VBD'), ('VP', 'VBG'), ('ADVP', 'RB')]

推荐答案

相同的Python代码,无需使用eval函数和nltk树数据结构

Python code for the same without using eval function and using nltk tree datastructure

sentences = " (S
  (S
(ADVP (RB recently))
(NP (NN someone))
(VP
  (VBD mentioned)
  (NP (DT the) (NN word) (NN malaria))
  (PP (TO to) (NP (PRP me)))))
  (, ,)
  (CC and)
  (IN so)
  (S
    (NP
      (NP (CD one) (JJ whole) (NN flood))
      (PP (IN of) (NP (NNS memories))))
    (VP (VBD came) (S (VP (VBG pouring) (ADVP (RB back))))))
  (. .))"

print list(tails(sentences))


def tails(items, path=()):
for child in items:
    if type(child) is nltk.Tree:
        if child.label() in {".", ","}:  # ignore punctuation
            continue
        for result in tails(child, path + (child.label(),)):
            yield result
    else:
        yield path[-2:]

这篇关于从python树中提取父节点和子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆