NLTK CFG recursion depth error
<pre><code>import nltk   
from nltk.parse.generate import generate,demo_grammar   
from nltk import CFG   
grammar = CFG.fromstring("""   
ROOT -&gt; S
S -&gt; NP VP
NP -&gt; NP PP
NP -&gt; DT NN
DT -&gt; 'The'
NN -&gt; 'work'
PP -&gt; IN NP
IN -&gt; 'of'
NP -&gt; DT NN
DT -&gt; 'the'
NN -&gt; 'painter'
VP -&gt; VBZ ADJP
VBZ -&gt; 'is'
ADJP -&gt; JJ
JJ -&gt; 'good'
""")    
print(grammar)   
for sentence in generate(grammar, n=100):   
   print(' '.join(sentence))
</code></pre>

<p>Gives an error</p>

<pre><code>RuntimeError: maximum recursion depth exceeded while calling a Python object
</code></pre>

<p>Tried changing covert function in functools.py, still the same issue.</p>

<p>I tried to number the repeating occurrence of NP NN DT etc. It seems to solve the problem due to unique identification( I presume). What wonders me is it should have been like this at first place i.e the tree production thrown out should have serialized the parts of speech.     </p>

<pre><code>import nltk   
from nltk.parse.generate import generate,demo_grammar   
from nltk import CFG   
grammar = CFG.fromstring("""
ROOT -&gt; S   
S -&gt; NP VP
NP -&gt; NP1 PP
NP1 -&gt; DT1 NN1
DT1 -&gt; 'The'
NN1 -&gt; 'work'
PP -&gt; IN NP2
IN -&gt; 'of'
NP2 -&gt; DT2 NN2
DT2 -&gt; 'the'
NN2 -&gt; 'painter'
VP -&gt; VBZ ADJP
VBZ -&gt; 'is'
ADJP -&gt; JJ
JJ -&gt; 'good'
""")    
print(grammar)   
for sentence in generate(grammar, n=100):   
   print(' '.join(sentence))
</code></pre>

python nltk