编程语言
首页 > 编程语言> > Python,NLTK,无法导入“parse_cfg”?

Python,NLTK,无法导入“parse_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.请read the docs

访问旧的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']

标签:python,nltk,context-free-grammar
来源: https://codeday.me/bug/20190714/1459342.html