@Override
  public NLGElement realise(NLGElement element) {
    NLGElement realisedElement = null;
    if (element instanceof InflectedWordElement) {
      realisedElement = doMorphology((InflectedWordElement) element);
    } else if (element instanceof StringElement) {
      realisedElement = element;
    } else if (element instanceof WordElement) {
      // AG: now retrieves the default spelling variant, not the baseform
      // String baseForm = ((WordElement) element).getBaseForm();
      String defaultSpell = ((WordElement) element).getDefaultSpellingVariant();

      if (defaultSpell != null) {
        realisedElement = new StringElement(defaultSpell);
      }
    } else if (element instanceof DocumentElement) {
      List<NLGElement> children = element.getChildren();
      ((DocumentElement) element).setComponents(realise(children));
      realisedElement = element;
    } else if (element instanceof ListElement) {
      realisedElement = new ListElement();
      ((ListElement) realisedElement).addComponents(realise(element.getChildren()));
    } else if (element instanceof CoordinatedPhraseElement) {
      List<NLGElement> children = element.getChildren();
      ((CoordinatedPhraseElement) element).clearCoordinates();

      if (children != null && children.size() > 0) {
        ((CoordinatedPhraseElement) element).addCoordinate(realise(children.get(0)));
        for (int index = 1; index < children.size(); index++) {
          ((CoordinatedPhraseElement) element).addCoordinate(realise(children.get(index)));
        }
        realisedElement = element;
      }
    } else if (element != null) {
      realisedElement = element;
    }
    return realisedElement;
  }
  @Override
  public NLGElement realise(NLGElement element) {
    NLGElement realisedElement = null;

    if (element != null && !element.getFeatureAsBoolean(Feature.ELIDED).booleanValue()) {

      if (element instanceof DocumentElement) {
        List<NLGElement> children = element.getChildren();
        ((DocumentElement) element).setComponents(realise(children));
        realisedElement = element;

      } else if (element instanceof PhraseElement) {
        realisedElement = realisePhraseElement((PhraseElement) element);

      } else if (element instanceof ListElement) {
        realisedElement = new ListElement();
        ((ListElement) realisedElement).addComponents(realise(element.getChildren()));

      } else if (element instanceof InflectedWordElement) {
        String baseForm = ((InflectedWordElement) element).getBaseForm();
        ElementCategory category = element.getCategory();

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

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

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

        realisedElement = element;

      } else if (element instanceof 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
        for (String feature : element.getAllFeatureNames()) {
          infl.setFeature(feature, element.getFeature(feature));
        }

        realisedElement = realise(infl);

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

      } else {
        realisedElement = element;
      }
    }

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