构建基于要素的语法时,为什么会出现“无效语法”错误?

时间:2018-12-21 11:24:47

标签: python-3.x nltk grammar feature-extraction nltk-book

为什么在%开头S的行中出现“无效语法”?

div

1 个答案:

答案 0 :(得分:0)

正如@cody所指出的,这是代码部分:

>>> import nltk
>>> nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')

输出以下内容:

% start S
# ###################
# Grammar Productions
# ###################
# S expansion productions
S -> NP[NUM=?n] VP[NUM=?n]
# NP expansion productions
NP[NUM=?n] -> N[NUM=?n] 
NP[NUM=?n] -> PropN[NUM=?n] 
NP[NUM=?n] -> Det[NUM=?n] N[NUM=?n]
NP[NUM=pl] -> N[NUM=pl] 
# VP expansion productions
VP[TENSE=?t, NUM=?n] -> IV[TENSE=?t, NUM=?n]
VP[TENSE=?t, NUM=?n] -> TV[TENSE=?t, NUM=?n] NP
# ###################
# Lexical Productions
# ###################
Det[NUM=sg] -> 'this' | 'every'
Det[NUM=pl] -> 'these' | 'all'
Det -> 'the' | 'some' | 'several'
PropN[NUM=sg]-> 'Kim' | 'Jody'
N[NUM=sg] -> 'dog' | 'girl' | 'car' | 'child'
N[NUM=pl] -> 'dogs' | 'girls' | 'cars' | 'children' 
IV[TENSE=pres,  NUM=sg] -> 'disappears' | 'walks'
TV[TENSE=pres, NUM=sg] -> 'sees' | 'likes'
IV[TENSE=pres,  NUM=pl] -> 'disappear' | 'walk'
TV[TENSE=pres, NUM=pl] -> 'see' | 'like'
IV[TENSE=past] -> 'disappeared' | 'walked'
TV[TENSE=past] -> 'saw' | 'liked'

尝试键入以下内容时:

>>> % start S
  File "<stdin>", line 1
    % start S
    ^
SyntaxError: invalid syntax

您会看到一个SyntaxError,因为它不是Python,而是编写.cfg / .fcfg语法的NLTK特定子语言。