示例#1
0
        /**
         * Determines if the given phrase has an expletive as a subject.
         *
         * @param phrase
         *            the <code>PhraseElement</code> to be examined.
         * @return <code>true</code> if the phrase has an expletive subject.
         */
        public static bool isExpletiveSubject(PhraseElement phrase)
        {
            IList <NLGElement> subjects = phrase.getFeatureAsElementList(InternalFeature.SUBJECTS);
            bool expletive = false;

            if (subjects.Count == 1)
            {
                NLGElement subjectNP = subjects[0];

                if (subjectNP.isA(new PhraseCategory(PhraseCategory.PhraseCategoryEnum.NOUN_PHRASE)))
                {
                    expletive = subjectNP.getFeatureAsBoolean(LexicalFeature.EXPLETIVE_SUBJECT);
                }
                else if (subjectNP.isA(new PhraseCategory(PhraseCategory.PhraseCategoryEnum.CANNED_TEXT)))
                {
                    expletive = "there".Equals(subjectNP.Realisation, StringComparison.OrdinalIgnoreCase);                     //$NON-NLS-1$
                }
            }
            return(expletive);
        }
示例#2
0
        /**
         * Performs the realisation on a sentence. This includes adding the
         * terminator and capitalising the first letter.
         *
         * @param components
         *            the <code>List</code> of <code>NLGElement</code>s representing
         *            the components that make up the sentence.
         * @param element
         *            the <code>NLGElement</code> representing the sentence.
         * @return the realised element as an <code>NLGElement</code>.
         */
        private NLGElement realiseSentence(IList <NLGElement> components, NLGElement element)
        {
            NLGElement realisedElement = null;

            if (components != null && components.Any())
            {
                StringBuilder realisation = new StringBuilder();
                realiseList(realisation, components, "");

                stripLeadingCommas(realisation);
                capitaliseFirstLetter(realisation);
                terminateSentence(realisation, element.getFeatureAsBoolean(InternalFeature.INTERROGATIVE));

                ((DocumentElement)element).clearComponents();
                // realisation.append(' ');
                element.Realisation = realisation.ToString();
                realisedElement     = element;
            }

            return(realisedElement);
        }
示例#3
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);
        }
        public override NLGElement realise(NLGElement element)
        {
            NLGElement realisedElement = null;

            if (element != null && !element.getFeatureAsBoolean(Feature.ELIDED))
            {
                if (element is DocumentElement)
                {
                    IList <NLGElement> children = element.Children;
                    ((DocumentElement)element).Components = realise(children);
                    realisedElement = element;
                }
                else if (element is PhraseElement)
                {
                    realisedElement = realisePhraseElement((PhraseElement)element);
                }
                else if (element is ListElement)
                {
                    realisedElement = new ListElement();
                    ((ListElement)realisedElement).addComponents(realise(element.Children));
                }
                else if (element is InflectedWordElement)
                {
                    string          baseForm = ((InflectedWordElement)element).BaseForm;
                    ElementCategory category = element.Category;

                    if (lexicon != null && !ReferenceEquals(baseForm, null))
                    {
                        WordElement word = ((InflectedWordElement)element).BaseWord;

                        if (word == null)
                        {
                            if (category is LexicalCategory)
                            {
                                word = lexicon.lookupWord(baseForm, (LexicalCategory)category);
                            }
                            else
                            {
                                word = lexicon.lookupWord(baseForm);
                            }
                        }

                        if (word != null)
                        {
                            ((InflectedWordElement)element).BaseWord = word;
                        }
                    }

                    realisedElement = element;
                }
                else if (element is WordElement)
                {
                    // AG: need to check if it's a word element, in which case it
                    // needs to be marked for inflection
                    InflectedWordElement infl = new InflectedWordElement((WordElement)element);


                    // the inflected word inherits all features from the base word
                    foreach (string feature in element.AllFeatureNames)
                    {
                        infl.setFeature(feature, element.getFeature(feature));
                    }

                    realisedElement = realise(infl);
                }
                else if (element is CoordinatedPhraseElement)
                {
                    realisedElement = CoordinatedPhraseHelper.realise(this, (CoordinatedPhraseElement)element);
                }
                else
                {
                    realisedElement = element;
                }
            }

            // Remove the spurious ListElements that have only one element.
            if (realisedElement is ListElement)
            {
                if (((ListElement)realisedElement).size() == 1)
                {
                    realisedElement = ((ListElement)realisedElement).First;
                }
            }

            return(realisedElement);
        }
