简体   繁体   English

Python,NLTK,无法导入“parse_cfg”?

[英]Python, NLTK, can't import “parse_cfg”?

So I'm working through a tutorial involving python and the NLTK. 所以我正在编写一个涉及python和NLTK的教程。

I'm currently working with context free grammars. 我目前正在使用无上下文语法。

I type the following command and get an error... 我输入以下命令并收到错误...

>>> from nltk import parse_cfg
Traceback (most recent call last):
 File "(stdin)", line 1, in (module)
ImportError: cannot import name parse_cfg

Does anyone have any idea what could be causing that? 有谁知道可能导致什么? Some of the cfg commands work, but not this one. 一些cfg命令可以工作,但不是这个。

We updated the API for NLTK 3. Please read the docs 我们更新了NLTK 3的API。请阅读文档

The way to access the old nltk.parse_cfg() is using CFG.fromstring() 访问旧的nltk.parse_cfg()是使用CFG.fromstring()

Example from http://www.nltk.org/howto/grammar.html : 来自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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM