Python 如何首先使用nltk.util.u进行搜索?

Python 如何首先使用nltk.util.u进行搜索?,python,nlp,nltk,Python,Nlp,Nltk,我尝试先使用Width_搜索(首先)一个特定的叶子词,然后在ParentedTree中搜索某个标签(NP)。如果已经有了一个方法,我真的不想自己实现它。这就是我尝试过的(包括我是如何制作这棵树的,以防我弄糟了): 这给了我一个生成器对象,但我不知道如何使用它来搜索我想要的东西(代词“They”)。我试着做一个简单的“for node in bf:print(node)”,它将字符串中的每一个字母单独打印在一行上,一直重复,直到我不得不关闭窗口 我读过这些文档,在谷歌上搜索了很多次,但我找不到一个

我尝试先使用Width_搜索(首先)一个特定的叶子词,然后在ParentedTree中搜索某个标签(NP)。如果已经有了一个方法,我真的不想自己实现它。这就是我尝试过的(包括我是如何制作这棵树的,以防我弄糟了):

这给了我一个生成器对象,但我不知道如何使用它来搜索我想要的东西(代词“They”)。我试着做一个简单的“for node in bf:print(node)”,它将字符串中的每一个字母单独打印在一行上,一直重复,直到我不得不关闭窗口


我读过这些文档,在谷歌上搜索了很多次,但我找不到一个实际用于搜索的例子。我做错了什么

nltk.util.width_-first方法对作为参数提供的树进行宽度优先遍历。要将其用作搜索机制,您需要检查生成器返回的每个结果中的值

如果您遍历由width_首先返回的生成器的结果,并在遍历的每个步骤输出结果,您可以看到它会遇到树中的每个节点(按BFS顺序),最终还会遇到树的叶节点和角色节点

因此,对于您的情况,您希望使用此生成器,并在每个节点检查一些值,以查看您是否已到达一个节点,该节点带有您在搜索中要查找的符号或叶标记

下面是一个示例句子,它的解析树来自nltk,以及遍历树

祝你好运

>>> sentence
'They capture mice in the cells'
>>> parse
Tree('S', [Tree('NP', [Tree('PRP', ['They'])]), Tree('VP', [Tree('VBP', ['capture']), Tree('NP', [Tree('Nom', [Tree('Nom', [Tree('NNS', ['mice'])]), Tree('PP', [Tree('Prep', ['in']), Tree('NP', [Tree('Det', ['the']), Tree('Nom', [Tree('NNS', ['cells'])])])])])])])])
>>> i = 0
>>> for node in breadth_first(parse):
...     print("*"*10)
...     print(node)
...     print(type(node))
...     if i > 10:
...             break
...     i += 1
...
**********
(S
  (NP (PRP They))
  (VP
    (VBP capture)
    (NP
      (Nom
        (Nom (NNS mice))
        (PP (Prep in) (NP (Det the) (Nom (NNS cells))))))))
<class 'nltk.tree.Tree'>
**********
(NP (PRP They))
<class 'nltk.tree.Tree'>
**********
(VP
  (VBP capture)
  (NP
    (Nom
      (Nom (NNS mice))
      (PP (Prep in) (NP (Det the) (Nom (NNS cells)))))))
<class 'nltk.tree.Tree'>
**********
(PRP They)
<class 'nltk.tree.Tree'>
**********
(VBP capture)
<class 'nltk.tree.Tree'>
**********
(NP
  (Nom
    (Nom (NNS mice))
    (PP (Prep in) (NP (Det the) (Nom (NNS cells))))))
<class 'nltk.tree.Tree'>
**********
They
<class 'str'>
**********
capture
<class 'str'>
**********
(Nom
  (Nom (NNS mice))
  (PP (Prep in) (NP (Det the) (Nom (NNS cells)))))
<class 'nltk.tree.Tree'>
**********
T
<class 'str'>
**********
h
<class 'str'>
**********
e
<class 'str'>
>句子
“他们在细胞中捕捉老鼠”
>>>解析
树('S',[Tree('NP',[Tree('PRP',['Thes'])),树('VP',[Tree('VBP',['capture']),树('NP',[Tree('Nom',[Tree('NNS',['mice'])),树('PP',[Tree('Prep',['in']),树('NP和树('Tree('Det',['the']),树('Nom',[Tree('NNS',['cells']))]))]))]
>>>i=0
>>>对于第一个(解析)中的节点:
...     打印(“*”*10)
...     打印(节点)
...     打印(类型(节点))
...     如果i>10:
...             打破
...     i+=1
...
**********
(S)
(NP(PRP)
(副总裁)
(VBP捕获)
(NP)
(名称
(Nom(NNS小鼠))
(PP(Prep-in)(NP(Det)(Nom(NNS-cells‘‘‘‘‘‘‘‘‘‘‘‘‘)’))
**********
(NP(PRP)
**********
(副总裁)
(VBP捕获)
(NP)
(名称
(Nom(NNS小鼠))
(PP(Prep-in)(NP(Det)(Nom(NNS-cells‘‘‘‘‘‘‘‘‘‘‘‘)’))
**********
(PRP他们)
**********
(VBP捕获)
**********
(NP)
(名称
(Nom(NNS小鼠))
(PP(Prep-in)(NP(Det)(Nom(NNS-cells)(()))))
**********
他们
**********
捕获
**********
(名称
(Nom(NNS小鼠))
(PP(Prep-in)(NP(Det)(Nom(NNS-cells)))))
**********
T
**********
H
**********
E

重复的?我看了看那个问题;我的问题是如何首先使用NLTK方法。不管怎样,这个问题是关于遍历深度优先的。有趣的是,递归仍然存在,没有显式中断吗?如果是这样,那么它就是一个bug=)
>>> sentence
'They capture mice in the cells'
>>> parse
Tree('S', [Tree('NP', [Tree('PRP', ['They'])]), Tree('VP', [Tree('VBP', ['capture']), Tree('NP', [Tree('Nom', [Tree('Nom', [Tree('NNS', ['mice'])]), Tree('PP', [Tree('Prep', ['in']), Tree('NP', [Tree('Det', ['the']), Tree('Nom', [Tree('NNS', ['cells'])])])])])])])])
>>> i = 0
>>> for node in breadth_first(parse):
...     print("*"*10)
...     print(node)
...     print(type(node))
...     if i > 10:
...             break
...     i += 1
...
**********
(S
  (NP (PRP They))
  (VP
    (VBP capture)
    (NP
      (Nom
        (Nom (NNS mice))
        (PP (Prep in) (NP (Det the) (Nom (NNS cells))))))))
<class 'nltk.tree.Tree'>
**********
(NP (PRP They))
<class 'nltk.tree.Tree'>
**********
(VP
  (VBP capture)
  (NP
    (Nom
      (Nom (NNS mice))
      (PP (Prep in) (NP (Det the) (Nom (NNS cells)))))))
<class 'nltk.tree.Tree'>
**********
(PRP They)
<class 'nltk.tree.Tree'>
**********
(VBP capture)
<class 'nltk.tree.Tree'>
**********
(NP
  (Nom
    (Nom (NNS mice))
    (PP (Prep in) (NP (Det the) (Nom (NNS cells))))))
<class 'nltk.tree.Tree'>
**********
They
<class 'str'>
**********
capture
<class 'str'>
**********
(Nom
  (Nom (NNS mice))
  (PP (Prep in) (NP (Det the) (Nom (NNS cells)))))
<class 'nltk.tree.Tree'>
**********
T
<class 'str'>
**********
h
<class 'str'>
**********
e
<class 'str'>