Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public virtual void section13_Test()
        {
            Lexicon    lexicon    = Lexicon.DefaultLexicon;  // default simplenlg lexicon
            NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon

            Realiser realiser = new Realiser(lexicon);

            SPhraseSpec s1 = nlgFactory.createClause("my cat", "like", "fish");
            SPhraseSpec s2 = nlgFactory.createClause("my dog", "like", "big bones");
            SPhraseSpec s3 = nlgFactory.createClause("my horse", "like", "grass");

            CoordinatedPhraseElement c = nlgFactory.createCoordinatedPhrase();

            c.addCoordinate(s1);
            c.addCoordinate(s2);
            c.addCoordinate(s3);

            string outputA = realiser.realiseSentence(c);

            Assert.AreEqual("My cat likes fish, my dog likes big bones and my horse likes grass.", outputA);

            SPhraseSpec p = nlgFactory.createClause("I", "be", "happy");
            SPhraseSpec q = nlgFactory.createClause("I", "eat", "fish");

            q.setFeature(Feature.COMPLEMENTISER, "because");
            q.setFeature(Feature.TENSE, Tense.PAST);
            p.addComplement(q);

            string outputB = realiser.realiseSentence(p);

            Assert.AreEqual("I am happy because I ate fish.", outputB);
        }
Esempio n. 3
0
        public virtual void multipleNLGElementListRealiserTest()
        {
            List <NLGElement> elements = new List <NLGElement>();
            // Create test NLGElements to realize:

            // "The cat jumping on the counter."
            DocumentElement sentence1 = nlgFactory.createSentence();
            NPPhraseSpec    subject_1 = nlgFactory.createNounPhrase("the", "cat");
            VPPhraseSpec    verb_1    = nlgFactory.createVerbPhrase("jump");

            verb_1.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE);
            PPPhraseSpec prep_1   = nlgFactory.createPrepositionPhrase();
            NPPhraseSpec object_1 = nlgFactory.createNounPhrase();

            object_1.setDeterminer("the");
            object_1.setNoun("counter");
            prep_1.addComplement(object_1);
            prep_1.setPreposition("on");
            SPhraseSpec clause_1 = nlgFactory.createClause();

            clause_1.setSubject(subject_1);
            clause_1.VerbPhrase = verb_1;
            clause_1.setObject(prep_1);
            sentence1.addComponent(clause_1);

            // "The dog running on the counter."
            DocumentElement sentence2 = nlgFactory.createSentence();
            NPPhraseSpec    subject_2 = nlgFactory.createNounPhrase("the", "dog");
            VPPhraseSpec    verb_2    = nlgFactory.createVerbPhrase("run");

            verb_2.setFeature(Feature.FORM, Form.PRESENT_PARTICIPLE);
            PPPhraseSpec prep_2   = nlgFactory.createPrepositionPhrase();
            NPPhraseSpec object_2 = nlgFactory.createNounPhrase();

            object_2.setDeterminer("the");
            object_2.setNoun("counter");
            prep_2.addComplement(object_2);
            prep_2.setPreposition("on");
            SPhraseSpec clause_2 = nlgFactory.createClause();

            clause_2.setSubject(subject_2);
            clause_2.VerbPhrase = verb_2;
            clause_2.setObject(prep_2);
            sentence2.addComponent(clause_2);


            elements.Add(sentence1);
            elements.Add(sentence2);

            IList <NLGElement> realisedElements = realiser.realise(elements);

            Assert.IsNotNull(realisedElements);
            Assert.AreEqual(2, realisedElements.Count);
            Assert.AreEqual("The cat jumping on the counter.", realisedElements[0].Realisation);
            Assert.AreEqual("The dog running on the counter.", realisedElements[1].Realisation);
        }
