3

I am trying to run this code, for natural language processing.

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)

But i am getting the following compilation error:

trees = cp.nbest_parse(query.split())

AttributeError: 'FeatureChartParser' object has no attribute 'nbest_parse'

I am using python3.4 and nltk 3.0a4 . What can I do now to run this?

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Mitu Vinci
  • 455
  • 9
  • 21
  • May this question helpful for you. http://stackoverflow.com/questions/20983494/python-and-nltk-how-to-analyze-sentence-grammar – Rahul K P Jul 09 '15 at 06:23

3 Answers3

2
>>> 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)]
alvas
  • 115,346
  • 109
  • 446
  • 738
  • This isn't exactly equivalent. First, you're only returning one tree. Second, you're only returning the first parsed tree, which likely isn't the "best" or most likely correct parsing. – Cerin Oct 19 '16 at 19:34
1

nbest_parse was deprecated in NLTK 3.0, to test example code, I have to use NLTK 2.0 instead.

Link: https://github.com/nltk/nltk/wiki/Porting-your-code-to-NLTK-3.0

0

According to nltk doc

>>> import  nltk
>>> from nltk import load_parser
>>> cp = load_parser('grammars/book_grammars/sql0.fcfg')
>>> query = 'What cities are located in China'
>>> for tree in cp.parse(query.split()):
    ....print(tree)
Dinuka Thilanga
  • 4,220
  • 10
  • 56
  • 93