簡體   English   中英

遞歸函數中的Python生成器

[英]Python generator within a recursive function

我正在嘗試在遞歸語句中使用生成器,但未獲得預期的結果。

一點背景知識:我正在使用語法分析樹,並且概念上的目標是遞歸樹直到找到合適的名詞(由“ NNP”標記表示),然后嘗試使用生成器來標識每個名詞短語(由“ NP”表示)專有名詞在其中。

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)

如果運行此代碼,則會得到以下輸出:

<generator object PullNP at 0x0288B648>
<generator object PullNP at 0x02885558>
<generator object PullNP at 0x02885558>
<generator object PullNP at 0x02885558>

遍歷生成器對象不會產生任何輸出。 但是,如果刪除yield語句而僅將PullNP作為遞歸函數運行,則可以確認print語句包含我希望它們輸出的內容。 即我希望我的生成器包含以下列表:

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']]], [',', ',']]

我已經閱讀了主要的堆棧溢出文章,解釋了生成器和良率,但我仍然不明白為什么生成器不輸出任何內容。

僅調用子生成器將不會使生成器(迭代器)運行。 您需要迭代返回的值(或顯式調用next )以運行並使用產生的值。

這是回溯功能。 因此,將檢索到的項目交還給調用方。 請注意標有# <---的代碼部分

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:
            for nnp in PullNP(thing, NPLists):
                yield nnp
        else:
            for s in thing:
                if isinstance(s, list) and NNP in s:
                    for nnp in PullNP(s, NPLists):  # <---
                        yield nnp                   # <---


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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM