/** * Creates an adjective phrase wrapping the given adjective. * * @param adjective * the main adjective for this phrase. * @return a <code>AdjPhraseSpec</code> representing this phrase. */ public virtual AdjPhraseSpec createAdjectivePhrase(object adjective) { AdjPhraseSpec phraseElement = new AdjPhraseSpec(this); NLGElement adjectiveElement = createNLGElement(adjective, new LexicalCategory(LexicalCategory.LexicalCategoryEnum.ADJECTIVE)); setPhraseHead(phraseElement, adjectiveElement); return(phraseElement); }
public virtual void testModalWithPassive() { NPPhraseSpec @object = phraseFactory.createNounPhrase("the", "pizza"); AdjPhraseSpec post = phraseFactory.createAdjectivePhrase("good"); AdvPhraseSpec @as = phraseFactory.createAdverbPhrase("as"); @as.addComplement(post); VPPhraseSpec verb = phraseFactory.createVerbPhrase("classify"); verb.addPostModifier(@as); verb.addComplement(@object); SPhraseSpec s = phraseFactory.createClause(); s.VerbPhrase = verb; s.setFeature(Feature.MODAL, "can"); // s.setFeature(Feature.FORM, Form.INFINITIVE); s.setFeature(Feature.PASSIVE, true); Assert.AreEqual("the pizza can be classified as good", realiser.realise(s).Realisation); }
protected virtual void setUp() { this.lexicon = Lexicon.getDefaultLexicon(); this.phraseFactory = new NLGFactory(this.lexicon); this.realiser = new Realiser(this.lexicon); this.man = this.phraseFactory.createNounPhrase("the", "man"); this.woman = this.phraseFactory.createNounPhrase("the", "woman"); this.dog = this.phraseFactory.createNounPhrase("the", "dog"); this.boy = this.phraseFactory.createNounPhrase("the", "boy"); this.beautiful = this.phraseFactory.createAdjectivePhrase("beautiful"); this.stunning = this.phraseFactory.createAdjectivePhrase("stunning"); this.salacious = this.phraseFactory.createAdjectivePhrase("salacious"); this.onTheRock = this.phraseFactory.createPrepositionPhrase("on"); this.np4 = this.phraseFactory.createNounPhrase("the", "rock"); this.onTheRock.addComplement(this.np4); this.behindTheCurtain = this.phraseFactory.createPrepositionPhrase("behind"); this.np5 = this.phraseFactory.createNounPhrase("the", "curtain"); this.behindTheCurtain.addComplement(this.np5); this.inTheRoom = this.phraseFactory.createPrepositionPhrase("in"); this.np6 = this.phraseFactory.createNounPhrase("the", "room"); this.inTheRoom.addComplement(this.np6); this.underTheTable = this.phraseFactory.createPrepositionPhrase("under"); this.underTheTable.addComplement(this.phraseFactory.createNounPhrase("the", "table")); this.proTest1 = this.phraseFactory.createNounPhrase("the", "singer"); this.proTest2 = this.phraseFactory.createNounPhrase("some", "person"); this.kick = this.phraseFactory.createVerbPhrase("kick"); this.kiss = this.phraseFactory.createVerbPhrase("kiss"); this.walk = this.phraseFactory.createVerbPhrase("walk"); this.talk = this.phraseFactory.createVerbPhrase("talk"); this.getUp = this.phraseFactory.createVerbPhrase("get up"); this.fallDown = this.phraseFactory.createVerbPhrase("fall down"); this.give = this.phraseFactory.createVerbPhrase("give"); this.say = this.phraseFactory.createVerbPhrase("say"); // the woman kisses the man s1 = this.phraseFactory.createClause(); s1.setSubject(this.woman); s1.setVerbPhrase(this.kiss); s1.setObject(this.man); // there is the dog on the rock s2 = this.phraseFactory.createClause(); s2.setSubject("there"); s2.setVerb("be"); s2.setObject(this.dog); s2.addPostModifier(this.onTheRock); // the man gives the woman John's flower s3 = this.phraseFactory.createClause(); s3.setSubject(this.man); s3.setVerbPhrase(this.give); var flower = this.phraseFactory.createNounPhrase("flower"); var john = this.phraseFactory.createNounPhrase("John"); john.setFeature(Feature.POSSESSIVE.ToString(), true); flower.setFeature(InternalFeature.SPECIFIER.ToString(), john); s3.setObject(flower); s3.setIndirectObject(this.woman); s4 = this.phraseFactory.createClause(); s4.setFeature(Feature.CUE_PHRASE.ToString(), "however"); s4.addFrontModifier("tomorrow"); var subject = this.phraseFactory .createCoordinatedPhrase(this.phraseFactory .createNounPhrase("Jane"), this.phraseFactory .createNounPhrase("Andrew")); s4.setSubject(subject); var pick = this.phraseFactory.createVerbPhrase("pick up"); s4.setVerbPhrase(pick); s4.setObject("the balls"); s4.addPostModifier("in the shop"); s4.setFeature(Feature.TENSE.ToString(), Tense.FUTURE); }
/** * Unwrap a <code>simplenlg.xmlrealiser.wrapper.NLGElement</code> and map it * to a <code>simplenlg.framework.NLGElement</code> * * @param wps * The wrapper object * @return the NLGElement */ public virtual NLGElement UnwrapNLGElement(wrapper.XmlNLGElement wps) { if (wps == null) { return(null); } if (wps is wrapper.XmlDocumentElement) { return((NLGElement)UnwrapDocumentElement((wrapper.XmlDocumentElement)wps)); } // Handle coordinate phrase specs first, which will cause recursion. NLGElement cp = UnwrapCoordinatePhraseSpec(wps); if (cp != null) { return(cp); } // Literal text. if (wps is wrapper.XmlStringElement) { wrapper.XmlStringElement wp = (wrapper.XmlStringElement)wps; NLGElement p = factory.createStringElement(wp.Val); return(p); } // WordElements (delegate to UnwrapWordElement) -- useful to have // because it is called by unWrapPhraseComponents, and pre/post mods // might be WordElements if (wps is XmlWordElement) { return(UnwrapWordElement((XmlWordElement)wps)); } // Sentence else if (wps is wrapper.XmlSPhraseSpec) { wrapper.XmlSPhraseSpec wp = (wrapper.XmlSPhraseSpec)wps; SPhraseSpec sp = factory.createClause(); NLGElement vp = null; List <NLGElement> subjects = new List <NLGElement>(); foreach (wrapper.XmlNLGElement p in wp.Subj) { NLGElement p1 = UnwrapNLGElement(p); checkFunction(DiscourseFunction.SUBJECT, p1); subjects.Add(p1); } if (subjects.Any()) { sp.setFeature(InternalFeature.SUBJECTS, subjects); } if (wp.Vp != null) { vp = UnwrapNLGElement(wp.Vp); sp.VerbPhrase = vp; } if (wp.CuePhrase != null) { NLGElement cue = UnwrapNLGElement(wp.CuePhrase); cue.setFeature(InternalFeature.DISCOURSE_FUNCTION, DiscourseFunction.CUE_PHRASE); sp.setFeature(Feature.CUE_PHRASE, cue); } if (wp.COMPLEMENTISER != null) { sp.setFeature(Feature.COMPLEMENTISER, wp.COMPLEMENTISER); } setSFeatures(wp, sp, vp); // Common phrase components. UnwrapPhraseComponents(sp, wps); return(sp); } // Phrases else if (wps is wrapper.XmlPhraseElement) { wrapper.XmlPhraseElement we = (wrapper.XmlPhraseElement)wps; PhraseElement hp = null; XmlWordElement w = we.Head; NLGElement head = UnwrapWordElement(w); // NLGElement head; // simplenlg.xmlrealiser.wrapper.XmlNLGElement w = // we.getHeadstring(); // check whether we have a stringelement or wordelement as head // if(w == null) { // w = we.getHeadword(); // head = UnwrapWordElement((XmlWordElement) w); // // } else { // head = factory.createStringElement(((XmlStringElement) // w).getVal()); // } // Noun Phrase if (wps is wrapper.XmlNPPhraseSpec) { wrapper.XmlNPPhraseSpec wp = (wrapper.XmlNPPhraseSpec)wps; NPPhraseSpec p = factory.createNounPhrase(head); hp = p; if (wp.Spec != null) { // p.setSpecifier(UnwrapWordElement(wp.getSpec())); wrapper.XmlNLGElement spec = wp.Spec; if (spec is XmlWordElement) { WordElement specifier = (WordElement)UnwrapWordElement((XmlWordElement)spec); if (specifier != null) { p.setSpecifier(specifier); } } else { p.setSpecifier(UnwrapNLGElement(spec)); } } setNPFeatures(wp, p); } // Adjective Phrase else if (wps is wrapper.XmlAdjPhraseSpec) { wrapper.XmlAdjPhraseSpec wp = (wrapper.XmlAdjPhraseSpec)wps; AdjPhraseSpec p = factory.createAdjectivePhrase(head); hp = p; p.setFeature(Feature.IS_COMPARATIVE, wp.ISCOMPARATIVE); p.setFeature(Feature.IS_SUPERLATIVE, wp.ISSUPERLATIVE); } // Prepositional Phrase else if (wps is wrapper.XmlPPPhraseSpec) { PPPhraseSpec p = factory.createPrepositionPhrase(head); hp = p; } // Adverb Phrase else if (wps is wrapper.XmlAdvPhraseSpec) { wrapper.XmlAdvPhraseSpec wp = (wrapper.XmlAdvPhraseSpec)wps; AdvPhraseSpec p = factory.createAdverbPhrase(); p.setHead(head); hp = p; p.setFeature(Feature.IS_COMPARATIVE, wp.ISCOMPARATIVE); p.setFeature(Feature.IS_SUPERLATIVE, wp.ISSUPERLATIVE); } // Verb Phrase else if (wps is wrapper.XmlVPPhraseSpec) { wrapper.XmlVPPhraseSpec wp = (wrapper.XmlVPPhraseSpec)wps; VPPhraseSpec p = factory.createVerbPhrase(head); hp = p; setVPFeatures(wp, p); } // Common phrase components. UnwrapPhraseComponents(hp, wps); // set the discourse function, if defined if (we.DiscourseFunction != null) { DiscourseFunction func; Enum.TryParse(we.DiscourseFunction.ToString(), out func); hp.setFeature(InternalFeature.DISCOURSE_FUNCTION, func); } // check the appositive feature bool?appositive = we.Appositive; if (appositive != null) { hp.setFeature(Feature.APPOSITIVE, appositive); } return(hp); } return(null); }
/** * @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); }