2013-06-07 2 views
1

재귀 문에서 생성자를 사용하려고하는데 예상 한 결과를 얻지 못하고 있습니다.재귀 함수 내에서 파이썬 생성기

약간의 배경 : 저는 구문 구문 분석 트리를 사용하고 있습니다. 개념적 목표는 고유 명사 ('NNP'태그로 표시)를 식별 할 때까지 트리를 되풀이하여 생성기를 사용하려고 시도하는 것입니다. 고유 명사가있는 모든 명사구 ('NP'로 표기)를 식별하십시오.

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

그리고 발전기를 반복하는 객체 수익률 출력 금지 :이 코드를 실행하면

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) 

나는이 출력을 얻을. 그러나 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']]], [',', ',']] 

.

답변

0
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) 
+0

고마워요! 이것은 효과가 있었다. – GrantD71