public void section14_Test() { var lexicon = Lexicon.getDefaultLexicon(); // default simplenlg lexicon var nlgFactory = new NLGFactory(lexicon); // factory based on lexicon var realiser = new Realiser(lexicon); var p1 = nlgFactory.createClause("Mary", "chase", "the monkey"); var p2 = nlgFactory.createClause("The monkey", "fight back"); var p3 = nlgFactory.createClause("Mary", "be", "nervous"); var s1 = nlgFactory.createSentence(p1); var s2 = nlgFactory.createSentence(p2); var s3 = nlgFactory.createSentence(p3); var par1 = nlgFactory.createParagraph(new List <INLGElement> { s1, s2, s3 }); var output14a = realiser.realise(par1).getRealisation(); Assert.AreEqual("Mary chases the monkey. The monkey fights back. Mary is nervous.\n\n", output14a); var section = nlgFactory.createSection("The Trials and Tribulation of Mary and the Monkey"); section.addComponent(par1); var output14b = realiser.realise(section).getRealisation(); Assert.AreEqual( "The Trials and Tribulation of Mary and the Monkey\nMary chases the monkey. The monkey fights back. Mary is nervous.\n\n", output14b); }
public virtual void section14_Test() { Lexicon lexicon = Lexicon.DefaultLexicon; // default simplenlg lexicon NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon Realiser realiser = new Realiser(lexicon); SPhraseSpec p1 = nlgFactory.createClause("Mary", "chase", "the monkey"); SPhraseSpec p2 = nlgFactory.createClause("The monkey", "fight back"); SPhraseSpec p3 = nlgFactory.createClause("Mary", "be", "nervous"); DocumentElement s1 = nlgFactory.createSentence(p1); DocumentElement s2 = nlgFactory.createSentence(p2); DocumentElement s3 = nlgFactory.createSentence(p3); DocumentElement par1 = nlgFactory.createParagraph(new List <DocumentElement> { s1, s2, s3 }); string output14a = realiser.realise(par1).Realisation; Assert.AreEqual("Mary chases the monkey. The monkey fights back. Mary is nervous.\n\n", output14a); DocumentElement section = nlgFactory.createSection("The Trials and Tribulation of Mary and the Monkey"); section.addComponent(par1); string output14b = realiser.realise(section).Realisation; Assert.AreEqual( "The Trials and Tribulation of Mary and the Monkey\nMary chases the monkey. The monkey fights back. Mary is nervous.\n\n", output14b); }
public void enumeratedList() { var lexicon = Lexicon.getDefaultLexicon(); var nlgFactory = new NLGFactory(lexicon); var realiser = new Realiser(lexicon); realiser.setFormatter(new HTMLFormatter()); var document = nlgFactory.createDocument("Document"); var paragraph = nlgFactory.createParagraph(); var list = nlgFactory.createEnumeratedList(); var item1 = nlgFactory.createListItem(); var 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. var sentence1 = nlgFactory.createSentence("this", "be", "the first sentence"); var sentence2 = nlgFactory.createSentence("this", "be", "the second sentence"); item1.addComponent(sentence1); item2.addComponent(sentence2); list.addComponent(item1); list.addComponent(item2); paragraph.addComponent(list); document.addComponent(paragraph); var expectedOutput = "<h1>Document</h1>" + "<p>" + "<ol>" + "<li>This is the first sentence.</li>" + "<li>This is the second sentence.</li>" + "</ol>" + "</p>"; var realisedOutput = realiser.realise(document).getRealisation(); Assert.AreEqual(expectedOutput, realisedOutput); }
/** * Realise. * * @param wt * the wt * @return the string * @throws XMLRealiserException * the xML realiser exception */ public static string realise(wrapper.XmlDocumentElement wt) { string output = ""; if (wt != null) { try { if (lexicon == null) { lexicon = Lexicon.DefaultLexicon; } UnWrapper w = new UnWrapper(lexicon); DocumentElement t = w.UnwrapDocumentElement(wt); if (t != null) { Realiser r = new Realiser(lexicon); r.initialise(); NLGElement tr = r.realise(t); output = tr.Realisation; } } catch (Exception e) { throw new XMLRealiserException("NLG XMLRealiser Error", e); } } return output; }
public virtual void enumeratedList() { Lexicon lexicon = Lexicon.DefaultLexicon; NLGFactory nlgFactory = new NLGFactory(lexicon); Realiser realiser = new Realiser(lexicon); realiser.Formatter = new HTMLFormatter(); DocumentElement document = nlgFactory.createDocument("Document"); DocumentElement paragraph = nlgFactory.createParagraph(); DocumentElement list = nlgFactory.createEnumeratedList(); DocumentElement item1 = nlgFactory.createListItem(); DocumentElement item2 = nlgFactory.createListItem(); DocumentElement sentence1 = nlgFactory.createSentence("this", "be", "the first sentence"); DocumentElement sentence2 = nlgFactory.createSentence("this", "be", "the second sentence"); item1.addComponent(sentence1); item2.addComponent(sentence2); list.addComponent(item1); list.addComponent(item2); paragraph.addComponent(list); document.addComponent(paragraph); string expectedOutput = "<h1>Document</h1>" + "<p>" + "<ol>" + "<li>This is the first sentence.</li>" + "<li>This is the second sentence.</li>" + "</ol>" + "</p>"; string realisedOutput = realiser.realise(document).Realisation; Assert.AreEqual(expectedOutput, realisedOutput); }
public virtual void indefiniteWithPremodifierTest() { SPhraseSpec s = phraseFactory.createClause("there", "be"); s.setFeature(Feature.TENSE, Tense.PRESENT); NPPhraseSpec np = phraseFactory.createNounPhrase("a", "stenosis"); s.setObject(np); // check without modifiers -- article should be "a" Assert.AreEqual("there is a stenosis", realiser.realise(s).Realisation); // add a single modifier -- should turn article to "an" np.addPreModifier(phraseFactory.createAdjectivePhrase("eccentric")); Assert.AreEqual("there is an eccentric stenosis", realiser.realise(s).Realisation); }
public void testEnumeratedList() { var lexicon = Lexicon.getDefaultLexicon(); var nlgFactory = new NLGFactory(lexicon); var realiser = new Realiser(lexicon); realiser.setFormatter(new TextFormatter()); var document = nlgFactory.createDocument("Document"); var paragraph = nlgFactory.createParagraph(); var subListItem1 = nlgFactory.createListItem(); var subListSentence1 = nlgFactory.createSentence("this", "be", "sub-list sentence 1"); subListItem1.addComponent(subListSentence1); var subListItem2 = nlgFactory.createListItem(); var subListSentence2 = nlgFactory.createSentence("this", "be", "sub-list sentence 2"); subListItem2.addComponent(subListSentence2); var subList = nlgFactory.createEnumeratedList(); subList.addComponent(subListItem1); subList.addComponent(subListItem2); var item1 = nlgFactory.createListItem(); var sentence1 = nlgFactory.createSentence("this", "be", "the first sentence"); item1.addComponent(sentence1); var item2 = nlgFactory.createListItem(); var sentence2 = nlgFactory.createSentence("this", "be", "the second sentence"); item2.addComponent(sentence2); var list = nlgFactory.createEnumeratedList(); list.addComponent(subList); list.addComponent(item1); list.addComponent(item2); paragraph.addComponent(list); document.addComponent(paragraph); var 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 var realisedOutput = realiser.realise(document).getRealisation(); Assert.AreEqual(expectedOutput, realisedOutput); }
public virtual void testEnumeratedList() { Lexicon lexicon = Lexicon.DefaultLexicon; NLGFactory nlgFactory = new NLGFactory(lexicon); Realiser realiser = new Realiser(lexicon); realiser.Formatter = new TextFormatter(); DocumentElement document = nlgFactory.createDocument("Document"); DocumentElement paragraph = nlgFactory.createParagraph(); DocumentElement subListItem1 = nlgFactory.createListItem(); DocumentElement subListSentence1 = nlgFactory.createSentence("this", "be", "sub-list sentence 1"); subListItem1.addComponent(subListSentence1); DocumentElement subListItem2 = nlgFactory.createListItem(); DocumentElement subListSentence2 = nlgFactory.createSentence("this", "be", "sub-list sentence 2"); subListItem2.addComponent(subListSentence2); DocumentElement subList = nlgFactory.createEnumeratedList(); subList.addComponent(subListItem1); subList.addComponent(subListItem2); DocumentElement item1 = nlgFactory.createListItem(); DocumentElement sentence1 = nlgFactory.createSentence("this", "be", "the first sentence"); item1.addComponent(sentence1); DocumentElement item2 = nlgFactory.createListItem(); DocumentElement sentence2 = nlgFactory.createSentence("this", "be", "the second sentence"); item2.addComponent(sentence2); DocumentElement list = nlgFactory.createEnumeratedList(); list.addComponent(subList); list.addComponent(item1); list.addComponent(item2); paragraph.addComponent(list); document.addComponent(paragraph); string 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 string realisedOutput = realiser.realise(document).Realisation; Assert.AreEqual(expectedOutput, realisedOutput); }
public virtual void emptyNLGElementRealiserTest() { List <NLGElement> elements = new List <NLGElement>(); IList <NLGElement> realisedElements = realiser.realise(elements); // Expect emtpy listed returned: Assert.IsNotNull(realisedElements); Assert.AreEqual(0, realisedElements.Count); }
public static void Main(string[] args) { var ss = new XMLLexicon(); var Factory = new NLGFactory(ss); var Realiser = new Realiser(ss); // Instructions will be given to you by the director. var verbp = Factory.createVerbPhrase("be given"); verbp.setFeature(Feature.TENSE.ToString(), Tense.FUTURE); var subj = Factory.createNounPhrase("The Director"); var oobj = Factory.createNounPhrase("Instruction"); var ioobj = Factory.createNounPhrase("you"); subj.setPlural(false); oobj.setPlural(true); var s = new List <INLGElement>() { verbp, subj, oobj, ioobj }; var clause = Factory.createClause(); clause.setVerb(verbp); clause.setSubject(subj); clause.setObject(oobj); clause.setIndirectObject(ioobj); var sentence = Factory.createSentence(clause); sentence.setFeature(Feature.TENSE.ToString(), Tense.FUTURE); var active = Realiser.realise(sentence).ToString(); Console.WriteLine($"{active}"); Console.WriteLine("done"); Console.ReadLine(); }
public void testEnumeratedListWithSeveralLevelsOfNesting() { var lexicon = Lexicon.getDefaultLexicon(); var nlgFactory = new NLGFactory(lexicon); var realiser = new Realiser(lexicon); realiser.setFormatter(new TextFormatter()); var document = nlgFactory.createDocument("Document"); var paragraph = nlgFactory.createParagraph(); // sub item 1 var subList1Item1 = nlgFactory.createListItem(); var subList1Sentence1 = nlgFactory.createSentence("sub-list item 1"); subList1Item1.addComponent(subList1Sentence1); // sub sub item 1 var subSubList1Item1 = nlgFactory.createListItem(); var subSubList1Sentence1 = nlgFactory.createSentence("sub-sub-list item 1"); subSubList1Item1.addComponent(subSubList1Sentence1); // sub sub item 2 var subSubList1Item2 = nlgFactory.createListItem(); var subSubList1Sentence2 = nlgFactory.createSentence("sub-sub-list item 2"); subSubList1Item2.addComponent(subSubList1Sentence2); // sub sub list var subSubList1 = nlgFactory.createEnumeratedList(); subSubList1.addComponent(subSubList1Item1); subSubList1.addComponent(subSubList1Item2); // sub item 2 var subList1Item2 = nlgFactory.createListItem(); var subList1Sentence2 = nlgFactory.createSentence("sub-list item 3"); subList1Item2.addComponent(subList1Sentence2); // sub list 1 var subList1 = nlgFactory.createEnumeratedList(); subList1.addComponent(subList1Item1); subList1.addComponent(subSubList1); subList1.addComponent(subList1Item2); // item 2 var item2 = nlgFactory.createListItem(); var sentence2 = nlgFactory.createSentence("item 2"); item2.addComponent(sentence2); // item 3 var item3 = nlgFactory.createListItem(); var sentence3 = nlgFactory.createSentence("item 3"); item3.addComponent(sentence3); // list var list = nlgFactory.createEnumeratedList(); list.addComponent(subList1); list.addComponent(item2); list.addComponent(item3); paragraph.addComponent(list); document.addComponent(paragraph); var 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"; var realisedOutput = realiser.realise(document).getRealisation(); Assert.AreEqual(expectedOutput, realisedOutput); }
/** * @param args */ public static void Main(string[] args) { // below is a simple complete example of using simplenlg V4 // afterwards is an example of using simplenlg just for morphology // set up Lexicon lexicon = new XMLLexicon(); // default simplenlg lexicon NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon // create sentences // "John did not go to the bigger park. He played football there." NPPhraseSpec thePark = nlgFactory.createNounPhrase("the", "park"); // create an NP AdjPhraseSpec bigp = nlgFactory.createAdjectivePhrase("big"); // create AdjP bigp.setFeature(Feature.IS_COMPARATIVE, true); // use comparative form ("bigger") thePark.addModifier(bigp); // add adj as modifier in NP // above relies on default placement rules. You can force placement as a premodifier // (before head) by using addPreModifier PPPhraseSpec toThePark = nlgFactory.createPrepositionPhrase("to"); // create a PP toThePark.setObject(thePark); // set PP object // could also just say nlgFactory.createPrepositionPhrase("to", the Park); SPhraseSpec johnGoToThePark = nlgFactory.createClause("John", "go", toThePark); // create sentence johnGoToThePark.setFeature(Feature.TENSE, Tense.PAST); // set tense johnGoToThePark.setFeature(Feature.NEGATED, true); // set negated // note that constituents (such as subject and object) are set with setXXX methods // while features are set with setFeature DocumentElement sentence = nlgFactory.createSentence(johnGoToThePark); // below creates a sentence DocumentElement by concatenating strings StringElement hePlayed = new StringElement("he played"); StringElement there = new StringElement("there"); WordElement football = new WordElement("football"); DocumentElement sentence2 = nlgFactory.createSentence(); sentence2.addComponent(hePlayed); sentence2.addComponent(football); sentence2.addComponent(there); // now create a paragraph which contains these sentences DocumentElement paragraph = nlgFactory.createParagraph(); paragraph.addComponent(sentence); paragraph.addComponent(sentence2); // create a realiser. Note that a lexicon is specified, this should be // the same one used by the NLGFactory Realiser realiser = new Realiser(lexicon); //realiser.setDebugMode(true); // uncomment this to print out debug info during realisation NLGElement realised = realiser.realise(paragraph); Console.WriteLine(realised.Realisation); // end of main example // second example - using simplenlg just for morphology // below is clumsy as direct access to morphology isn't properly supported in V4.2 // hopefully will be better supported in later versions // get word element for "child" WordElement word = (WordElement)nlgFactory.createWord("child", new LexicalCategory(LexicalCategory.LexicalCategoryEnum.NOUN)); // create InflectedWordElement from word element InflectedWordElement inflectedWord = new InflectedWordElement(word); // set the inflected word to plural inflectedWord.Plural = true; // realise the inflected word string result = realiser.realise(inflectedWord).Realisation; Console.WriteLine(result); }
public virtual void forcherTest() { phraseFactory.Lexicon = lexicon; PhraseElement s1 = phraseFactory.createClause(null, "associate", "Marie"); s1.setFeature(Feature.PASSIVE, true); PhraseElement pp1 = phraseFactory.createPrepositionPhrase("with"); //$NON-NLS-1$ pp1.addComplement("Peter"); //$NON-NLS-1$ pp1.addComplement("Paul"); //$NON-NLS-1$ s1.addPostModifier(pp1); Assert.AreEqual("Marie is associated with Peter and Paul", realiser.realise(s1).Realisation); //$NON-NLS-1$ SPhraseSpec s2 = phraseFactory.createClause(); s2.setSubject(phraseFactory.createNounPhrase("Peter")); //$NON-NLS-1$ s2.setVerb("have"); //$NON-NLS-1$ s2.setObject("something to do"); //$NON-NLS-1$ s2.addPostModifier(phraseFactory.createPrepositionPhrase("with", "Paul")); //$NON-NLS-1$ //$NON-NLS-2$ Assert.AreEqual("Peter has something to do with Paul", realiser.realise(s2).Realisation); //$NON-NLS-1$ }
public virtual void stringElementAsHeadTest() { NPPhraseSpec np = phraseFactory.createNounPhrase(); np.setHead(phraseFactory.createStringElement("dogs and cats")); np.setSpecifier(phraseFactory.createWord("the", new LexicalCategory(LexicalCategory.LexicalCategoryEnum.DETERMINER))); Assert.AreEqual("the dogs and cats", realiser.realise(np).Realisation); }
public virtual void spellingVariantWithInflectionTest() { WordElement word = lexicon.getWord("formalization"); IList <string> spellVars = word.getFeatureAsStringList(LexicalFeature.SPELL_VARS); Assert.IsTrue(spellVars.Contains("formalisation")); Assert.AreEqual(Inflection.REGULAR, word.getDefaultInflectionalVariant()); // create with default spelling NPPhraseSpec np = factory.createNounPhrase("the", word); np.setFeature(Feature.NUMBER, NumberAgreement.PLURAL); Assert.AreEqual("the formalizations", realiser.realise(np).Realisation); // reset spell var word.DefaultSpellingVariant = "formalisation"; Assert.AreEqual("the formalisations", realiser.realise(np).Realisation); }
public virtual void beInflectionTest() { Realiser r = new Realiser(); WordElement word = lexicon.getWord("be", new LexicalCategory(LexicalCategory.LexicalCategoryEnum.VERB)); InflectedWordElement inflWord = new InflectedWordElement(word); inflWord.setFeature(Feature.PERSON, Person.FIRST); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("was", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.SECOND); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("were", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.THIRD); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("was", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.FIRST); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("am", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.SECOND); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("are", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.THIRD); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("is", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.NUMBER, NumberAgreement.PLURAL); inflWord.setFeature(Feature.PERSON, Person.FIRST); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("were", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.SECOND); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("were", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.THIRD); inflWord.setFeature(Feature.TENSE, Tense.PAST); Assert.AreEqual("were", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.FIRST); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("are", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.SECOND); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("are", r.realise(inflWord).ToString()); inflWord.setFeature(Feature.PERSON, Person.THIRD); inflWord.setFeature(Feature.TENSE, Tense.PRESENT); Assert.AreEqual("are", r.realise(inflWord).ToString()); }
public virtual void testEnumeratedListWithSeveralLevelsOfNesting() { Lexicon lexicon = Lexicon.DefaultLexicon; NLGFactory nlgFactory = new NLGFactory(lexicon); Realiser realiser = new Realiser(lexicon); realiser.Formatter = new TextFormatter(); DocumentElement document = nlgFactory.createDocument("Document"); DocumentElement paragraph = nlgFactory.createParagraph(); // sub item 1 DocumentElement subList1Item1 = nlgFactory.createListItem(); DocumentElement subList1Sentence1 = nlgFactory.createSentence("sub-list item 1"); subList1Item1.addComponent(subList1Sentence1); // sub sub item 1 DocumentElement subSubList1Item1 = nlgFactory.createListItem(); DocumentElement subSubList1Sentence1 = nlgFactory.createSentence("sub-sub-list item 1"); subSubList1Item1.addComponent(subSubList1Sentence1); // sub sub item 2 DocumentElement subSubList1Item2 = nlgFactory.createListItem(); DocumentElement subSubList1Sentence2 = nlgFactory.createSentence("sub-sub-list item 2"); subSubList1Item2.addComponent(subSubList1Sentence2); // sub sub list DocumentElement subSubList1 = nlgFactory.createEnumeratedList(); subSubList1.addComponent(subSubList1Item1); subSubList1.addComponent(subSubList1Item2); // sub item 2 DocumentElement subList1Item2 = nlgFactory.createListItem(); DocumentElement subList1Sentence2 = nlgFactory.createSentence("sub-list item 3"); subList1Item2.addComponent(subList1Sentence2); // sub list 1 DocumentElement subList1 = nlgFactory.createEnumeratedList(); subList1.addComponent(subList1Item1); subList1.addComponent(subSubList1); subList1.addComponent(subList1Item2); // item 2 DocumentElement item2 = nlgFactory.createListItem(); DocumentElement sentence2 = nlgFactory.createSentence("item 2"); item2.addComponent(sentence2); // item 3 DocumentElement item3 = nlgFactory.createListItem(); DocumentElement sentence3 = nlgFactory.createSentence("item 3"); item3.addComponent(sentence3); // list DocumentElement list = nlgFactory.createEnumeratedList(); list.addComponent(subList1); list.addComponent(item2); list.addComponent(item3); paragraph.addComponent(list); document.addComponent(paragraph); string 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"; string realisedOutput = realiser.realise(document).Realisation; Assert.AreEqual(expectedOutput, realisedOutput); }