代码如下:
groucho_grammar = nltk.CFG.fromstring("""
S -> V NP PP CONJ V NP PP
PP -> PRP NP
NP -> Det N | PRP N |DET ADJ CONJ ADJ N P
Det -> 'a' | 'every' | 'all'
N -> 'work' | 'Word Document' | 'results' | 'step'
ADJ -> 'intermediate' | 'final'
V -> 'Describe' | 'present'
P -> 'of' | 'in'
CONJ -> 'and'
PRP -> 'your'
""")
sent = ['Describe', 'every', 'step' ,'of', 'your', 'work', 'and' ,\
'present', 'all', 'intermediate' ,'and' ,'final', 'results', 'in' ,'a', 'Word Document']
parser = nltk.ChartParser(groucho_grammar)
for tree in parser.parse(sent):
print(tree)
当我这样做时,它运行时没有任何错误,但它不打印任何语法树。我不知道我做错了什么。我遵循了nltk书中的指导原则,但这没有帮助。在
总是以字节大小编写CFG语法,请参见Python and NLTK: How to analyze sentence grammar?
让我们先处理
describe your work
。[出来]:
^{pr2}$现在让我们试试
describe every step of your work
:[出来]:
现在让我们试试
present final results in a Word Document
:[出来]:
现在,让我们为
present all final results in a Word Document
添加NP -> DT NP
:[出来]:
现在让我们来看看
present all intermediate and final results in a Word_Document
的连词:[出来]:
但这只给你一个读数
present all [(intermediate and final) (results) (in a Word_Document)]
。对于模棱两可的结果,我将留给你想象现在让我们继续并连接
S -> S CONJ S
的describe your work and present all intermediate and final results in a Word_Document
:[出来]:
当然还有其他的方法来编写CFG语法来适应你的句子,这只是众多方法中的一种。但一般来说,用bitesize编写CFG语法。在
相关问题 更多 >
编程相关推荐