加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:31:32 所属栏目:Python 来源:网络整理
导读:所以我正在编写一个涉及 python和NLTK的教程. 我目前正在使用无上下文语法. 我输入以下命令并收到错误… from nltk import parse_cfgTraceback (most recent call last): File "(stdin)",line 1,in (module)ImportError: cannot import name 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']

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读