All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aima.core.learning.framework.DataSetSpecification Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

The newest version!
package aima.core.learning.framework;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Ravi Mohan
 * 
 */
public class DataSetSpecification {
	List attributeSpecifications;

	private String targetAttribute;

	public DataSetSpecification() {
		this.attributeSpecifications = new ArrayList();
	}

	public boolean isValid(List uncheckedAttributes) {
		if (attributeSpecifications.size() != uncheckedAttributes.size()) {
			throw new RuntimeException("size mismatch specsize = "
					+ attributeSpecifications.size() + " attrbutes size = "
					+ uncheckedAttributes.size());
		}
		Iterator attributeSpecIter = attributeSpecifications
				.iterator();
		Iterator valueIter = uncheckedAttributes.iterator();
		while (valueIter.hasNext() && attributeSpecIter.hasNext()) {
			if (!(attributeSpecIter.next().isValid(valueIter.next()))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @return Returns the targetAttribute.
	 */
	public String getTarget() {
		return targetAttribute;
	}

	public List getPossibleAttributeValues(String attributeName) {
		for (AttributeSpecification as : attributeSpecifications) {
			if (as.getAttributeName().equals(attributeName)) {
				return ((StringAttributeSpecification) as)
						.possibleAttributeValues();
			}
		}
		throw new RuntimeException("No such attribute" + attributeName);
	}

	public List getAttributeNames() {
		List names = new ArrayList();
		for (AttributeSpecification as : attributeSpecifications) {
			names.add(as.getAttributeName());
		}
		return names;
	}

	public void defineStringAttribute(String name, String[] attributeValues) {
		attributeSpecifications.add(new StringAttributeSpecification(name,
				attributeValues));
		setTarget(name);// target defaults to last column added
	}

	/**
	 * @param target
	 *            The targetAttribute to set.
	 */
	public void setTarget(String target) {
		this.targetAttribute = target;
	}

	public AttributeSpecification getAttributeSpecFor(String name) {
		for (AttributeSpecification spec : attributeSpecifications) {
			if (spec.getAttributeName().equals(name)) {
				return spec;
			}
		}
		throw new RuntimeException("no attribute spec for  " + name);
	}

	public void defineNumericAttribute(String name) {
		attributeSpecifications.add(new NumericAttributeSpecification(name));
	}

	public List getNamesOfStringAttributes() {
		List names = new ArrayList();
		for (AttributeSpecification spec : attributeSpecifications) {
			if (spec instanceof StringAttributeSpecification) {
				names.add(spec.getAttributeName());
			}
		}
		return names;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy