3 Unit tests for the sr (Shift Reduce Parser) class
Create and run a shift reduce parser over both a syntactically ambiguous
and unambiguous sentence. Note that unlike the recursive descent parser, one
and only one parse is ever returned.
| |
>>> from nltk.parse import ShiftReduceParser
>>> sr = ShiftReduceParser(grammar)
|
|
| |
>>> sentence1 = 'the cat chased the dog'.split()
>>> sentence2 = 'the cat chased the dog on the rug'.split()
|
|
| |
>>> for t in sr.nbest_parse(sentence1):
... print t
(S (NP the (N cat)) (VP (V chased) (NP the (N dog))))
|
|
The shift reduce parser uses heuristics to decide what to do when there are
multiple possible shift or reduce operations available - for the supplied
grammar clearly the wrong operation is selected.
| |
>>> for t in sr.nbest_parse(sentence2):
... print t
|
|
6 Unit tests for LARGE context-free grammars
Reading the ATIS grammar.
| |
>>> grammar = nltk.data.load('grammars/large_grammars/atis.cfg')
>>> grammar
<Grammar with 5517 productions>
|
|
Reading the test sentences.
| |
>>> sentences = nltk.data.load('grammars/large_grammars/atis_sentences.txt', format='raw')
>>> sentences = nltk.parse.util.extract_test_sentences(sentences)
>>> len(sentences)
98
>>> testsentence = sentences[22]
>>> testsentence[0]
['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
>>> testsentence[1]
17
>>> sentence = testsentence[0]
|
|
Bottom-up parsing.
| |
>>> parser = nltk.parse.BottomUpChartParser(grammar)
>>> chart = parser.chart_parse(sentence)
>>> print chart.num_edges()
7661
>>> print len(chart.parses(grammar.start()))
17
|
|
Bottom-up Left-corner parsing.
| |
>>> parser = nltk.parse.BottomUpLeftCornerChartParser(grammar)
>>> chart = parser.chart_parse(sentence)
>>> print chart.num_edges()
4986
>>> print len(chart.parses(grammar.start()))
17
|
|
Top-down parsing.
| |
>>> parser = nltk.parse.TopDownChartParser(grammar)
>>> chart = parser.chart_parse(sentence)
>>> print chart.num_edges()
28352
>>> print len(chart.parses(grammar.start()))
17
|
|
Earley parsing.
| |
>>> parser = nltk.parse.EarleyChartParser(grammar)
>>> chart = parser.chart_parse(sentence)
>>> print chart.num_edges()
28352
>>> print len(chart.parses(grammar.start()))
17
|
|