Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.framework;
import java.util.*;
import simplenlg.features.Feature;
import simplenlg.features.NumberAgreement;
import simplenlg.features.Tense;
/**
*
* NLGElement is the base class that all elements extend from. This
* is abstract and cannot therefore be instantiated itself. The additional
* element classes should be used to correctly identify the type of element
* required.
*
*
*
* Realisation in SimpleNLG revolves around a tree structure. Each node in the
* tree is represented by a NLGElement, which in turn may have
* child nodes. The job of the processors is to replace various types of
* elements with other elements. The eventual goal, once all the processors have
* been run, is to produce a single string element representing the final
* realisation.
*
*
*
* The features are stored in a Map of String (the
* feature name) and Object (the value of the feature).
*
*
* @author D. Westwater, University of Aberdeen.
* @version 4.0
*/
public abstract class NLGElement {
/**
* The category of this element.
*/
private ElementCategory category;
/**
* The features of this element.
*/
protected HashMap features = new HashMap();
/**
* The parent of this element.
*/
private NLGElement parent;
/**
* The realisation of this element.
*/
private String realisation;
/**
* The NLGFactory which created this element
*/
private NLGFactory factory;
/**
* Sets the category of this element.
*
* @param newCategory the new ElementCategory for this element.
*/
public void setCategory(ElementCategory newCategory) {
this.category = newCategory;
}
/**
* Retrieves the category for this element.
*
* @return the category as a ElementCategory.
*/
public ElementCategory getCategory() {
return this.category;
}
/**
* Adds a feature to the feature map. If the feature already exists then it
* is given the new value. If the value provided is null the
* feature is removed from the map.
*
* @param featureName the name of the feature.
* @param featureValue the new value of the feature or null if the
* feature is to be removed.
*/
public void setFeature(String featureName, Object featureValue) {
if(featureName != null) {
if(featureValue == null) {
this.features.remove(featureName);
} else {
this.features.put(featureName, featureValue);
}
}
}
/**
* A convenience method for setting boolean features.
*
* @param featureName the name of the feature.
* @param featureValue the boolean value of the feature.
*/
public void setFeature(String featureName, boolean featureValue) {
if(featureName != null) {
this.features.put(featureName, new Boolean(featureValue));
}
}
/**
* A convenience method for setting integer features.
*
* @param featureName the name of the feature.
* @param featureValue the int value of the feature.
*/
public void setFeature(String featureName, int featureValue) {
if(featureName != null) {
this.features.put(featureName, new Integer(featureValue));
}
}
/**
* A convenience method for setting long integer features.
*
* @param featureName the name of the feature.
* @param featureValue the long value of the feature.
*/
public void setFeature(String featureName, long featureValue) {
if(featureName != null) {
this.features.put(featureName, new Long(featureValue));
}
}
/**
* A convenience method for setting floating point number features.
*
* @param featureName the name of the feature.
* @param featureValue the float value of the feature.
*/
public void setFeature(String featureName, float featureValue) {
if(featureName != null) {
this.features.put(featureName, new Float(featureValue));
}
}
/**
* A convenience method for setting double precision floating point number
* features.
*
* @param featureName the name of the feature.
* @param featureValue the double value of the feature.
*/
public void setFeature(String featureName, double featureValue) {
if(featureName != null) {
this.features.put(featureName, new Double(featureValue));
}
}
/**
* Retrieves the value of the feature.
*
* @param featureName the name of the feature.
* @return the Object value of the feature.
*/
public Object getFeature(String featureName) {
return featureName != null ? this.features.get(featureName) : null;
}
/**
* Retrieves the value of the feature as a string. If the feature doesn't
* exist then null is returned.
*
* @param featureName the name of the feature.
* @return the String representation of the value. This is
* taken by calling the object's toString() method.
*/
public String getFeatureAsString(String featureName) {
Object value = getFeature(featureName);
String stringValue = null;
if(value != null) {
stringValue = value.toString();
}
return stringValue;
}
/**
*
* Retrieves the value of the feature as a list of elements. If the feature
* is a single NLGElement then it is wrapped in a list. If the
* feature is a Collection then each object in the collection
* is checked and only NLGElements are returned in the list.
*
*
* If the feature does not exist then an empty list is returned.
*
*
* @param featureName the name of the feature.
* @return the List of NLGElements
*/
public List getFeatureAsElementList(String featureName) {
List list = new ArrayList();
Object value = getFeature(featureName);
if(value instanceof NLGElement) {
list.add((NLGElement) value);
} else if(value instanceof Collection>) {
Iterator> iterator = ((Collection>) value).iterator();
Object nextObject = null;
while(iterator.hasNext()) {
nextObject = iterator.next();
if(nextObject instanceof NLGElement) {
list.add((NLGElement) nextObject);
}
}
}
return list;
}
/**
*
* Retrieves the value of the feature as a list of java objects. If the feature
* is a single element, the list contains only this element.
* If the feature is a Collection each object in the collection is
* returned in the list.
*
*
* If the feature does not exist then an empty list is returned.
*
*
* @param featureName the name of the feature.
* @return the List of Objects
*/
public List