Python, NLTK, can't import "parse_cfg"?
So I'm working on a tutorial involving python and NLTK.
I am currently working with context free grammars.
I type the following command and I 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
Can anyone understand what could be causing this? Some cfg commands work, but not this one.
+3
source to share
1 answer
We've updated the API for NLTK 3. Please read the docs
To access the old, nltk.parse_cfg()
useCFG.fromstring()
Example from 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']
+11
source to share