NLTK ChartParser提供空列表

问题描述 投票:0回答:1

任务是定义语法,并使用ChartParser使用nltk中的ChartParser包解析任何给定的句子。我的代码如下:

import nltk

your_grammar = nltk.CFG.fromstring("""
S -> NP VP
NP -> NP PP
Nom ->  N
VP -> V NP | V S | VP PP
PP -> P NP
Det -> 'the' 
N -> 'block' | 'table' 
V -> 'Put'
P -> 'on' 
""")

parser = nltk.ChartParser(your_grammar)
sent = 'Put the block on the table'.split()
print (list(parser.parse(sent)))

但是输出给出一个空列表。

输出:

[]

有人可以帮我吗?

python nltk
1个回答
0
投票

当前,语法无法匹配任何字符串,因为它包含无限循环。

[如果我部分展开S,则会得到NP 'on' NP VP,由于NP -> NP,不可能将其解析为“ nothing”;我必须总是匹配其他东西。

如果为NP -> '',则将允许NP不匹配(良好),因此,这意味着S -> VP将开始与第一个单词Put匹配。这应该将语法放在正确的轨道上,具体取决于您希望其如何匹配。

© www.soinside.com 2019 - 2024. All rights reserved.