Example usage for weka.core DenseInstance isMissing

List of usage examples for weka.core DenseInstance isMissing

Introduction

In this page you can find the example usage for weka.core DenseInstance isMissing.

Prototype

@Override
publicboolean isMissing(int attIndex) 

Source Link

Document

Tests if a specific value is "missing".

Usage

From source file:org.barcelonamedia.uima.CAS2WekaInstance.java

License:Open Source License

private static DenseInstance toWekaInternalInstance(List<AttributeValue> attributeValues,
        Instances wekaInstances) throws CASException {
    double[] zeroValues = new double[wekaInstances.numAttributes()];
    Arrays.fill(zeroValues, 0.0d);
    DenseInstance wekaInstance = new DenseInstance(1.0d, zeroValues);
    wekaInstance.setDataset(wekaInstances);

    Iterator<AttributeValue> attributeValuesIterator = attributeValues.iterator();

    while (attributeValuesIterator.hasNext()) {
        String value = null;/*  w  w  w .  ja  v  a2 s .  c  o  m*/
        String attributeName = null;

        AttributeValue attributeValue = attributeValuesIterator.next();
        attributeName = attributeValue.getAttributeName();
        Attribute attribute = wekaInstances.attribute(attributeName);
        if (attribute == null)
            continue;

        if (attributeValue instanceof NumericAttributeValue) {
            value = ((NumericAttributeValue) attributeValue).getValue();
            wekaInstance.setValue(attribute, Double.parseDouble(value));
        } else if (attributeValue instanceof DateAttributeValue) {
            //this isn't actually very smart.... I need to understand this better
            //any volunteers for the four lines of code I need here?
            value = ((DateAttributeValue) attributeValue).getValue();
            wekaInstance.setValue(attribute, value);
        } else if (attributeValue instanceof NominalAttributeValue) {
            value = ((NominalAttributeValue) attributeValue).getValue();
            int valueIndex = attribute.indexOfValue(value);
            wekaInstance.setValue(attribute, (double) valueIndex);
        } else if (attributeValue instanceof StringAttributeValue) {
            value = ((StringAttributeValue) attributeValue).getValue();
            wekaInstance.setValue(attribute, value);
        }
    }

    Enumeration attributes = wekaInstances.enumerateAttributes();
    while (attributes.hasMoreElements()) {
        Attribute attribute = (Attribute) attributes.nextElement();
        if (attribute.isNumeric() && wekaInstance.isMissing(attribute)) {
            wekaInstance.setValue(attribute, 0);
        }
    }

    return wekaInstance;
}