Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public virtual void setUp()
        {
            lexicon = new XMLLexicon(); // built in lexicon

            phraseFactory = new NLGFactory(lexicon);
            realiser      = new Realiser(lexicon);

            man   = phraseFactory.createNounPhrase("the", "man");         //$NON-NLS-1$ //$NON-NLS-2$
            woman = phraseFactory.createNounPhrase("the", "woman");       //$NON-NLS-1$//$NON-NLS-2$
            dog   = phraseFactory.createNounPhrase("the", "dog");         //$NON-NLS-1$ //$NON-NLS-2$
            boy   = phraseFactory.createNounPhrase("the", "boy");         //$NON-NLS-1$ //$NON-NLS-2$

            beautiful = phraseFactory.createAdjectivePhrase("beautiful"); //$NON-NLS-1$
            stunning  = phraseFactory.createAdjectivePhrase("stunning");  //$NON-NLS-1$
            salacious = phraseFactory.createAdjectivePhrase("salacious"); //$NON-NLS-1$

            onTheRock = phraseFactory.createPrepositionPhrase("on");      //$NON-NLS-1$
            np4       = phraseFactory.createNounPhrase("the", "rock");    //$NON-NLS-1$ //$NON-NLS-2$
            onTheRock.addComplement(np4);

            behindTheCurtain = phraseFactory.createPrepositionPhrase("behind"); //$NON-NLS-1$
            np5 = phraseFactory.createNounPhrase("the", "curtain");             //$NON-NLS-1$ //$NON-NLS-2$
            behindTheCurtain.addComplement(np5);

            inTheRoom = phraseFactory.createPrepositionPhrase("in");   //$NON-NLS-1$
            np6       = phraseFactory.createNounPhrase("the", "room"); //$NON-NLS-1$ //$NON-NLS-2$
            inTheRoom.addComplement(np6);

            underTheTable = phraseFactory.createPrepositionPhrase("under");              //$NON-NLS-1$
            underTheTable.addComplement(phraseFactory.createNounPhrase("the", "table")); //$NON-NLS-1$ //$NON-NLS-2$

            proTest1 = phraseFactory.createNounPhrase("the", "singer");                  //$NON-NLS-1$ //$NON-NLS-2$
            proTest2 = phraseFactory.createNounPhrase("some", "person");                 //$NON-NLS-1$ //$NON-NLS-2$

            kick     = phraseFactory.createVerbPhrase("kick");                           //$NON-NLS-1$
            kiss     = phraseFactory.createVerbPhrase("kiss");                           //$NON-NLS-1$
            walk     = phraseFactory.createVerbPhrase("walk");                           //$NON-NLS-1$
            talk     = phraseFactory.createVerbPhrase("talk");                           //$NON-NLS-1$
            getUp    = phraseFactory.createVerbPhrase("get up");                         //$NON-NLS-1$
            fallDown = phraseFactory.createVerbPhrase("fall down");                      //$NON-NLS-1$
            give     = phraseFactory.createVerbPhrase("give");                           //$NON-NLS-1$
            say      = phraseFactory.createVerbPhrase("say");                            //$NON-NLS-1$
        }
Exemplo n.º 3
0
        /**
         * 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);
        }
Exemplo n.º 4
0
        /**
         * @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);
        }