Esempio n. 4
0
        public virtual void xmlLexiconImmutabilityTest()
        {
            NLGFactory factory  = new NLGFactory(lexicon);
            Realiser   realiser = new Realiser(lexicon);

            // "wall" is singular.
            NPPhraseSpec wall = factory.createNounPhrase("the", "wall");

            Assert.AreEqual(NumberAgreement.SINGULAR, wall.getFeature(Feature.NUMBER));

            // Realise a sentence with plural form of "wall"
            wall.Plural = true;
            SPhraseSpec sentence = factory.createClause("motion", "observe");

            sentence.setFeature(Feature.TENSE, Tense.PAST);
            PPPhraseSpec pp = factory.createPrepositionPhrase("in", wall);

            sentence.addPostModifier(pp);
            realiser.realiseSentence(sentence);

            // Create a new 'the wall' NP and check to make sure that the syntax processor has
            // not propagated plurality to the canonical XMLLexicon WordElement object.
            wall = factory.createNounPhrase("the", "wall");
            Assert.AreEqual(NumberAgreement.SINGULAR, wall.getFeature(Feature.NUMBER));
        }
Esempio n. 5
0
        public virtual void section8_Test()
        {
            Lexicon    lexicon    = Lexicon.DefaultLexicon;  // default simplenlg lexicon
            NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon
            Realiser   realiser   = new Realiser(lexicon);

            NPPhraseSpec subject = nlgFactory.createNounPhrase("Mary");
            NPPhraseSpec @object = nlgFactory.createNounPhrase("the monkey");
            VPPhraseSpec verb    = nlgFactory.createVerbPhrase("chase");

            subject.addModifier("fast");

            SPhraseSpec p = nlgFactory.createClause();

            p.setSubject(subject);
            p.setVerb(verb);
            p.setObject(@object);

            string outputA = realiser.realiseSentence(p);

            Assert.AreEqual("Fast Mary chases the monkey.", outputA);

            verb.addModifier("quickly");

            string outputB = realiser.realiseSentence(p);

            Assert.AreEqual("Fast Mary quickly chases the monkey.", outputB);
        }
Esempio n. 6
0
        public void variantsTest()
        {
            var lexicon    = Lexicon.getDefaultLexicon(); // default simplenlg lexicon
            var nlgFactory = new NLGFactory(lexicon);     // factory based on lexicon
            var realiser   = new Realiser(lexicon);

            var p = nlgFactory.createClause();

            p.setSubject("my dog");
            p.setVerb("is"); // variant of be
            p.setObject("George");

            var output = realiser.realiseSentence(p);

            Assert.AreEqual("My dog is George.", output);

            p = nlgFactory.createClause();
            p.setSubject("my dog");
            p.setVerb("chases"); // variant of chase
            p.setObject("George");

            output = realiser.realiseSentence(p);
            Assert.AreEqual("My dog chases George.", output);


            p = nlgFactory.createClause();
            p.setSubject(nlgFactory.createNounPhrase("the", "dogs")); // variant of "dog"
            p.setVerb("is");                                          // variant of be
            p.setObject("happy");                                     // variant of happy
            output = realiser.realiseSentence(p);
            Assert.AreEqual("The dog is happy.", output);

            p = nlgFactory.createClause();
            p.setSubject(nlgFactory.createNounPhrase("the", "children")); // variant of "child"
            p.setVerb("is");                                              // variant of be
            p.setObject("happy");                                         // variant of happy
            output = realiser.realiseSentence(p);
            Assert.AreEqual("The child is happy.", output);

            // following functionality is enabled
            p = nlgFactory.createClause();
            p.setSubject(nlgFactory.createNounPhrase("the", "dogs")); // variant of "dog"
            p.setVerb("is");                                          // variant of be
            p.setObject("happy");                                     // variant of happy
            output = realiser.realiseSentence(p);
            Assert.AreEqual("The dog is happy.", output);             //corrected automatically
        }
        public override void RunCommand(object sender)
        {
            var engine = (IAutomationEngineInstance)sender;

            Lexicon     lexicon    = Lexicon.getDefaultLexicon();
            NLGFactory  nlgFactory = new NLGFactory(lexicon);
            SPhraseSpec p          = nlgFactory.createClause();

            p.AddAppInstance(engine, v_InstanceName);
        }
        public async override Task RunCommand(object sender)
        {
            var engine = (IAutomationEngineInstance)sender;

            Lexicon     lexicon    = Lexicon.getDefaultLexicon();
            NLGFactory  nlgFactory = new NLGFactory(lexicon);
            SPhraseSpec p          = nlgFactory.createClause();

            new OBAppInstance(v_InstanceName, p).SetVariableValue(engine, v_InstanceName);
        }
