/* * Created on Apr 9, 2005 * */ package aima.learning.framework; import java.util.Hashtable; public class Example { Hashtable attributes; private Attribute targetAttribute; public Example(Hashtable attributes, Attribute targetAttribute) { this.attributes = attributes; this.targetAttribute = targetAttribute; } public String getAttributeValueAsString(String attributeName) { return attributes.get(attributeName).valueAsString(); } public double getAttributeValueAsDouble(String attributeName) { Attribute attribute = attributes.get(attributeName); if(attribute == null || !(attribute instanceof NumericAttribute) ){ throw new RuntimeException("cannot return numerical value for non numeric attribute"); } return ((NumericAttribute)attribute).valueAsDouble(); } public String toString() { return attributes.toString(); } public String targetValue() { return getAttributeValueAsString(targetAttribute.name()); } public boolean equals(Object o) { if (this == o) { return true; } if ((o == null) || (this.getClass() != o.getClass())) { return false; } Example other = (Example) o; return attributes.equals(other.attributes); } public int hashCode() { return attributes.hashCode(); } public Example numerize(Hashtable> attrValueToNumber) { Hashtable numerizedExampleData = new Hashtable(); for (String key : attributes.keySet()){ Attribute attribute = attributes.get(key); if ( attribute instanceof StringAttribute){ int correspondingNumber = attrValueToNumber.get(key).get(attribute.valueAsString()); NumericAttributeSpecification spec = new NumericAttributeSpecification(key); numerizedExampleData.put(key,new NumericAttribute((double)correspondingNumber,spec)); } else {//Numeric Attribute numerizedExampleData.put(key,attribute); } } return new Example(numerizedExampleData,numerizedExampleData.get(targetAttribute.name())); } }