Wol*_*lff 4 python nltk context-free-grammar
所以我正在编写一个涉及python和NLTK的教程.
我目前正在使用无上下文语法.
我输入以下命令并收到错误...
>>> from nltk import parse_cfg
Traceback (most recent call last):
File "(stdin)", line 1, in (module)
ImportError: cannot import name parse_cfg
Run Code Online (Sandbox Code Playgroud)
有谁知道可能导致什么?一些cfg命令可以工作,但不是这个.
小智 14
我们更新了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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5700 次 |
| 最近记录: |