Esempio n. 9
0
        public virtual void section6A_Test()
        {
            Lexicon    lexicon    = Lexicon.DefaultLexicon;  // default simplenlg lexicon
            NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon
            Realiser   realiser   = new Realiser(lexicon);

            SPhraseSpec p = nlgFactory.createClause();

            p.setSubject("Mary");
            p.setVerb("chase");
            p.setObject("the monkey");

            p.setFeature(Feature.TENSE, Tense.PAST);
            string output = realiser.realiseSentence(p);

            Assert.AreEqual("Mary chased the monkey.", output);

            p.setFeature(Feature.TENSE, Tense.FUTURE);
            output = realiser.realiseSentence(p);
            Assert.AreEqual("Mary will chase the monkey.", output);

            p.setFeature(Feature.NEGATED, true);
            output = realiser.realiseSentence(p);
            Assert.AreEqual("Mary will not chase the monkey.", output);

            p = nlgFactory.createClause();
            p.setSubject("Mary");
            p.setVerb("chase");
            p.setObject("the monkey");

            p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.YES_NO);
            output = realiser.realiseSentence(p);
            Assert.AreEqual("Does Mary chase the monkey?", output);

            p.setSubject("Mary");
            p.setVerb("chase");
            p.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHO_OBJECT);
            output = realiser.realiseSentence(p);
            Assert.AreEqual("Who does Mary chase?", output);
        }
        public virtual void spellingVariantsInVPTest()
        {
            WordElement eth = (WordElement)factory.createWord("etherise",
                                                              new LexicalCategory(LexicalCategory.LexicalCategoryEnum.VERB));

            Assert.AreEqual("etherize", eth.DefaultSpellingVariant);
            eth.DefaultSpellingVariant = "etherise";
            Assert.AreEqual("etherise", eth.DefaultSpellingVariant);
            SPhraseSpec s = factory.createClause(factory.createNounPhrase("the", "doctor"), eth,
                                                 factory.createNounPhrase("the patient"));

            Assert.AreEqual("the doctor etherises the patient", realiser.realise(s).Realisation);
        }
Esempio n. 11
0
        public void section5A_Test()
        {
            var lexicon    = Lexicon.getDefaultLexicon(); // default simplenlg lexicon
            var nlgFactory = new NLGFactory(lexicon);     // factory based on lexicon
            var realiser   = new Realiser(lexicon);

            var p = nlgFactory.createClause();

            p.setSubject("Mary");
            p.setVerb("chase");
            p.setObject("the monkey");

            var output = realiser.realiseSentence(p);

            Assert.AreEqual("Mary chases the monkey.", output);
        } // testSection5A
Esempio n. 12
0
        public virtual void section5_Test()
        {
            Lexicon    lexicon    = Lexicon.DefaultLexicon;  // default simplenlg lexicon
            NLGFactory nlgFactory = new NLGFactory(lexicon); // factory based on lexicon
            Realiser   realiser   = new Realiser(lexicon);

            SPhraseSpec p = nlgFactory.createClause();

            p.setSubject("my dog");
            p.setVerb("chase");
            p.setObject("George");

            string output = realiser.realiseSentence(p);

            Assert.AreEqual("My dog chases George.", output);
        }
Esempio n. 13
0
        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();
        }
Esempio n. 14
0
        public virtual void testWhatObjectInterrogative()
        {
            Lexicon    lexicon  = Lexicon.DefaultLexicon;
            NLGFactory nlg      = new NLGFactory(lexicon);
            Realiser   realiser = new Realiser(lexicon);

            // Case 1, no object is explicitly given:
            SPhraseSpec  clause    = nlg.createClause("you", "think");
            PPPhraseSpec aboutJohn = nlg.createPrepositionPhrase("about", "John");

            clause.addPostModifier(aboutJohn);
            clause.setFeature(Feature.INTERROGATIVE_TYPE, InterrogativeType.WHAT_OBJECT);
            string realisation = realiser.realiseSentence(clause);

            Console.WriteLine(realisation);
            Assert.AreEqual("What do you think about John?", realisation);

            // Case 2:
            // Add "bad things" as the object so the object doesn't remain null:
            clause.setObject("bad things");
            realisation = realiser.realiseSentence(clause);
            Console.WriteLine(realiser.realiseSentence(clause));
            Assert.AreEqual("What do you think about John?", realisation);
        }
Esempio n. 15
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);
        }