NLTK ValueError: 无法解析第 1 行:S -> NP-SBJ VP 。预期为非终端,发现:



我是NLP的新手,现在我想解析很多句子(大约10000个(来获取他们的CFG树。在此之前,我必须构建一个语法来解析它们。我尝试使用 NLTK 树库,但它们不能作为语法加载。我不知道为什么,谁能给我一些建议?

import nltk
from nltk.corpus import treebank
nltk.download('treebank')
treeData = treebank.parsed_sents()
treeData_rules = []
for sent in treeData:
for production in sent.productions():
treeData_rules.append(production)
test = open("test.cfg","w+")
for i in range(len(treeData_rules)):
test.write(str(treeData_rules[i]))
test.write('n')
test.close()
grammar1 = nltk.data.load('file:test.cfg')

它给了我一个错误: ** 值错误:无法解析第 1 行:S -> NP-SBJ VP 。 预期为非终端,发现:。 **

我不知道为什么NLTK不能加载NLTK树库语法。

这只是一个猜测,但短语结构语法的正常形式不允许终端符号作为派生端的类别。这就是为什么它说"预期非终端"。在您的规则 (S-> NP-SUBJ VP .( 中找到的唯一终端符号是点 ".",以防这不是复制粘贴错误,则 "." 属于规则。删除该点并重试,然后它应该可以工作。

最新更新