Beispiel #1
0
def test(grammarText, sentences):
	"""Test the coverage of a CFG grammar.
	
	grammarText -- the grammar string
	sentences -- a list of sentences to test, with invalid ones prefixed with '*'
	
	"""	
	
	valid_sentences = [s for s in sentences if s[0] != '*']
	invalid_sentences = [s[1:] for s in sentences if s[0] == '*']
	parser = ChartParser(parse_cfg(grammarText))

	for sentence in valid_sentences:
		parses = parser.nbest_parse(sentence.split())
		print(sentence + "\n" + "\n".join(map(str, parses)) + "\n")
		assert parses, "Valid sentence failed to parse."

	for sentence in invalid_sentences:
		print("*" + sentence)
		parses = parser.nbest_parse(sentence.split())
		assert parses == [], "Invalid sentence parsed successfully."