Vinny Chase
Vinny Chase

Reputation: 157

Python NLTK parsing error? 'str' object has no attribute 'check_coverage'

I am trying to use NLTK to determine if a sentence is valid. i loaded the grammar, but whenever I try to get the parser it does not work and I get the error "AttributeError: 'str' object has no attribute 'check_coverage'" This is my code:

sentence = ['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
grammar = nltk.data.load('grammars/large_grammars/atis.cfg', 'text')
parser =  nltk.parse.BottomUpChartParser(grammar)
chart = parser.chart_parse(sentence)

Here is the full traceback: Traceback (most recent call last):

 File "<ipython-input-448-852d3bb24984>", line 1, in <module>
 chart = parser.chart_parse(sentence)

 File "C:\Users\Class2016\Anaconda3\lib\site-packages\nltk\parse\chart.py", 
 line 1310, in chart_parse
  self._grammar.check_coverage(tokens)

AttributeError: 'str' object has no attribute 'check_coverage'

I got this part of code from the example listed here under Unit tests for LARGE context-free grammars: http://www.nltk.org/howto/parse.html

Any information on why this is happening or how to correct this would be greatly appreciated.

Thanks!

Upvotes: 3

Views: 2734

Answers (1)

cs95
cs95

Reputation: 402814

Going by this link, you might want to parse those rules first using nltk.parse_cfg:

rules = nltk.data.load('grammars/large_grammars/atis.cfg', 'text')
grammar = nltk.parse_cfg(rules)
parser =  nltk.parse.BottomUpChartParser(parsed_grammar)

Upvotes: 1

Related Questions