示例#1
0
        /**
         * The main method for realising coordinated phrases.
         *
         * @param parent
         *            the <code>SyntaxProcessor</code> that called this method.
         * @param phrase
         *            the <code>CoordinatedPhrase</code> to be realised.
         * @return the realised <code>NLGElement</code>.
         */

        public static INLGElement realise(SyntaxProcessor parent,
                                          CoordinatedPhraseElement phrase)
        {
            ListElement realisedElement = null;

            if (phrase != null)
            {
                realisedElement = new ListElement();
                PhraseHelper.realiseList(parent, realisedElement, phrase
                                         .getPreModifiers(), DiscourseFunction.PRE_MODIFIER);

                var coordinated = new CoordinatedPhraseElement();

                List <INLGElement> children = phrase.getChildren();
                var conjunction             = phrase.getFeatureAsString(Feature.CONJUNCTION.ToString());
                coordinated.setFeature(Feature.CONJUNCTION.ToString(), conjunction);
                coordinated.setFeature(Feature.CONJUNCTION_TYPE.ToString(), phrase
                                       .getFeature(Feature.CONJUNCTION_TYPE.ToString()));

                InflectedWordElement conjunctionElement = null;

                if (children != null && children.size() > 0)
                {
                    if (phrase.getFeatureAsBoolean(Feature.RAISE_SPECIFIER.ToString())
                        )
                    {
                        raiseSpecifier(children);
                    }

                    var child = phrase.getLastCoordinate();
                    if (child is SPhraseSpec)
                    {
                        ((SPhraseSpec)child).setFeature(Feature.POSSESSIVE.ToString(), phrase
                                                        .getFeature(Feature.POSSESSIVE.ToString()));
                    }
                    else
                    {
                        child.setFeature(Feature.POSSESSIVE.ToString(), phrase
                                         .getFeature(Feature.POSSESSIVE.ToString()));
                    }

                    child = children.get(0);

                    setChildFeatures(phrase, child);

                    coordinated.addCoordinate(parent.realise(child));
                    for (var index = 1; index < children.size(); index++)
                    {
                        child = children.get(index);
                        setChildFeatures(phrase, child);
                        if (child is SPhraseSpec)
                        {
                            if (phrase.getFeatureAsBoolean(Feature.AGGREGATE_AUXILIARY.ToString())
                                )
                            {
                                ((SPhraseSpec)child).setFeature(InternalFeature.REALISE_AUXILIARY.ToString(),
                                                                false);
                            }
                        }
                        else
                        {
                            if (phrase.getFeatureAsBoolean(Feature.AGGREGATE_AUXILIARY.ToString())
                                )
                            {
                                child.setFeature(InternalFeature.REALISE_AUXILIARY.ToString(),
                                                 false);
                            }
                        }

                        if (child.isA(PhraseCategoryEnum.CLAUSE))
                        {
                            ((SPhraseSpec)child)
                            .setFeature(
                                Feature.SUPRESSED_COMPLEMENTISER.ToString(),
                                phrase
                                .getFeature(Feature.SUPRESSED_COMPLEMENTISER.ToString()));
                        }

                        //skip conjunction if it's null or empty string
                        if (conjunction != null && conjunction.length() > 0)
                        {
                            conjunctionElement = new InflectedWordElement(
                                conjunction, new LexicalCategory_CONJUNCTION());
                            conjunctionElement.setFeature(
                                InternalFeature.DISCOURSE_FUNCTION.ToString(),
                                DiscourseFunction.CONJUNCTION);
                            coordinated.addCoordinate(conjunctionElement);
                        }

                        coordinated.addCoordinate(parent.realise(child));
                    }
                    realisedElement.addComponent(coordinated);
                }

                PhraseHelper.realiseList(parent, realisedElement, phrase
                                         .getPostModifiers(), DiscourseFunction.POST_MODIFIER);
                PhraseHelper.realiseList(parent, realisedElement, phrase
                                         .getComplements(), DiscourseFunction.COMPLEMENT);
            }
            return(realisedElement);
        }
