首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >递归函数中的Python生成器

递归函数中的Python生成器
EN

Stack Overflow用户
提问于 2013-06-07 12:06:24
回答 1查看 146关注 0票数 2

我试图在递归语句中使用生成器,但没有得到我期望的结果。

一个小背景:我正在使用语法分析树,概念上的目标是向下递归树,直到我识别出专有名词(由'NNP‘标签表示),然后我尝试使用生成器来识别专有名词所在的每个名词短语(由’NP‘表示)。

代码语言:javascript
复制
alist = ['ROOT', ['S', ['NP', ['PRP', 'We']], ['VP', ['VBP', 'have'], ['VP', ['VBN', 'received'], ['NP', ['NN', 'information']],
        ['PP', ['IN', 'from'], ['NP', ['NP', ['DT', 'a'], ['NN', 'source']], ['VP', ['VBN', 'entitled'], ['PP', ['TO', 'to'],
        ['NP', ['NN', 'belief']]], [',', ','], ['SBAR', ['IN', 'that'], ['S', ['NP', ['NNP', 'Lincoln']], ['VP', ['VP', ['VBZ', 'has'],
        ['VP', ['VBN', 'paid'], ['NP', ['DT', 'a'], ['JJ', 'hurried'], ['NN', 'visit']], ['PP', ['TO', 'to'],
        ['NP', ['NP', ['DT', 'the'], ['NNP', 'Army']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'Potomac']]]]],
        [',', ','], ['PRN', ['-LRB-', '-LRB-'], ['ADVP', ['RB', 'now']], ['ADJP', ['JJ', 'burrowing'], ['PP', ['IN', 'on'],
        ['NP', ['NP', ['DT', 'the'], ['NN', 'north'], ['NN', 'bank']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'James']]],
        [',', ',']]]], ['-RRB-', '-RRB-']]]], ['CC', 'and'], ['VP', ['VBD', 'satisfied'], ['NP', ['PRP', 'himself']],
        [',', ','], ['PP', ['IN', 'by'], ['NP', ['JJ', 'personal'], ['NN', 'observation']]], [',', ','],
        ['PP', ['IN', 'in'], ['NP', ['NN', 'regard']]], ['PP', ['TO', 'to'], ['NP', ['NP', ['DT', 'the'], ['JJ', 'true'], ['NN', 'situation']],
        ['PP', ['IN', 'of'], ['NP', ['NNS', 'affairs']]]]]]]]]]]]]], ['.', '.']]]

def PullNP(NNP, NPLists):
    if NNP in NPLists:
        print "Pulling relevant NP"
        print NNP
        yield NNP
    for thing in NPLists:
        if NNP in thing:
            PullNP(thing, NPLists)
        else:
            for s in thing:
                if str(type(s)) == "<type 'list'>" and NNP in s: PullNP(s, NPLists)


def RecurseNNP(alist, pastlists=None, count=None):
    if pastlists is None: pastlists = []
    if count is None: count = 0
    if 'NNP' in alist[0]:
        NNPs = PullNP(alist, pastlists)
        print NNPs
        for np in NNPs:
            print np
    else:
        if str(type(alist)) == "<type 'list'>":
            if alist[0] == 'NP':
                pastlists.append(alist)
            for x in alist[1:]:
                RecurseNNP(x, pastlists, count)

RecurseNNP(alist)

如果我运行这段代码,我会得到这样的输出:

代码语言:javascript
复制
<generator object PullNP at 0x0288B648>
<generator object PullNP at 0x02885558>
<generator object PullNP at 0x02885558>
<generator object PullNP at 0x02885558>

并且遍历生成器对象不会产生任何输出。但是,如果删除yield语句并仅将PullNP作为递归函数运行,则可以确认打印语句包含我希望它们输出的内容。也就是说,我希望我的生成器包含这些列表:

代码语言:javascript
复制
Pulling relevant NP
['NP', ['NNP', 'Lincoln']]
Pulling relevant NP
['NP', ['DT', 'the'], ['NNP', 'Army']]
Pulling relevant NP
['NP', ['NP', ['DT', 'the'], ['NNP', 'Army']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'Potomac']]]]
Pulling relevant NP
['NP', ['DT', 'the'], ['NNP', 'Army']]
Pulling relevant NP
['NP', ['NP', ['DT', 'the'], ['NNP', 'Army']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'Potomac']]]]
Pulling relevant NP
['NP', ['DT', 'the'], ['NNP', 'Potomac']]
Pulling relevant NP
['NP', ['NP', ['DT', 'the'], ['NNP', 'Army']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'Potomac']]]]
Pulling relevant NP
['NP', ['DT', 'the'], ['NNP', 'James']]
Pulling relevant NP
['NP', ['NP', ['DT', 'the'], ['NN', 'north'], ['NN', 'bank']], ['PP', ['IN', 'of'], ['NP', ['DT', 'the'], ['NNP', 'James']]], [',', ',']]

我已经阅读了解释生成器和输出的主要堆栈溢出帖子,但我仍然不明白为什么我的生成器没有输出任何东西。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16976056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档