import nltk
from nltk.parse.generate import generate,demo_grammar
from nltk import CFG
grammar = CFG.fromstring("""
ROOT -> S
S -> NP VP
NP -> NP PP
NP -> DT NN
DT -> 'The'
NN -> 'work'
PP -> IN NP
IN -> 'of'
NP -> DT NN
DT -> 'the'
NN -> 'painter'
VP -> VBZ ADJP
VBZ -> 'is'
ADJP -> JJ
JJ -> 'good'
""")
print(grammar)
for sentence in generate(grammar, n=100):
print(' '.join(sentence))
给出了一个错误NLTK CFG递归深度误差
RuntimeError: maximum recursion depth exceeded while calling a Python object
试图改变在functools.py隐蔽功能,还是同样的问题。
查看https://github.com/nltk/nltk/issues/7 – alvas
看过帖子。所以问题一直持续到日期,是吗? –
这不是图书馆的问题。这是你如何定义语法的一个问题。你看到语法中出现无限循环的部分吗? – alvas