![JAR search and dependency download from the Maven repository](/logo.png)
simplenlg.format.english.HTMLFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of SimpleNLG Show documentation
Show all versions of SimpleNLG Show documentation
Java API for Natural Language Generation
The newest version!
/*
* The contents of this file are subject to the Mozilla Public License
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* https://www.mozilla.org/en-US/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is "Simplenlg".
*
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
*
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Westwater, Roman Kutlak, Margaret Mitchell, and Saad Mahamood.
*/
package simplenlg.format.english;
import java.util.ArrayList;
import java.util.List;
import simplenlg.framework.*;
/**
*
* This processing module adds some simple plain HTML formatting to the
* SimpleNLG output. This includes the following:
*
* - Adding the document title to the beginning of the text.
* - Adding section titles in the relevant places.
* - Adding appropriate new line breaks for ease-of-reading.
* - Indenting list items with ' * '.
*
*
*
* @author D. Westwater, University of Aberdeen ~ for the TextFormatter;
*
J Christie, University of Aberdeen ~ for HTMLFormatter
* @version 4.0 original TextFormatter Version;
version 0.0 HTMLFormatter
*/
//public class TextFormatter extends NLGModule {
public class HTMLFormatter extends NLGModule {
// Modifications by James Christie to convert TextFormatter into a HTML Formatter
@Override
public void initialise() {
// Do nothing
} // constructor
@Override
public NLGElement realise(NLGElement element) { // realise a single element
NLGElement realisedComponent = null;
StringBuffer realisation = new StringBuffer();
if(element != null) {
ElementCategory category = element.getCategory();
List components = element.getChildren();
//NB: The order of the if-statements below is important!
// check if this is a canned text first
if(element instanceof StringElement) {
realisation.append(element.getRealisation());
} else if(category instanceof DocumentCategory) {
// && element instanceof DocumentElement
switch((DocumentCategory) category){
case DOCUMENT:
String title = element instanceof DocumentElement ? ((DocumentElement) element).getTitle() : null;
if(null != title) {
realisation.append("" + title + "
");
}
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
}
}
break;
case SECTION:
title = element instanceof DocumentElement ? ((DocumentElement) element).getTitle() : null;
if(title != null) {
String sectionTitle = ((DocumentElement) element).getTitle();
realisation.append("" + sectionTitle + "
");
}
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
}
}
break;
case LIST:
realisation.append("");
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
}
}
realisation.append("
");
break;
case ENUMERATED_LIST:
realisation.append("");
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
}
}
realisation.append("
");
break;
case PARAGRAPH:
if(null != components && 0 < components.size()) {
realisedComponent = realise(components.get(0));
if(realisedComponent != null) {
realisation.append("");
realisation.append(realisedComponent.getRealisation());
}
for(int i = 1; i < components.size(); i++) {
if(realisedComponent != null) {
realisation.append(" ");
}
realisedComponent = realise(components.get(i));
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
}
}
realisation.append("
");
}
break;
case SENTENCE:
realisation.append(element.getRealisation());
break;
case LIST_ITEM:
realisation.append("");
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation());
if(components.indexOf(eachComponent) < components.size() - 1) {
realisation.append(' ');
}
}
}
realisation.append(" ");
break;
}
// also need to check if element is a listelement (items can
// have embedded lists post-orthography) or a coordinate
} else if(element instanceof ListElement || element instanceof CoordinatedPhraseElement) {
for(NLGElement eachComponent : components) {
realisedComponent = realise(eachComponent);
if(realisedComponent != null) {
realisation.append(realisedComponent.getRealisation()).append(' ');
}
}
}
}
return new StringElement(realisation.toString());
} // realise ~ single element
@Override
public List realise(List elements) { // realise a list of elements
List realisedList = new ArrayList();
if(elements != null) {
for(NLGElement eachElement : elements) {
realisedList.add(realise(eachElement));
}
}
return realisedList;
} // realise ~ list of elements
} // class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy