1
2
3
4
5
6
7
8
9 """
10 Utility functions for parsers.
11 """
12
13
14
15
16
17
18 from featurechart import load_earley
19
21 """
22 Unit tests for CFG.
23 """
24 - def __init__(self, grammar, suite, accept=None, reject=None):
31
32
33 - def run(self, show_trees=False):
34 """
35 Sentences in the test suite are divided into two classes:
36 - grammatical (C{accept}) and
37 - ungrammatical (C{reject}).
38 If a sentence should parse accordng to the grammar, the value of
39 C{trees} will be a non-empty list. If a sentence should be rejected
40 according to the grammar, then the value of C{trees} will be C{None}.
41 """
42 for test in self.suite:
43 print test['doc'] + ":",
44 for key in ['accept', 'reject']:
45 for sent in test[key]:
46 tokens = sent.split()
47 trees = self.cp.parse(tokens)
48 if show_trees and trees:
49 print
50 print sent
51 for tree in trees:
52 print tree
53 if key=='accept':
54 if trees == []:
55 raise ValueError, "Sentence '%s' failed to parse'" % sent
56 else:
57 accepted = True
58 else:
59 if trees:
60 raise ValueError, "Sentence '%s' received a parse'" % sent
61 else:
62 rejected = True
63 if accepted and rejected:
64 print "All tests passed!"
65