Esempio n. 1
0
        /**
         * ~Set the features for a sentence. This method also checks whether any
         * features have been set on the VP, in which case, they are set if they
         * haven't been set on the S
         *
         * @param wp
         *            the xml SPhraseSpec object
         * @param sp
         *            the sentence.
         * @param vp
         *            the verb phrase.
         */
        private void setSFeatures(wrapper.XmlSPhraseSpec wp, SPhraseSpec sp, NLGElement vp)
        {
            if (wp.CLAUSESTATUS != null)
            {
                Enum.TryParse(wp.CLAUSESTATUS.ToString(), out ClauseStatus clauseStatus);
                sp.setFeature(InternalFeature.CLAUSE_STATUS, clauseStatus);
            }

            if (wp.PERSON != null)
            {
                Enum.TryParse(wp.PERSON.ToString(), out Person person);
                sp.setFeature(Feature.PERSON, person);
            }

            if (wp.FORM != null)
            {
                Enum.TryParse(wp.FORM.ToString(), out Form form);
                sp.setFeature(Feature.FORM, form);
            }

            if (wp.TENSE != null)
            {
                Enum.TryParse(wp.TENSE.ToString(), out Form tense);
                sp.setFeature(Feature.TENSE, tense);
            }
            else if (vp != null && vp.hasFeature(Feature.TENSE))
            {
                sp.setFeature(Feature.TENSE, vp.getFeature(Feature.TENSE));
            }

            // modal -- set on S or inherited from VP
            if (wp.MODAL != null)
            {
                sp.setFeature(Feature.MODAL, wp.MODAL);
            }
            else if (vp != null && vp.hasFeature(Feature.MODAL))
            {
                sp.setFeature(Feature.MODAL, vp.getFeature(Feature.MODAL));
            }

            // interrogative
            if (wp.INTERROGATIVETYPE != null)
            {
                Enum.TryParse(wp.INTERROGATIVETYPE.ToString(), out InterrogativeType interrogativeType);
                sp.setFeature(Feature.INTERROGATIVE_TYPE, interrogativeType);
            }
            else if (vp != null && vp.hasFeature(Feature.INTERROGATIVE_TYPE))
            {
                sp.setFeature(Feature.INTERROGATIVE_TYPE, vp.getFeature(Feature.INTERROGATIVE_TYPE));
            }

            // set on clauses.
            bool sAggregateAuxiliary = wp.AGGREGATEAUXILIARY ?? false;
            bool vAggregateAuxiliary = vp?.getFeatureAsBoolean(Feature.AGGREGATE_AUXILIARY) ?? false;

            sp.setFeature(Feature.AGGREGATE_AUXILIARY, sAggregateAuxiliary || vAggregateAuxiliary);

            // passive: can be set on S or VP
            bool sPass = wp.PASSIVE ?? false;
            bool vPass = vp?.getFeatureAsBoolean(Feature.PASSIVE) ?? false;

            sp.setFeature(Feature.PASSIVE, sPass || vPass);

            // progressive: can be set on S or VP
            bool sProg = wp.PROGRESSIVE ?? false;
            bool vProg = vp?.getFeatureAsBoolean(Feature.PROGRESSIVE) ?? false;

            sp.setFeature(Feature.PROGRESSIVE, sProg || vProg);

            // perfect: can be set on S or VP
            bool sPerf = wp.PERFECT ?? false;
            bool vPerf = vp?.getFeatureAsBoolean(Feature.PERFECT) ?? false;

            sp.setFeature(Feature.PERFECT, sPerf || vPerf);

            // negation: can be set on S or VP
            bool sNeg = wp.NEGATED ?? false;
            bool vNeg = vp?.getFeatureAsBoolean(Feature.NEGATED) ?? false;

            sp.setFeature(Feature.NEGATED, sNeg || vNeg);

            // set on clauses.
            bool ssgg = wp.SUPPRESSGENITIVEINGERUND ?? false;
            bool vsgg = vp?.getFeatureAsBoolean(Feature.SUPPRESS_GENITIVE_IN_GERUND) ?? false;

            sp.setFeature(Feature.SUPPRESS_GENITIVE_IN_GERUND, ssgg || vsgg);

            // set on clauses.
            bool ssc = wp.SUPRESSEDCOMPLEMENTISER ?? false;
            bool vsc = vp?.getFeatureAsBoolean(Feature.SUPRESSED_COMPLEMENTISER) ?? false;

            sp.setFeature(Feature.SUPRESSED_COMPLEMENTISER, ssc || vsc);
        }
Esempio n. 2
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);
        }