示例#2
0
        public override INLGElement realise(INLGElement element)
        {
            INLGElement realisedElement = null;
            object      function        = null; //the element's discourse function

            //get the element's function first
            if (element is ListElement)
            {
                List <INLGElement> children = element.getChildren();
                if (!children.isEmpty())
                {
                    var firstChild = children.get(0);
                    function = firstChild.getFeature(InternalFeature.DISCOURSE_FUNCTION.ToString());
                }
            }
            else
            {
                if (element != null)
                {
                    function = element.getFeature(InternalFeature.DISCOURSE_FUNCTION.ToString());
                }
            }

            if (element != null)
            {
                var category = element.getCategory();

                if (category is IDocumentCategory && element is DocumentElement)
                {
                    var components = ((DocumentElement)element).getComponents();

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

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

                    default:
                        ((DocumentElement)element).setComponents(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
                    var buffer = new StringBuilder();

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

                        // TODO: unless this is the end of the sentence
                        if (all_appositives)
                        {
                            buffer.append(", ");
                        }
                        realiseList(buffer, element.getChildren(), this.commaSepPremodifiers ? "," : "");
                        if (all_appositives)
                        {
                            buffer.append(", ");
                        }
                    }
                    else if (DiscourseFunction.POST_MODIFIER.Equals(function))
                    {
// &&
                        // appositive)
                        // {
                        List <INLGElement> postmods = element.getChildren();
                        // bug fix due to Owen Bennett
                        var len = postmods.size();

                        for (var i = 0; i < len; i++)
                        {
                            // for(NLGElement postmod: element.getChildren()) {
                            var postmod = postmods.get(i);

                            // if the postmod is appositive, it's sandwiched in
                            // commas
                            if (postmod.getFeatureAsBoolean(Feature.APPOSITIVE.ToString()))
                            {
                                buffer.append(", ");
                                buffer.append(realise(postmod).ToString());

                                if (i < len - 1)
                                {
                                    buffer.append(", ");
                                }
                            }
                            else
                            {
                                buffer.append(realise(postmod).ToString());
                                if (postmod is ListElement ||
                                    (postmod.getRealisation() != null && !postmod.getRealisation().Equals("")))
                                {
                                    buffer.append(" ");
                                }
                            }
                        }
                    }
                    else if ((DiscourseFunction.CUE_PHRASE.Equals(function) ||
                              DiscourseFunction.FRONT_MODIFIER.Equals(function)) &&
                             this.commaSepCuephrase)
                    {
                        realiseList(buffer, element.getChildren(), this.commaSepCuephrase ? "," : "");
                    }
                    else
                    {
                        realiseList(buffer, element.getChildren(), "");
                    }

                    // realiseList(buffer, element.getChildren(), "");
                    realisedElement = new StringElement(buffer.ToString());
                }
                else if (element is CoordinatedPhraseElement)
                {
                    realisedElement = realiseCoordinatedPhrase(element.getChildren());
                }
                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.setCategory(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)) &&
                    this.commaSepCuephrase)
                {
                    var realisation = realisedElement.getRealisation();

                    if (!realisation.endsWith(","))
                    {
                        realisation = realisation + ",";
                    }

                    realisedElement.setRealisation(realisation);
                }
            }

            //remove preceding and trailing whitespace from internal punctuation
            removePunctSpace(realisedElement);
            return(realisedElement);
        }
示例#3
0
        public override INLGElement realise(INLGElement element)
        {
            //Debug.WriteLine($"realise single element {element}");
            INLGElement realisedElement = null;

            if (element != null && !element.getFeatureAsBoolean(Feature.ELIDED.ToString()))
            {
                if (element is DocumentElement)
                {
                    List <INLGElement> children = element.getChildren();
                    ((DocumentElement)element).setComponents(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
                                                                         .getChildren()));
                }
                else
                if (element is InflectedWordElement)
                {
                    var baseForm = ((InflectedWordElement)element).getBaseForm();
                    var category = element.getCategory();

                    if (lexicon != null && baseForm != null)
                    {
                        var word = ((InflectedWordElement)element).getBaseWord();

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

                        if (word != null)
                        {
                            ((InflectedWordElement)element).setBaseWord(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
                    var infl = new InflectedWordElement(
                        (WordElement)element);

                    // // the inflected word inherits all features from the base
                    // word
                    foreach (var feature in element.getAllFeatureNames())
                    {
                        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).getFirst();
                }
            }

            return(realisedElement);
        }