load_earley在nltk 3.0中不可用

时间:2017-02-20 22:05:34

标签: python nlp nltk

我正在尝试从NLTK教科书中复制一个示例 - http://www.ling.helsinki.fi/kit/2009s/clt231/NLTK/book/ch10-AnalyzingTheMeaningOfSentences.html

但是,在运行此示例时:

>>> from nltk.parse import load_earley
>>> cp = load_earley('grammars/book_grammars/sql0.fcfg')
>>> query = 'What cities are located in China'
>>> trees = cp.nbest_parse(query.split())
>>> answer = trees[0].node['sem']
>>> q = ' '.join(answer)
>>> print q

我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name load_earley

load_earley已停产吗?如果是这样,我无法找到它的替代品。请帮助

2 个答案:

答案 0 :(得分:2)

我相信是这样的,我遇到了同样的问题。该课程使用NLTK 2.0版。也:

The material presented in this book assumes that you are using Python version 2.4 or 2.5

答案 1 :(得分:0)

没关系。我得到了更新的课程 - &gt; http://www.nltk.org/book/ch10.html

上述代码转换为

>>>from nltk import load_parser
>>> cp = load_parser('grammars/book_grammars/sql0.fcfg')
>>> query = 'What cities are located in China'
>>> trees = list(cp.parse(query.split()))
>>> answer = trees[0].label()['SEM']
>>> answer = [s for s in answer if s]
>>> q = ' '.join(answer)
>>> print(q)

load_earley由load_parser替换

>>> trees = cp.nbest_parse(query.split())
>>> answer = trees[0].node['sem']

替换为

>>> trees = list(cp.parse(query.split()))
>>> answer = trees[0].label()['SEM']
>>> answer = [s for s in answer if s]