Package nltk :: Package parse :: Module util
[hide private]
[frames] | no frames]

Source Code for Module nltk.parse.util

 1  # Natural Language Toolkit: Parser Utility Functions 
 2  # 
 3  # Author: Ewan Klein <[email protected]> 
 4  #          
 5  # URL: <http://nltk.org> 
 6  # For license information, see LICENSE.TXT 
 7   
 8   
 9  """ 
10  Utility functions for parsers. 
11  """ 
12   
13  ###################################################################### 
14  #{ Test Suites 
15  ###################################################################### 
16   
17   
18  from featurechart import load_earley 
19   
20 -class TestGrammar(object):
21 """ 22 Unit tests for CFG. 23 """
24 - def __init__(self, grammar, suite, accept=None, reject=None):
25 self.test_grammar = grammar 26 27 self.cp = load_earley(grammar, trace=0) 28 self.suite = suite 29 self._accept = accept 30 self._reject = reject
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