NLTK ValueError: Unable to parse line 1: S -> NP-SBJ VP . Expected a nonterminal, found:

NLTK ValueError: Unable to parse line 1: S -> NP-SBJ VP . Expected a nonterminal, found:

我是NLP的新手,现在我想解析很多句子(大约10000个)来得到他们的CFG树。在此之前,我必须构建一个语法来解析它们。我尝试使用 NLTK treebank,但它们不能作为语法加载。不知道为什么,请问有没有大佬指点一下?

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')

它给我一个错误: ** ValueError:无法解析第 1 行:S -> NP-SBJ VP。 预期是一个非终结符,发现: 。 **

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

这只是一个猜测,但是短语结构文法的正常形式在推导端不允许将终结符号作为一个类别。这就是为什么它说 "expected a nonterminal"。在您的规则 (S-> NP-SUBJ VP .) 中找到的唯一终端符号是点“.”,以防这不是“.”的复制粘贴错误。属于规则。去掉那个点再试,应该可以了。