我正在尝试运行此代码,以进行自然语言处理。
import nltk
from nltk import load_parser
cp = load_parser('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)
但是我收到以下编译错误:
trees = cp.nbest_parse(query.split())
AttributeError:“ FeatureChartParser”对象没有属性“ nbest_parse”
我正在使用python3.4和nltk 3.0a4。我现在该怎么办?
最佳答案
>>> import nltk
>>> from nltk import load_parser
>>> cp = load_parser('grammars/book_grammars/sql0.fcfg')
>>> query = 'What cities are located in China'
>>> trees = next(cp.parse(query.split()))
>>> answer = trees[0].label()
>>> answer
NP[SEM=(SELECT, City FROM city_table)]