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 testSection3(self): lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(lexicon) s1 = nlgFactory.createSentence("my dog is happy") r = Realiser(lexicon) output = r.realiseSentence(s1) self.assertEqual("My dog is happy.", output)
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 )
class RealiserTest(unittest.TestCase): def setUp(self): self.lexicon = Lexicon.getDefaultLexicon() self.nlgFactory = NLGFactory(self.lexicon) self.realiser = Realiser(self.lexicon) #self.realiser.setDebugMode(True) # Test the realization of List of NLGElements that is null def testEmptyNLGElementRealiser(self): elements = [] realisedElements = self.realiser.realise(elements) # Expect emtpy listed returned: self.assertIsNotNone(realisedElements) self.assertEqual(0, len(realisedElements)) # Test the realization of List of NLGElements that is null def testNullNLGElementRealiser(self): elements = None realisedElements = self.realiser.realise(elements) # Expect emtpy listed returned: self.assertIsNotNone(realisedElements) self.assertEqual(0, len(realisedElements)) # Tests the realization of multiple NLGElements in a list. def testMultipleNLGElementListRealiser(self): # "The cat jumping on the counter." sentence1 = self.nlgFactory.createSentence() subject_1 = self.nlgFactory.createNounPhrase("the", "cat") verb_1 = self.nlgFactory.createVerbPhrase("jump") verb_1.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE) prep_1 = self.nlgFactory.createPrepositionPhrase() object_1 = self.nlgFactory.createNounPhrase() object_1.setDeterminer("the") object_1.setNoun("counter") prep_1.addComplement(object_1) prep_1.setPreposition("on") clause_1 = self.nlgFactory.createClause() clause_1.setSubject(subject_1) clause_1.setVerbPhrase(verb_1) clause_1.setObject(prep_1) sentence1.addComponent(clause_1) # "The dog running on the counter." sentence2 = self.nlgFactory.createSentence() subject_2 = self.nlgFactory.createNounPhrase("the", "dog") verb_2 = self.nlgFactory.createVerbPhrase("run") verb_2.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE) prep_2 = self.nlgFactory.createPrepositionPhrase() object_2 = self.nlgFactory.createNounPhrase() object_2.setDeterminer("the") object_2.setNoun("counter") prep_2.addComplement(object_2) prep_2.setPreposition("on") clause_2 = self.nlgFactory.createClause() clause_2.setSubject(subject_2) clause_2.setVerbPhrase(verb_2) clause_2.setObject(prep_2) sentence2.addComponent(clause_2) # Create test NLGElements to realize: elements = [sentence1, sentence2] realisedElements = self.realiser.realise(elements) # self.assertIsNotNone(realisedElements) self.assertEqual(2, len(realisedElements)) self.assertEqual("The cat jumping on the counter.", realisedElements[0].getRealisation()) self.assertEqual("The dog running on the counter.", realisedElements[1].getRealisation()) # Tests the correct pluralization with possessives (GitHub issue #9) def testCorrectPluralizationWithPossessives(self): sisterNP = self.nlgFactory.createNounPhrase("sister") word = self.nlgFactory.createInflectedWord("Albert Einstein", LexicalCategory.NOUN) word.setFeature(LexicalFeature.PROPER, True) possNP = self.nlgFactory.createNounPhrase(word) possNP.setFeature(Feature.POSSESSIVE, True) sisterNP.setSpecifier(possNP) self.assertEqual("Albert Einstein's sister", self.realiser.realise(sisterNP).getRealisation()) sisterNP.setPlural(True) self.assertEqual("Albert Einstein's sisters", self.realiser.realise(sisterNP).getRealisation()) sisterNP.setPlural(False) possNP.setFeature(LexicalFeature.GENDER, Gender.MASCULINE) possNP.setFeature(Feature.PRONOMINAL, True) self.assertEqual("his sister", self.realiser.realise(sisterNP).getRealisation()) sisterNP.setPlural(True) self.assertEqual("his sisters", self.realiser.realise(sisterNP).getRealisation())
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)
from simplenlg.framework.LexicalCategory import * from simplenlg.framework.NLGFactory import * from simplenlg.realiser.english.Realiser import * lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(self.lexicon) realiser = Realiser(self.lexicon) nlgFactory.createSentence()
import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), "SimpleNLG-4.4.8.jar")) from simplenlg.framework import NLGFactory, CoordinatedPhraseElement, ListElement, PhraseElement from simplenlg.lexicon import Lexicon from simplenlg.realiser.english import Realiser from simplenlg.features import Feature, Tense, NumberAgreement from simplenlg.phrasespec import NPPhraseSpec from java.lang import Boolean lexicon = Lexicon.getDefaultLexicon() nlgFactory = NLGFactory(lexicon) realiser = Realiser(lexicon) print("hello world") p = nlgFactory.createSentence("my dog is happy") sentence = realiser.realiseSentence(p) print(sentence) p = nlgFactory.createClause() p.setSubject("Mary") p.setVerb("chase") p.setObject("the monkey") sentence2 = realiser.realiseSentence(p) print(sentence2)
class FeatureTest(SimpleNLG4Test): def setUp(self): super().setUp() self.docFactory = NLGFactory(self.lexicon) def tearDown(self): super().tearDown() self.docFactory = None # Tests use of the Possessive Feature. def testPossessiveFeature_PastTense(self): self.phraseFactory.setLexicon(self.lexicon) self.realiser.setLexicon(self.lexicon) # Create the pronoun 'she' she = self.phraseFactory.createWord("she",LexicalCategory.PRONOUN) # Set possessive on the pronoun to make it 'her' she.setFeature(Feature.POSSESSIVE, True) # Create a noun phrase with the subject lover and the determiner as she herLover = self.phraseFactory.createNounPhrase(she,"lover") # Create a clause to say 'he be her lover' clause = self.phraseFactory.createClause("he", "be", herLover) # Add the cue phrase need the comma as orthography # currently doesn't handle self. # This could be expanded to be a noun phrase with determiner # 'two' and noun 'week', set to plural and with a premodifier of # 'after' clause.setFeature(Feature.CUE_PHRASE, "after two weeks,") # Add the 'for a fortnight' as a post modifier. Alternatively # self could be added as a prepositional phrase 'for' with a # complement of a noun phrase ('a' 'fortnight') clause.addPostModifier("for a fortnight") # Set 'be' to 'was' as past tense clause.setFeature(Feature.TENSE,Tense.PAST) # Add the clause to a sentence.phraseFactory sentence1 = self.docFactory.createSentence(clause) # Realise the sentence realised = self.realiser.realise(sentence1) self.assertEqual("After two weeks, he was her lover for a fortnight.", \ realised.getRealisation()) # Basic tests. def testTwoPossessiveFeature_PastTense(self): self.phraseFactory.setLexicon(self.lexicon) # Create the pronoun 'she' she = self.phraseFactory.createWord("she",LexicalCategory.PRONOUN) # Set possessive on the pronoun to make it 'her' she.setFeature(Feature.POSSESSIVE, True) # Create a noun phrase with the subject lover and the determiner # as she herLover = self.phraseFactory.createNounPhrase(she,"lover") herLover.setPlural(True) # Create the pronoun 'he' he = self.phraseFactory.createNounPhrase(LexicalCategory.PRONOUN,"he") he.setPlural(True) # Create a clause to say 'they be her lovers' clause = self.phraseFactory.createClause(he, "be", herLover) clause.setFeature(Feature.POSSESSIVE, True) # Add the cue phrase need the comma as orthography # currently doesn't handle self. # This could be expanded to be a noun phrase with determiner # 'two' and noun 'week', set to plural and with a premodifier of # 'after' clause.setFeature(Feature.CUE_PHRASE, "after two weeks,") # Add the 'for a fortnight' as a post modifier. Alternatively # self could be added as a prepositional phrase 'for' with a # complement of a noun phrase ('a' 'fortnight') clause.addPostModifier("for a fortnight") # Set 'be' to 'was' as past tense clause.setFeature(Feature.TENSE,Tense.PAST) # Add the clause to a sentence. sentence1 = self.docFactory.createSentence(clause) # Realise the sentence realised = self.realiser.realise(sentence1) self.assertEqual("After two weeks, they were her lovers for a fortnight.", \ realised.getRealisation()) # Test use of the Complementiser feature by combining two S's using cue phrase and gerund. def testComplementiserFeature_PastTense(self): self.phraseFactory.setLexicon(self.lexicon) born = self.phraseFactory.createClause("Dave Bus", "be", "born") born.setFeature(Feature.TENSE,Tense.PAST) born.addPostModifier("in") born.setFeature(Feature.COMPLEMENTISER, "which") theHouse = self.phraseFactory.createNounPhrase("the", "house") theHouse.addComplement(born) clause = self.phraseFactory.createClause(theHouse, "be", \ self.phraseFactory.createPrepositionPhrase("in", "Edinburgh")) sentence = self.docFactory.createSentence(clause) realised = self.realiser.realise(sentence) # Retrieve the realisation and dump it to the console self.assertEqual("The house which Dave Bus was born in is in Edinburgh.", \ realised.getRealisation()) # Test use of the Complementiser feature in a CoordinatedPhraseElement by combine two S's using cue phrase and gerund. def testComplementiserFeatureInACoordinatePhrase_PastTense(self): self.phraseFactory.setLexicon(self.lexicon) dave = self.phraseFactory.createWord("Dave Bus", LexicalCategory.NOUN) albert = self.phraseFactory.createWord("Albert", LexicalCategory.NOUN) coord1 = CoordinatedPhraseElement(dave, albert) born = self.phraseFactory.createClause(coord1, "be", "born") born.setFeature(Feature.TENSE,Tense.PAST) born.addPostModifier("in") born.setFeature(Feature.COMPLEMENTISER, "which") theHouse = self.phraseFactory.createNounPhrase("the", "house") theHouse.addComplement(born) clause = self.phraseFactory.createClause(theHouse, "be", \ self.phraseFactory.createPrepositionPhrase("in", "Edinburgh")) sentence = self.docFactory.createSentence(clause) realised = self.realiser.realise(sentence) # Retrieve the realisation and dump it to the console self.assertEqual("The house which Dave Bus and Albert were born in is in Edinburgh.", \ realised.getRealisation()) # Test the use of the Progressive and Complementiser Features in future tense. def testProgressiveAndComplementiserFeatures_FutureTense(self): self.phraseFactory.setLexicon(self.lexicon) # Inner clause is 'I' 'make' 'sentence' 'for'. inner = self.phraseFactory.createClause("I","make", "sentence for") # Inner clause set to progressive. inner.setFeature(Feature.PROGRESSIVE,True) #Complementiser on inner clause is 'whom' inner.setFeature(Feature.COMPLEMENTISER, "whom") # create the engineer and add the inner clause as post modifier engineer = self.phraseFactory.createNounPhrase("the engineer") engineer.addComplement(inner) # Outer clause is: 'the engineer' 'go' (preposition 'to' 'holidays') outer = self.phraseFactory.createClause(engineer,"go", self.phraseFactory.createPrepositionPhrase("to","holidays")) # Outer clause tense is Future. outer.setFeature(Feature.TENSE, Tense.FUTURE) # Possibly progressive as well not sure. outer.setFeature(Feature.PROGRESSIVE,True) #Outer clause postmodifier would be 'tomorrow' outer.addPostModifier("tomorrow") sentence = self.docFactory.createSentence(outer) realised = self.realiser.realise(sentence) # Retrieve the realisation and dump it to the console self.assertEqual("The engineer whom I am making sentence for will be going to holidays tomorrow.", \ realised.getRealisation()) # Tests the use of the Complementiser, Passive, Perfect features in past tense. def testComplementiserPassivePerfectFeatures_PastTense(self): self.setUp() self.realiser.setLexicon(self.lexicon) inner = self.phraseFactory.createClause("I", "play", "poker") inner.setFeature(Feature.TENSE,Tense.PAST) inner.setFeature(Feature.COMPLEMENTISER, "where") house = self.phraseFactory.createNounPhrase("the", "house") house.addComplement(inner) outer = self.phraseFactory.createClause(None, "abandon", house) outer.addPostModifier("since 1986") outer.setFeature(Feature.PASSIVE, True) outer.setFeature(Feature.PERFECT, True) sentence = self.docFactory.createSentence(outer) realised = self.realiser.realise(sentence) # Retrieve the realisation and dump it to the console self.assertEqual("The house where I played poker has been abandoned since 1986.", \ realised.getRealisation()) # Tests the user of the progressive and complementiser featuers in past tense. def testProgressiveComplementiserFeatures_PastTense(self): self.phraseFactory.setLexicon(self.lexicon) sandwich = self.phraseFactory.createNounPhrase(LexicalCategory.NOUN, "sandwich") sandwich.setPlural(True) first = self.phraseFactory.createClause("I", "make", sandwich) first.setFeature(Feature.TENSE,Tense.PAST) first.setFeature(Feature.PROGRESSIVE,True) first.setPlural(False) second = self.phraseFactory.createClause("the mayonnaise", "run out") second.setFeature(Feature.TENSE,Tense.PAST) second.setFeature(Feature.COMPLEMENTISER, "when") first.addComplement(second) sentence = self.docFactory.createSentence(first) realised = self.realiser.realise(sentence) # Retrieve the realisation and dump it to the console self.assertEqual("I was making sandwiches when the mayonnaise ran out.", \ realised.getRealisation()) # Test the use of Passive in creating a Passive sentence structure: <Object> + [be] + <verb> + [by] + [Subject]. def testPassiveFeature(self): self.realiser.setLexicon(self.lexicon) phrase = self.phraseFactory.createClause("recession", "affect", "value") phrase.setFeature(Feature.PASSIVE, True) sentence = self.docFactory.createSentence(phrase) realised = self.realiser.realise(sentence) self.assertEqual("Value is affected by recession.", realised.getRealisation()) # Test for repetition of the future auxiliary "will", courtesy of Luxor Vlonjati def testFutureTense(self): test = self.phraseFactory.createClause() subj = self.phraseFactory.createNounPhrase("I") verb = self.phraseFactory.createVerbPhrase("go") adverb = self.phraseFactory.createAdverbPhrase("tomorrow") test.setSubject(subj) test.setVerbPhrase(verb) test.setFeature(Feature.TENSE, Tense.FUTURE) test.addPostModifier(adverb) sentence = self.realiser.realiseSentence(test) self.assertEqual("I will go tomorrow.", sentence) test2 = self.phraseFactory.createClause() vb = self.phraseFactory.createWord("go", LexicalCategory.VERB) test2.setSubject(subj) test2.setVerb(vb) test2.setFeature(Feature.TENSE, Tense.FUTURE) test2.addPostModifier(adverb) sentence2 = self.realiser.realiseSentence(test) self.assertEqual("I will go tomorrow.", sentence2)
# from simplenlg.phrasespec.SPhraseSpec import * # from simplenlg.phrasespec.VPPhraseSpec import * from simplenlg.realiser.english.Realiser import * from simplenlg.phrasespec import * # The hot resources : # https://pypi.org/project/simplenlg/ # https://github.com/bjascob/pySimpleNLG # https://github.com/simplenlg/simplenlg/wiki lexicon = Lexicon.getDefaultLexicon() factory = NLGFactory(lexicon) realiser = Realiser(lexicon) sentence1 = factory.createSentence() subject1 = factory.createNounPhrase('the', 'cat') sentence1.addComponent(subject1) # use addComponent not setSubject verb1 = factory.createVerbPhrase('jump') sentence1.addComponent(verb1) print(realiser.realise(sentence1)) # sentence1.setFeature(Feature.TENSE, Tense.PAST) # print(realiser.realise(sentence1)) -- don't work :( # Look - using a clause instead of a sentence made tense switching work : # the reason is because you can use setSubject on clauses, not sentences clause2 = factory.createClause() subject1 = factory.createNounPhrase('the', 'cat') clause2.setSubject(subject1) verb1 = factory.createVerbPhrase('jump') clause2.setVerb(verb1)
class StringElementTest(unittest.TestCase): def setUp(self): self.lexicon = Lexicon.getDefaultLexicon() self.phraseFactory = NLGFactory(self.lexicon) self.realiser = Realiser(self.lexicon) # Test that string elements can be used as heads of NP def testStringElementAsHead(self): np = self.phraseFactory.createNounPhrase() np.setHead(self.phraseFactory.createStringElement("dogs and cats")) np.setSpecifier( self.phraseFactory.createWord("the", LexicalCategory.DETERMINER)) self.assertEqual("the dogs and cats", self.realiser.realise(np).getRealisation()) # Sentences whose VP is a canned string def testStringElementAsVP(self): s = self.phraseFactory.createClause() s.setVerbPhrase( self.phraseFactory.createStringElement("eats and drinks")) s.setSubject(self.phraseFactory.createStringElement("the big fat man")) self.assertEqual("the big fat man eats and drinks", self.realiser.realise(s).getRealisation()) # Test for when the SPhraseSpec has a NPSpec added directly after it: # "Mary loves NP[the cow]." def testTailNPStringElement(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement( (self.phraseFactory.createStringElement("mary loves"))) np = self.phraseFactory.createNounPhrase() np.setHead("cow") np.setDeterminer("the") senSpec.addComplement(np) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("Mary loves the cow.", self.realiser.realise(completeSen).getRealisation()) # Test for a NP followed by a canned text: "NP[A cat] loves a dog". def testFrontNPStringElement(self): senSpec = self.phraseFactory.createClause() np = self.phraseFactory.createNounPhrase() np.setHead("cat") np.setDeterminer("the") senSpec.addComplement(np) senSpec.addComplement( self.phraseFactory.createStringElement("loves a dog")) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("The cat loves a dog.", \ self.realiser.realise(completeSen).getRealisation()) # Test for a StringElement followed by a NP followed by a StringElement # "The world loves NP[ABBA] but not a sore loser." def testMulitpleStringElements(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement( self.phraseFactory.createStringElement("the world loves")) np = self.phraseFactory.createNounPhrase() np.setHead("ABBA") senSpec.addComplement(np) senSpec.addComplement( self.phraseFactory.createStringElement("but not a sore loser")) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("The world loves ABBA but not a sore loser.", \ self.realiser.realise(completeSen).getRealisation()) # Test for multiple NP phrases with a single StringElement phrase: # "NP[John is] a trier NP[for cheese]." def testMulitpleNPElements(self): senSpec = self.phraseFactory.createClause() frontNoun = self.phraseFactory.createNounPhrase() frontNoun.setHead("john") senSpec.addComplement(frontNoun) senSpec.addComplement( self.phraseFactory.createStringElement("is a trier")) backNoun = self.phraseFactory.createNounPhrase() backNoun.setDeterminer("for") backNoun.setNoun("cheese") senSpec.addComplement(backNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("John is a trier for cheese.", \ self.realiser.realise(completeSen).getRealisation()) # White space check: Test to see how SNLG deals with additional whitespaces: # NP[The Nasdaq] rose steadily during NP[early trading], however it plummeted due to NP[a shock] after NP[IBM] announced poor # NP[first quarter results]. def testWhiteSpaceNP(self): senSpec = self.phraseFactory.createClause() firstNoun = self.phraseFactory.createNounPhrase() firstNoun.setDeterminer("the") firstNoun.setNoun("Nasdaq") senSpec.addComplement(firstNoun) senSpec.addComplement( self.phraseFactory.createStringElement(" rose steadily during ")) secondNoun = self.phraseFactory.createNounPhrase() secondNoun.setSpecifier("early") secondNoun.setNoun("trading") senSpec.addComplement(secondNoun) senSpec.addComplement( self.phraseFactory.createStringElement( " , however it plummeted due to")) thirdNoun = self.phraseFactory.createNounPhrase() thirdNoun.setSpecifier("a") thirdNoun.setNoun("shock") senSpec.addComplement(thirdNoun) senSpec.addComplement( self.phraseFactory.createStringElement(" after ")) fourthNoun = self.phraseFactory.createNounPhrase() fourthNoun.setNoun("IBM") senSpec.addComplement(fourthNoun) senSpec.addComplement( self.phraseFactory.createStringElement("announced poor ")) fifthNoun = self.phraseFactory.createNounPhrase() fifthNoun.setSpecifier("first quarter") fifthNoun.setNoun("results") fifthNoun.setPlural(True) senSpec.addComplement(fifthNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) correct = "The Nasdaq rose steadily during early trading, however it plummeted " \ + "due to a shock after IBM announced poor first quarter results." self.assertEqual(correct, self.realiser.realise(completeSen).getRealisation()) # Point absorption test: Check to see if SNLG respects abbreviations at the end of a sentence. # "NP[Yahya] was sleeping his own and dreaming etc." def testPointAbsorption(self): senSpec = self.phraseFactory.createClause() firstNoun = self.phraseFactory.createNounPhrase() firstNoun.setNoun("yaha") senSpec.addComplement(firstNoun) senSpec.addComplement("was sleeping on his own and dreaming etc.") completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("Yaha was sleeping on his own and dreaming etc.", \ self.realiser.realise(completeSen).getRealisation()) # Point absorption test: As above, but with trailing white space. # "NP[Yaha] was sleeping his own and dreaming etc. " def testPointAbsorptionTrailingWhiteSpace(self): senSpec = self.phraseFactory.createClause() firstNoun = self.phraseFactory.createNounPhrase() firstNoun.setNoun("yaha") senSpec.addComplement(firstNoun) senSpec.addComplement( "was sleeping on his own and dreaming etc. ") completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("Yaha was sleeping on his own and dreaming etc.", \ self.realiser.realise(completeSen).getRealisation()) # Abbreviation test: Check to see how SNLG deals with abbreviations in the middle of a sentence. # "NP[Yahya] and friends etc. went to NP[the park] to play." def testMiddleAbbreviation(self): senSpec = self.phraseFactory.createClause() firstNoun = self.phraseFactory.createNounPhrase() firstNoun.setNoun("yahya") senSpec.addComplement(firstNoun) senSpec.addComplement( self.phraseFactory.createStringElement("and friends etc. went to")) secondNoun = self.phraseFactory.createNounPhrase() secondNoun.setDeterminer("the") secondNoun.setNoun("park") senSpec.addComplement(secondNoun) senSpec.addComplement("to play") completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("Yahya and friends etc. went to the park to play.", \ self.realiser.realise(completeSen).getRealisation()) # Indefinite Article Inflection: StringElement to test how SNLG handles a/an situations. # "I see an NP[elephant]" def testStringIndefiniteArticleInflectionVowel(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement( self.phraseFactory.createStringElement("I see a")) firstNoun = self.phraseFactory.createNounPhrase("elephant") senSpec.addComplement(firstNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("I see an elephant.", self.realiser.realise(completeSen).getRealisation()) # Indefinite Article Inflection: StringElement to test how SNLG handles a/an situations. # "I see NP[a elephant]" --> def testNPIndefiniteArticleInflectionVowel(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement(self.phraseFactory.createStringElement("I see")) firstNoun = self.phraseFactory.createNounPhrase("elephant") firstNoun.setDeterminer("a") senSpec.addComplement(firstNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) self.assertEqual("I see an elephant.", self.realiser.realise(completeSen).getRealisation()) ''' # Not useful in python. Java code returns "I see an cow." (ie.. it doesn't change the article) # assertNotSame simply verifies that two objects do not refer to the same object. # Indefinite Article Inflection: StringElement to test how SNLG handles a/an situations. # "I see an NP[cow]" def testStringIndefiniteArticleInflectionConsonant(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement(self.phraseFactory.createStringElement("I see an")) firstNoun = self.phraseFactory.createNounPhrase("cow") senSpec.addComplement(firstNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) # Do not attempt "an" -> "a" self.assertNotSame("I see an cow.", self.realiser.realise(completeSen).getRealisation()) ''' # Indefinite Article Inflection: StringElement to test how SNLG handles a/an situations. # "I see NP[an cow]" --> def testNPIndefiniteArticleInflectionConsonant(self): senSpec = self.phraseFactory.createClause() senSpec.addComplement(self.phraseFactory.createStringElement("I see")) firstNoun = self.phraseFactory.createNounPhrase("cow") firstNoun.setDeterminer("an") senSpec.addComplement(firstNoun) completeSen = self.phraseFactory.createSentence() completeSen.addComponent(senSpec) # Do not attempt "an" -> "a" self.assertEqual("I see an cow.", self.realiser.realise(completeSen).getRealisation()) # aggregationStringElementTest: Test to see if we can aggregate two StringElements # in a CoordinatedPhraseElement. def testAggregationStringElement(self): coordinate = self.phraseFactory.createCoordinatedPhrase( \ StringElement("John is going to Tesco"), StringElement("Mary is going to Sainsburys")) sentence = self.phraseFactory.createClause() sentence.addComplement(coordinate) self.assertEqual("John is going to Tesco and Mary is going to Sainsburys.", \ self.realiser.realiseSentence(sentence)) # Tests that no empty space is added when a StringElement is instantiated with an empty string # or None object. def testNullAndEmptyStringElement(self): NoneStringElement = self.phraseFactory.createStringElement(None) emptyStringElement = self.phraseFactory.createStringElement("") beautiful = self.phraseFactory.createStringElement("beautiful") horseLike = self.phraseFactory.createStringElement("horse-like") creature = self.phraseFactory.createStringElement("creature") # Test1: None or empty at beginning test1 = self.phraseFactory.createClause("a unicorn", "be", "regarded as a") test1.addPostModifier(emptyStringElement) test1.addPostModifier(beautiful) test1.addPostModifier(horseLike) test1.addPostModifier(creature) self.assertEqual("A unicorn is regarded as a beautiful horse-like creature.", \ self.realiser.realiseSentence(test1)) # Test2: empty or None at end test2 = self.phraseFactory.createClause("a unicorn", "be", "regarded as a") test2.addPostModifier(beautiful) test2.addPostModifier(horseLike) test2.addPostModifier(creature) test2.addPostModifier(NoneStringElement) self.assertEqual("A unicorn is regarded as a beautiful horse-like creature.", \ self.realiser.realiseSentence(test2)) # Test3: empty or None in the middle test3 = self.phraseFactory.createClause("a unicorn", "be", "regarded as a") test3.addPostModifier("beautiful") test3.addPostModifier("horse-like") test3.addPostModifier("") test3.addPostModifier("creature") self.assertEqual("A unicorn is regarded as a beautiful horse-like creature.", \ self.realiser.realiseSentence(test3)) # Test4: empty or None in the middle with empty or None at beginning test4 = self.phraseFactory.createClause("a unicorn", "be", "regarded as a") test4.addPostModifier("") test4.addPostModifier("beautiful") test4.addPostModifier("horse-like") test4.addPostModifier(NoneStringElement) test4.addPostModifier("creature") self.assertEqual("A unicorn is regarded as a beautiful horse-like creature.", \ self.realiser.realiseSentence(test4))
class ExternalTest(unittest.TestCase): # called before each test def setUp(self): self.lexicon = Lexicon.getDefaultLexicon() self.phraseFactory = NLGFactory(self.lexicon) self.realiser = Realiser(self.lexicon) # Basic tests def testForcher(self): # Bjorn Forcher's tests self.phraseFactory.setLexicon(self.lexicon) s1 = self.phraseFactory.createClause(None, "associate", "Marie") s1.setFeature(Feature.PASSIVE, True) pp1 = self.phraseFactory.createPrepositionPhrase("with") pp1.addComplement("Peter") pp1.addComplement("Paul") s1.addPostModifier(pp1) self.assertEqual("Marie is associated with Peter and Paul", self.realiser.realise(s1).getRealisation()) s2 = self.phraseFactory.createClause() s2.setSubject(self.phraseFactory.createNounPhrase("Peter")) s2.setVerb("have") s2.setObject("something to do") s2.addPostModifier( self.phraseFactory.createPrepositionPhrase("with", "Paul")) self.assertEqual("Peter has something to do with Paul", self.realiser.realise(s2).getRealisation()) def testLu(self): # Xin Lu's test self.phraseFactory.setLexicon(self.lexicon) s1 = self.phraseFactory.createClause("we", "consider", "John") s1.addPostModifier("a friend") self.assertEqual("we consider John a friend", self.realiser.realise(s1).getRealisation()) def testDwight(self): # Rachel Dwight's test self.phraseFactory.setLexicon(self.lexicon) noun4 = self.phraseFactory.createNounPhrase("FGFR3 gene in every cell") noun4.setSpecifier("the") prep1 = self.phraseFactory.createPrepositionPhrase("of", noun4) noun1 = self.phraseFactory.createNounPhrase("the", "patient's mother") noun2 = self.phraseFactory.createNounPhrase("the", "patient's father") noun3 = self.phraseFactory.createNounPhrase("changed copy") noun3.addPreModifier("one") noun3.addComplement(prep1) coordNoun1 = CoordinatedPhraseElement(noun1, noun2) coordNoun1.setConjunction("or") verbPhrase1 = self.phraseFactory.createVerbPhrase("have") verbPhrase1.setFeature(Feature.TENSE, Tense.PRESENT) sentence1 = self.phraseFactory.createClause(coordNoun1, verbPhrase1, noun3) #realiser.setDebugMode(True) string = "the patient's mother or the patient's father has one changed copy of the FGFR3 gene in every cell" self.assertEqual(string, self.realiser.realise(sentence1).getRealisation()) # Rachel's second test noun3 = self.phraseFactory.createNounPhrase("a", "gene test") noun2 = self.phraseFactory.createNounPhrase("an", "LDL test") noun1 = self.phraseFactory.createNounPhrase("the", "clinic") verbPhrase1 = self.phraseFactory.createVerbPhrase("perform") coord1 = CoordinatedPhraseElement(noun2, noun3) sentence1 = self.phraseFactory.createClause(noun1, verbPhrase1, coord1) sentence1.setFeature(Feature.TENSE, Tense.PAST) self.assertEqual("the clinic performed an LDL test and a gene test", \ self.realiser.realise(sentence1).getRealisation()) def testNovelli(self): # Nicole Novelli's test p = self.phraseFactory.createClause("Mary", "chase", "George") pp = self.phraseFactory.createPrepositionPhrase("in", "the park") p.addPostModifier(pp) self.assertEqual("Mary chases George in the park", self.realiser.realise(p).getRealisation()) # another question from Nicole run = self.phraseFactory.createClause("you", "go", "running") run.setFeature(Feature.MODAL, "should") run.addPreModifier("really") think = self.phraseFactory.createClause("I", "think") think.setObject(run) run.setFeature(Feature.SUPRESSED_COMPLEMENTISER, True) text = self.realiser.realise(think).getRealisation() self.assertEqual("I think you should really go running", text) def testPiotrek(self): # Piotrek Smulikowski's test self.phraseFactory.setLexicon(self.lexicon) sent = self.phraseFactory.createClause("I", "shoot", "the duck") sent.setFeature(Feature.TENSE, Tense.PAST) loc = self.phraseFactory.createPrepositionPhrase( "at", "the Shooting Range") sent.addPostModifier(loc) sent.setFeature(Feature.CUE_PHRASE, "then") self.assertEqual("then I shot the duck at the Shooting Range", \ self.realiser.realise(sent).getRealisation()) def testPrescott(self): # Michael Prescott's test self.phraseFactory.setLexicon(self.lexicon) embedded = self.phraseFactory.createClause("Jill", "prod", "Spot") sent = self.phraseFactory.createClause("Jack", "see", embedded) embedded.setFeature(Feature.SUPRESSED_COMPLEMENTISER, True) embedded.setFeature(Feature.FORM, Form.BARE_INFINITIVE) self.assertEqual("Jack sees Jill prod Spot", self.realiser.realise(sent).getRealisation()) def testWissner(self): # Michael Wissner's text p = self.phraseFactory.createClause("a wolf", "eat") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_OBJECT) self.assertEqual("what does a wolf eat", self.realiser.realise(p).getRealisation()) def testPhan(self): # Thomas Phan's text subjectElement = self.phraseFactory.createNounPhrase("I") verbElement = self.phraseFactory.createVerbPhrase("run") prepPhrase = self.phraseFactory.createPrepositionPhrase("from") prepPhrase.addComplement("home") verbElement.addComplement(prepPhrase) newSentence = self.phraseFactory.createClause() newSentence.setSubject(subjectElement) newSentence.setVerbPhrase(verbElement) self.assertEqual("I run from home", self.realiser.realise(newSentence).getRealisation()) def testKerber(self): # Frederic Kerber's tests sp = self.phraseFactory.createClause("he", "need") secondSp = self.phraseFactory.createClause() secondSp.setVerb("build") secondSp.setObject("a house") secondSp.setFeature(Feature.FORM, Form.INFINITIVE) sp.setObject("stone") sp.addComplement(secondSp) self.assertEqual("he needs stone to build a house", self.realiser.realise(sp).getRealisation()) sp2 = self.phraseFactory.createClause("he", "give") sp2.setIndirectObject("I") sp2.setObject("the book") self.assertEqual("he gives me the book", self.realiser.realise(sp2).getRealisation()) def testStephenson(self): # Bruce Stephenson's test qs2 = self.phraseFactory.createClause() qs2.setSubject("moles of Gold") qs2.setVerb("are") qs2.setFeature(Feature.NUMBER, NumberAgreement.PLURAL) qs2.setFeature(Feature.PASSIVE, False) qs2.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.HOW_MANY) qs2.setObject("in a 2.50 g sample of pure Gold") sentence = self.phraseFactory.createSentence(qs2) self.assertEqual("How many moles of Gold are in a 2.50 g sample of pure Gold?", \ self.realiser.realise(sentence).getRealisation()) def testPierre(self): # John Pierre's test p = self.phraseFactory.createClause("Mary", "chase", "George") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_OBJECT) self.assertEqual("What does Mary chase?", self.realiser.realiseSentence(p)) p = self.phraseFactory.createClause("Mary", "chase", "George") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.YES_NO) self.assertEqual("Does Mary chase George?", self.realiser.realiseSentence(p)) p = self.phraseFactory.createClause("Mary", "chase", "George") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHERE) self.assertEqual("Where does Mary chase George?", self.realiser.realiseSentence(p)) p = self.phraseFactory.createClause("Mary", "chase", "George") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY) self.assertEqual("Why does Mary chase George?", self.realiser.realiseSentence(p)) p = self.phraseFactory.createClause("Mary", "chase", "George") p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.HOW) self.assertEqual("How does Mary chase George?", self.realiser.realiseSentence(p)) def testData2TextTest(self): # Data2Text tests # test OK to have number at end of sentence p = self.phraseFactory.createClause("the dog", "weigh", "12") self.assertEqual("The dog weighes 12.", self.realiser.realiseSentence(p)) # test OK to have "there be" sentence with "there" as a StringElement dataDropout2 = self.phraseFactory.createNLGElement("data dropouts") dataDropout2.setPlural(True) sentence2 = self.phraseFactory.createClause() sentence2.setSubject(self.phraseFactory.createStringElement("there")) sentence2.setVerb("be") sentence2.setObject(dataDropout2) self.assertEqual("There are data dropouts.", self.realiser.realiseSentence(sentence2)) # test OK to have gerund form verb weather1 = self.phraseFactory.createClause("SE 10-15", "veer", "S 15-20") weather1.setFeature(Feature.FORM, Form.GERUND) self.assertEqual("SE 10-15 veering S 15-20.", self.realiser.realiseSentence(weather1)) # test OK to have subject only weather2 = self.phraseFactory.createClause("cloudy and misty", "be", "XXX") weather2.getVerbPhrase().setFeature(Feature.ELIDED, True) self.assertEqual("Cloudy and misty.", self.realiser.realiseSentence(weather2)) # test OK to have VP only weather3 = self.phraseFactory.createClause("S 15-20", "increase", "20-25") weather3.setFeature(Feature.FORM, Form.GERUND) weather3.getSubject().setFeature(Feature.ELIDED, True) self.assertEqual("Increasing 20-25.", self.realiser.realiseSentence(weather3)) # conjoined test weather4 = self.phraseFactory.createClause("S 20-25", "back", "SSE") weather4.setFeature(Feature.FORM, Form.GERUND) weather4.getSubject().setFeature(Feature.ELIDED, True) coord = CoordinatedPhraseElement() coord.addCoordinate(weather1) coord.addCoordinate(weather3) coord.addCoordinate(weather4) coord.setConjunction("then") self.assertEqual( "SE 10-15 veering S 15-20, increasing 20-25 then backing SSE.", self.realiser.realiseSentence(coord)) # no verb weather5 = self.phraseFactory.createClause("rain", None, "likely") self.assertEqual("Rain likely.", self.realiser.realiseSentence(weather5)) @unittest.skip('aggregation not implemented') def testRafael(self): # Rafael Valle's tests ss = [] coord = ClauseCoordinationRule() coord.setFactory(self.phraseFactory) ss.add(self.agreePhrase("John Lennon")) # john lennon agreed with it ss.add(self.disagreePhrase( "Geri Halliwell")) # Geri Halliwell disagreed with it ss.add(self.commentPhrase("Melanie B")) # Mealnie B commented on it ss.add(self.agreePhrase("you")) # you agreed with it ss.add(self.commentPhrase("Emma Bunton")) #Emma Bunton commented on it results = coord.apply(ss) ret = [self.realiser.realise(e).getRealisation() for e in results] string = "[John Lennon and you agreed with it, Geri Halliwell disagreed with it, Melanie B and Emma Bunton commented on it]" self.assertEqual(string, ret.toString()) def commentPhrase(self, name): # used by testRafael s = self.phraseFactory.createClause() s.setSubject(self.phraseFactory.createNounPhrase(name)) s.setVerbPhrase(self.phraseFactory.createVerbPhrase("comment on")) s.setObject("it") s.setFeature(Feature.TENSE, Tense.PAST) return s def agreePhrase(self, name): # used by testRafael s = self.phraseFactory.createClause() s.setSubject(self.phraseFactory.createNounPhrase(name)) s.setVerbPhrase(self.phraseFactory.createVerbPhrase("agree with")) s.setObject("it") s.setFeature(Feature.TENSE, Tense.PAST) return s def disagreePhrase(self, name): # used by testRafael s = self.phraseFactory.createClause() s.setSubject(self.phraseFactory.createNounPhrase(name)) s.setVerbPhrase(self.phraseFactory.createVerbPhrase("disagree with")) s.setObject("it") s.setFeature(Feature.TENSE, Tense.PAST) return s @unittest.skip('aggregation not implemented') def testWkipedia(self): # test code fragments in wikipedia realisation subject = self.phraseFactory.createNounPhrase("the", "woman") subject.setPlural(True) sentence = self.phraseFactory.createClause(subject, "smoke") sentence.setFeature(Feature.NEGATED, True) self.assertEqual("The women do not smoke.", realiser.realiseSentence(sentence)) # aggregation s1 = self.phraseFactory.createClause("the man", "be", "hungry") s2 = self.phraseFactory.createClause("the man", "buy", "an apple") result = ClauseCoordinationRule().apply(s1, s2) self.assertEqual("The man is hungry and buys an apple.", realiser.realiseSentence(result)) def testLean(self): # A Lean's test sentence = self.phraseFactory.createClause() sentence.setVerb("be") sentence.setObject("a ball") sentence.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_SUBJECT) self.assertEqual("What is a ball?", self.realiser.realiseSentence(sentence)) sentence = self.phraseFactory.createClause() sentence.setVerb("be") object = self.phraseFactory.createNounPhrase("example") object.setPlural(True) object.addModifier("of jobs") sentence.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_SUBJECT) sentence.setObject(object) self.assertEqual("What are examples of jobs?", self.realiser.realiseSentence(sentence)) p = self.phraseFactory.createClause() sub1 = self.phraseFactory.createNounPhrase("Mary") sub1.setFeature(LexicalFeature.GENDER, Gender.FEMININE) sub1.setFeature(Feature.PRONOMINAL, True) sub1.setFeature(Feature.PERSON, Person.FIRST) p.setSubject(sub1) p.setVerb("chase") p.setObject("the monkey") output2 = self.realiser.realiseSentence(p) self.assertEqual("I chase the monkey.", output2) test = self.phraseFactory.createClause() subject = self.phraseFactory.createNounPhrase("Mary") subject.setFeature(Feature.PRONOMINAL, True) subject.setFeature(Feature.PERSON, Person.SECOND) test.setSubject(subject) test.setVerb("cry") test.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY) test.setFeature(Feature.TENSE, Tense.PRESENT) self.assertEqual("Why do you cry?", self.realiser.realiseSentence(test)) test = self.phraseFactory.createClause() subject = self.phraseFactory.createNounPhrase("Mary") subject.setFeature(Feature.PRONOMINAL, True) subject.setFeature(Feature.PERSON, Person.SECOND) test.setSubject(subject) test.setVerb("be") test.setObject("crying") test.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHY) test.setFeature(Feature.TENSE, Tense.PRESENT) self.assertEqual("Why are you crying?", self.realiser.realiseSentence(test)) def testKalijurand(self): # K Kalijurand's test lemma = "walk" word = self.lexicon.lookupWord(lemma, LexicalCategory.VERB) inflectedWord = InflectedWordElement(word) inflectedWord.setFeature(Feature.FORM, Form.PAST_PARTICIPLE) form = self.realiser.realise(inflectedWord).getRealisation() self.assertEqual("walked", form) inflectedWord = InflectedWordElement(word) inflectedWord.setFeature(Feature.PERSON, Person.THIRD) form = self.realiser.realise(inflectedWord).getRealisation() self.assertEqual("walks", form) def testLay(self): # Richard Lay's test lemma = "slap" word = self.lexicon.lookupWord(lemma, LexicalCategory.VERB) inflectedWord = InflectedWordElement(word) inflectedWord.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE) form = self.realiser.realise(inflectedWord).getRealisation() self.assertEqual("slapping", form) v = self.phraseFactory.createVerbPhrase("slap") v.setFeature(Feature.PROGRESSIVE, True) progressive = self.realiser.realise(v).getRealisation() self.assertEqual("is slapping", progressive)