示例#5
0
        public override NLGElement realise(NLGElement element)
        {
            NLGElement realisedElement = null;
            object     function        = null;  //the element's discourse function

            //get the element's function first
            if (element is ListElement)
            {
                IList <NLGElement> children = element.Children;
                if (children.Any())
                {
                    NLGElement firstChild = children[0];
                    function = firstChild.getFeature(InternalFeature.DISCOURSE_FUNCTION);
                }
            }
            else
            {
                if (element != null)
                {
                    function = element.getFeature(InternalFeature.DISCOURSE_FUNCTION);
                }
            }

            if (element != null)
            {
                ElementCategory category = element.Category;

                if (category is DocumentCategory && element is DocumentElement)
                {
                    IList <NLGElement> components = ((DocumentElement)element).Components;

                    switch (((DocumentCategory)category).GetDocumentCategory())
                    {
                    case DocumentCategory.DocumentCategoryEnum.SENTENCE:
                        realisedElement = realiseSentence(components, element);
                        break;

                    case DocumentCategory.DocumentCategoryEnum.LIST_ITEM:
                        if (components != null && components.Any())
                        {
                            // recursively realise whatever is in the list item
                            // NB: this will realise embedded lists within list items
                            realisedElement        = new ListElement(realise(components));
                            realisedElement.Parent = element.Parent;
                        }
                        break;

                    default:
                        ((DocumentElement)element).Components = realise(components);
                        realisedElement = element;
                        break;
                    }
                }
                else if (element is ListElement)
                {
                    // AG: changes here: if we have a premodifier, then we ask the
                    // realiseList method to separate with a comma.
                    // if it's a postmod, we need commas at the start and end only
                    // if it's appositive
                    StringBuilder buffer = new StringBuilder();

                    if (DiscourseFunction.PRE_MODIFIER.Equals(function))
                    {
                        bool all_appositives = true;
                        foreach (NLGElement child in element.Children)
                        {
                            all_appositives = all_appositives && child.getFeatureAsBoolean(Feature.APPOSITIVE);
                        }

                        // TODO: unless this is the end of the sentence
                        if (all_appositives)
                        {
                            buffer.Append(", ");
                        }
                        realiseList(buffer, element.Children, commaSepPremodifiers ? "," : "");
                        if (all_appositives)
                        {
                            buffer.Append(", ");
                        }
                    }
                    else if (DiscourseFunction.POST_MODIFIER.Equals(function))
                    {                     // && appositive)
                        IList <NLGElement> postmods = element.Children;
                        // bug fix due to Owen Bennett
                        int len = postmods.Count;

                        for (int i = 0; i < len; i++)
                        {
                            // for(NLGElement postmod: element.getChildren()) {
                            NLGElement postmod = postmods[i];

                            // if the postmod is appositive, it's sandwiched in commas
                            if (postmod.getFeatureAsBoolean(Feature.APPOSITIVE))
                            {
                                buffer.Append(", ");
                                buffer.Append(realise(postmod));
                                buffer.Append(", ");
                            }
                            else
                            {
                                buffer.Append(realise(postmod));
                                if (postmod is ListElement || (postmod.Realisation != null && !postmod.Realisation.Equals("")))
                                {
                                    buffer.Append(" ");
                                }
                            }
                        }
                    }
                    else if ((DiscourseFunction.CUE_PHRASE.Equals(function) || DiscourseFunction.FRONT_MODIFIER.Equals(function)) && commaSepCuephrase)
                    {
                        realiseList(buffer, element.Children, commaSepCuephrase ? "," : "");
                    }
                    else
                    {
                        realiseList(buffer, element.Children, "");
                    }

                    // realiseList(buffer, element.getChildren(), "");
                    realisedElement = new StringElement(buffer.ToString());
                }
                else if (element is CoordinatedPhraseElement)
                {
                    realisedElement = realiseCoordinatedPhrase(element.Children);
                }
                else
                {
                    realisedElement = element;
                }

                // make the realised element inherit the original category
                // essential if list items are to be properly formatted later
                if (realisedElement != null)
                {
                    realisedElement.Category = category;
                }

                //check if this is a cue phrase; if param is set, postfix a comma
                if ((DiscourseFunction.CUE_PHRASE.Equals(function) || DiscourseFunction.FRONT_MODIFIER.Equals(function)) && commaSepCuephrase)
                {
                    string realisation = realisedElement.Realisation;

                    if (!realisation.EndsWith(",", StringComparison.Ordinal))
                    {
                        realisation = realisation + ",";
                    }

                    realisedElement.Realisation = realisation;
                }
                if (element.Capitalized)
                {
                    string realisation = realisedElement.Realisation;
                    realisedElement.Realisation = realisation.Substring(0, 1).ToUpper() + realisation.Substring(1);
                }
            }
            //remove preceding and trailing whitespace from internal punctuation
            removePunctSpace(realisedElement);
            return(realisedElement);
        }