从python中提取父节点和子节点

2024-04-19 04:11:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用nltk的树数据结构。下面是样品吗nltk.树. 在

(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.树数据结构。我想提取每个叶节点的父节点和父节点,例如,对于'recently'我想要(ADVP,RB),对于'someone'它是(NP,NN),这是最后的结果想要。早点answer使用eval()函数这样做,我想避免。在

^{pr2}$

Tags: in数据结构节点np样品nnppnltk
1条回答
网友
1楼 · 发布于 2024-04-19 04:11:37

Python代码同样没有使用eval函数和使用nltk树数据结构

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:]

相关问题 更多 >