我正在学习关于Python和NLTK的教程。
目前我正在学习上下文无关语法。
我输入以下命令并收到了一个错误提示...
>>> from nltk import parse_cfg
Traceback (most recent call last):
File "(stdin)", line 1, in (module)
ImportError: cannot import name parse_cfg
有人知道是什么原因导致这种情况吗?一些cfg命令可以运行,但是这个却不行。
我正在学习关于Python和NLTK的教程。
目前我正在学习上下文无关语法。
我输入以下命令并收到了一个错误提示...
>>> from nltk import parse_cfg
Traceback (most recent call last):
File "(stdin)", line 1, in (module)
ImportError: cannot import name parse_cfg
有人知道是什么原因导致这种情况吗?一些cfg命令可以运行,但是这个却不行。
我们更新了NLTK 3的API。请阅读文档
访问旧的nltk.parse_cfg()
的方式是使用CFG.fromstring()
来自http://www.nltk.org/howto/grammar.html的示例:
>>> from nltk import CFG
>>> grammar = CFG.fromstring("""
... S -> NP VP
... PP -> P NP
... NP -> Det N | NP PP
... VP -> V NP | VP PP
... Det -> 'a' | 'the'
... N -> 'dog' | 'cat'
... V -> 'chased' | 'sat'
... P -> 'on' | 'in'
... """)
>>> grammar
<Grammar with 14 productions>
>>> grammar.start()
S
>>> grammar.productions()
[S -> NP VP, PP -> P NP, NP -> Det N, NP -> NP PP, VP -> V NP, VP -> VP PP,
Det -> 'a', Det -> 'the', N -> 'dog', N -> 'cat', V -> 'chased', V -> 'sat',
P -> 'on', P -> 'in']
parse
模块的开发人员已经将其移动了,但忘记把它放回去了。问题已提出:https://github.com/nltk/nltk/issues/771 - alvasnltk.__version__
命令以确定哪个版本的nltk引起了问题。 - alvas