【问题标题】:When building Feature based grammar, why do I get "invalid syntax" error?构建基于特征的语法时,为什么会出现“无效语法”错误?
【发布时间】:2018-12-21 11:24:46
【问题描述】:

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

nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')
% start S 
S -> NP[NUM=?n] VP[NUM=?n]
# NP expansion productions
NP[NUM=?n] -> PropN[NUM=?n]
NP[NUM=?n] -> Det[NUM=?n] N[NUM=?n]
NP[NUM=pl] -> N[NUM=pl]

【问题讨论】:

  • 您是否尝试使用 Python 解释器运行这些行?这是不正确的。在书中的那个例子中,它们运行第一行,其余行是show_cfg 函数的输出。
  • 是的,我做错了。谢谢!

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


【解决方案1】:

正如@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 特定子语言。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2021-10-07
    • 2020-04-21
    相关资源
    最近更新 更多