def testEnumeratedList(self): lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(lexicon) realiser = Realiser(lexicon) realiser.setFormatter(HTMLFormatter()) document = nlgFactory.createDocument("Document") paragraph = nlgFactory.createParagraph() list_1 = nlgFactory.createEnumeratedList() item1 = nlgFactory.createListItem() item2 = nlgFactory.createListItem() # NB: a list item employs orthographical operations only until sentence level; # nest clauses within a sentence to generate more than 1 clause per list item. sentence1 = nlgFactory.createSentence("this", "be", "the first sentence") sentence2 = nlgFactory.createSentence("this", "be", "the second sentence") item1.addComponent(sentence1) item2.addComponent(sentence2) list_1.addComponent(item1) list_1.addComponent(item2) paragraph.addComponent(list_1) document.addComponent(paragraph) expectedOutput = "<h1>Document</h1>" + "<p>" + "<ol>" + "This is the first sentence." \ + "This is the second sentence." + "</ol>" + "</p>" realisedOutput = realiser.realise(document).getRealisation() self.assertEqual(expectedOutput, realisedOutput)
class StandAloneExample(unittest.TestCase): def setUp(self): self.lexicon = XMLLexicon() self.nlgFactory = NLGFactory(self.lexicon) self.realiser = Realiser(self.lexicon) def testSentenceCreation(self): # create sentences thePark = self.nlgFactory.createNounPhrase("the", "park") bigp = self.nlgFactory.createAdjectivePhrase("big") bigp.setFeature(Feature.IS_COMPARATIVE, True) thePark.addModifier(bigp) # above relies on default placement rules. You can force placement as a premodifier # (before head) by using addPreModifier toThePark = self.nlgFactory.createPrepositionPhrase("to") toThePark.setObject(thePark) # could also just say self.nlgFactory.createPrepositionPhrase("to", the Park) johnGoToThePark = self.nlgFactory.createClause("John", "go", toThePark) johnGoToThePark.setFeature(Feature.TENSE, Tense.PAST) johnGoToThePark.setFeature(Feature.NEGATED, True) # note that constituents (such as subject and object) are set with setXXX methods # while features are set with setFeature sentence = self.nlgFactory.createSentence(johnGoToThePark) # below creates a sentence DocumentElement by concatenating strings hePlayed = StringElement("he played") there = StringElement("there") football = WordElement("football") sentence2 = self.nlgFactory.createSentence() sentence2.addComponent(hePlayed) sentence2.addComponent(football) sentence2.addComponent(there) # now create a paragraph which contains these sentences paragraph = self.nlgFactory.createParagraph() paragraph.addComponent(sentence) paragraph.addComponent(sentence2) realised = self.realiser.realise(paragraph).getRealisation() # Test self.assertEqual( "John did not go to the bigger park. He played football there.\n\n", realised) def testMorphology(self): # second example - using simplenlg just for morphology word = self.nlgFactory.createWord("child", LexicalCategory.NOUN) # create InflectedWordElement from word element inflectedWord = InflectedWordElement(word) # set the inflected word to plural inflectedWord.setPlural(True) # realise the inflected word realised = self.realiser.realise(inflectedWord).getRealisation() # Test self.assertEqual('children', realised)
def testEnumeratedList(self): lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(lexicon) realiser = Realiser(lexicon) realiser.setFormatter(TextFormatter()) document = nlgFactory.createDocument("Document") paragraph = nlgFactory.createParagraph() subListItem1 = nlgFactory.createListItem() subListSentence1 = nlgFactory.createSentence("this", "be", "sub-list sentence 1") subListItem1.addComponent(subListSentence1) subListItem2 = nlgFactory.createListItem() subListSentence2 = nlgFactory.createSentence("this", "be", "sub-list sentence 2") subListItem2.addComponent(subListSentence2) subList = nlgFactory.createEnumeratedList() subList.addComponent(subListItem1) subList.addComponent(subListItem2) item1 = nlgFactory.createListItem() sentence1 = nlgFactory.createSentence("this", "be", "the first sentence") item1.addComponent(sentence1) item2 = nlgFactory.createListItem() sentence2 = nlgFactory.createSentence("this", "be", "the second sentence") item2.addComponent(sentence2) list_1 = nlgFactory.createEnumeratedList() list_1.addComponent(subList) list_1.addComponent(item1) list_1.addComponent(item2) paragraph.addComponent(list_1) document.addComponent(paragraph) expectedOutput = "Document\n" + \ "\n" + \ "1.1 - This is sub-list sentence 1.\n" + \ "1.2 - This is sub-list sentence 2.\n"+ \ "2 - This is the first sentence.\n" + \ "3 - This is the second sentence.\n" + \ "\n\n" # for the end of a paragraph realisedOutput = realiser.realise(document).getRealisation() self.assertEquals(expectedOutput, realisedOutput)
def testSection14(self): lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory( lexicon ) realiser = Realiser( lexicon ) p1 = nlgFactory.createClause( "Mary", "chase", "the monkey" ) p2 = nlgFactory.createClause( "The monkey", "fight back" ) p3 = nlgFactory.createClause( "Mary", "be", "nervous" ) s1 = nlgFactory.createSentence( p1 ) s2 = nlgFactory.createSentence( p2 ) s3 = nlgFactory.createSentence( p3 ) par1 = nlgFactory.createParagraph( [s1, s2, s3] ) output14a = realiser.realise( par1 ).getRealisation() correct = "Mary chases the monkey. The monkey fights back. Mary is nervous.\n\n" self.assertEqual(correct, output14a ) section = nlgFactory.createSection( "The Trials and Tribulation of Mary and the Monkey" ) section.addComponent( par1 ) output14b = realiser.realise( section ).getRealisation() correct = "The Trials and Tribulation of Mary and the Monkey\nMary chases the monkey. " + \ "The monkey fights back. Mary is nervous.\n\n" self.assertEqual(correct, output14b )
def testEnumeratedListWithSeveralLevelsOfNesting(self): lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(lexicon) realiser = Realiser(lexicon) realiser.setFormatter(TextFormatter()) document = nlgFactory.createDocument("Document") paragraph = nlgFactory.createParagraph() # sub item 1 subList1Item1 = nlgFactory.createListItem() subList1Sentence1 = nlgFactory.createSentence("sub-list item 1") subList1Item1.addComponent(subList1Sentence1) # sub sub item 1 subSubList1Item1 = nlgFactory.createListItem() subSubList1Sentence1 = nlgFactory.createSentence("sub-sub-list item 1") subSubList1Item1.addComponent(subSubList1Sentence1) # sub sub item 2 subSubList1Item2 = nlgFactory.createListItem() subSubList1Sentence2 = nlgFactory.createSentence("sub-sub-list item 2") subSubList1Item2.addComponent(subSubList1Sentence2) # sub sub list subSubList1 = nlgFactory.createEnumeratedList() subSubList1.addComponent(subSubList1Item1) subSubList1.addComponent(subSubList1Item2) # sub item 2 subList1Item2 = nlgFactory.createListItem() subList1Sentence2 = nlgFactory.createSentence("sub-list item 3") subList1Item2.addComponent(subList1Sentence2) # sub list 1 subList1 = nlgFactory.createEnumeratedList() subList1.addComponent(subList1Item1) subList1.addComponent(subSubList1) subList1.addComponent(subList1Item2) # item 2 item2 = nlgFactory.createListItem() sentence2 = nlgFactory.createSentence("item 2") item2.addComponent(sentence2) # item 3 item3 = nlgFactory.createListItem() sentence3 = nlgFactory.createSentence("item 3") item3.addComponent(sentence3) # list list_1 = nlgFactory.createEnumeratedList() list_1.addComponent(subList1) list_1.addComponent(item2) list_1.addComponent(item3) paragraph.addComponent(list_1) document.addComponent(paragraph) expectedOutput = "Document\n" + \ "\n" + \ "1.1 - Sub-list item 1.\n" + \ "1.2.1 - Sub-sub-list item 1.\n" + \ "1.2.2 - Sub-sub-list item 2.\n" + \ "1.3 - Sub-list item 3.\n"+ \ "2 - Item 2.\n" + \ "3 - Item 3.\n" + \ "\n\n" realisedOutput = realiser.realise(document).getRealisation() self.assertEquals(expectedOutput, realisedOutput)