NLTK CFG.fromstring给出ValueError

时间:2017-02-13 15:09:17

标签: python parsing nlp nltk context-free-grammar

以下是提供ValueError

的代码
import nltk as nl

grammar1 = nl.CFG.fromstring("""
S -> ',' NP VP
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
V -> "saw" | "ate" | "walked"
VP -> V NP | V NP PP
PP -> P NP
, -> ','
""")

ValueError: Unable to parse line 10: , -> ',' Expected a nonterminal, found: , -> ','

以下代码完全正常

import nltk as nl

grammar1 = nl.CFG.fromstring("""
S -> COMMA NP VP
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
V -> "saw" | "ate" | "walked"
VP -> V NP | V NP PP
PP -> P NP
COMMA -> ','
""")

为什么会这样?我想为标点符号很常见的一堆数据生成语法,而且我的语法的生成规则的数量在多个地方都有这些标点符号。我是否必须更改终端名称全面生产规则? 我使用的是python 3.4和nltk 3.2.1

0 个答案:

没有答案