簡體   English   中英

Python,NLTK,無法導入“parse_cfg”?

[英]Python, NLTK, can't import “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。請閱讀文檔

訪問舊的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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM