class A2Q1GeneratedUnitTests(unittest.TestCase): LEXICON_FILE_NAME = 'Lexicon' GRAMMAR_FILE_NAME = 'Grammar' PARSE_TREES_FILE_NAME = 'ParseTrees' @classmethod def setUpClass(cls): assert(hasattr(cls,'PREAMBLE')) # clear the old parse trees file. It should start with # the preamble specified later on in the generated section with open(cls.PARSE_TREES_FILE_NAME,'w') as f: f.write(cls.PREAMBLE) f.write('\n') def setUp(self): if not exists(self.LEXICON_FILE_NAME): self.skipTest("Unable to find file {} as lexicon".format( self.LEXICON_FILE_NAME)) if not exists(self.GRAMMAR_FILE_NAME): self.skipTest("Unable to find file {} as grammar".format( self.GRAMMAR_FILE_NAME)) assert exists(self.PARSE_TREES_FILE_NAME) valid,lexiconText = q1utils.sanitizeAndValidateLexicon( self.LEXICON_FILE_NAME) if not valid: self.skipTest("Lexicon {} is invalid.".format( self.LEXICON_FILE_NAME)) valid,grammarText = q1utils.sanitizeAndValidateGrammar( self.GRAMMAR_FILE_NAME) if not valid: self.skipTest("Grammar {} is invalid.".format( self.GRAMMAR_FILE_NAME)) allRules = grammarText + '\n' + lexiconText try: grammar = CFG.fromstring(allRules) self._parser = BottomUpChartParser(grammar) except Exception as e: self.skipTest(str(e)) def _runSentenceHelper(self,sentence): # archetype method for parsing sentences trees = self._parser.parse(word_tokenize(sentence)) with open(self.PARSE_TREES_FILE_NAME,'a') as f: f.write(sentence) f.write('\n') for tree in trees: f.write(str(tree)) f.write('\n') if not trees: f.write('No parses') f.write('\n\n') return bool(trees)
class A2Q1GeneratedUnitTests(unittest.TestCase): LEXICON_FILE_NAME = 'Lexicon' GRAMMAR_FILE_NAME = 'Grammar' PARSE_TREES_FILE_NAME = 'ParseTrees' @classmethod def setUpClass(cls): assert(hasattr(cls,'PREAMBLE')) with open(cls.PARSE_TREES_FILE_NAME,'w') as f: f.write(cls.PREAMBLE) f.write('\n') def setUp(self): if not exists(self.LEXICON_FILE_NAME): self.skipTest("Unable to find file {} as lexicon".format( self.LEXICON_FILE_NAME)) if not exists(self.GRAMMAR_FILE_NAME): self.skipTest("Unable to find file {} as grammar".format( self.GRAMMAR_FILE_NAME)) assert exists(self.PARSE_TREES_FILE_NAME) valid,lexiconText = q1utils.sanitizeAndValidateLexicon( self.LEXICON_FILE_NAME) if not valid: self.skipTest("Lexicon {} is invalid.".format( self.LEXICON_FILE_NAME)) valid,grammarText = q1utils.sanitizeAndValidateGrammar( self.GRAMMAR_FILE_NAME) if not valid: self.skipTest("Grammar {} is invalid.".format( self.GRAMMAR_FILE_NAME)) allRules = grammarText + '\n' + lexiconText try: grammar = CFG.fromstring(allRules) self._parser = BottomUpChartParser(grammar) except Exception as e: self.skipTest(str(e)) def _runSentenceHelper(self,sentence): trees = self._parser.parse(word_tokenize(sentence)) with open(self.PARSE_TREES_FILE_NAME,'a') as f: f.write(sentence) f.write('\n') for tree in trees: f.write(str(tree)) f.write('\n') if not trees: f.write('No parses') f.write('\n\n') return bool(trees) PREAMBLE = '%Sam Earle,c2earles,999228438' def test_parseTheStudentPreferredToSleep(self): self.assertTrue(self._runSentenceHelper('the student preferred to sleep'), 'Could not parse "the student preferred to sleep"') def test_parseTheStudentPersuadedTheTeacherToSleep(self): self.assertTrue(self._runSentenceHelper('the student persuaded the teacher to sleep'), 'Could not parse "the student persuaded the teacher to sleep"') def test_parseTheStudentPromisedTheTeacherToSleep(self): self.assertTrue(self._runSentenceHelper('the student promised the teacher to sleep'), 'Could not parse "the student promised the teacher to sleep"') def test_parseTheStudentExpectedTheTeacherToSleep(self): self.assertTrue(self._runSentenceHelper('the student expected the teacher to sleep'), 'Could not parse "the student expected the teacher to sleep"') def test_parseTheTeacherExpectedToSleep(self): self.assertTrue(self._runSentenceHelper('the teacher expected to sleep'), 'Could not parse "the teacher expected to sleep"') def test_parseTheTeacherPersuadedTheStudent(self): self.assertTrue(self._runSentenceHelper('the teacher persuaded the student'), 'Could not parse "the teacher persuaded the student"') def test_parseTheTeacherPromisedToSleep(self): self.assertTrue(self._runSentenceHelper('the teacher promised to sleep'), 'Could not parse "the teacher promised to sleep"') def test_parseTheStudentPreferredTheTeacher(self): self.assertTrue(self._runSentenceHelper('the student preferred the teacher'), 'Could not parse "the student preferred the teacher"')