ArffDatasetAdapter implements IDatasetDeserializer<ILabeledDataset<ILabeledInstance>> { protected static IAttribute parseAttribute(final String line) throws UnsupportedAttributeTypeException { String attributeDefinitionSplit = line.replace("\\t", " ").substring(EArffItem.ATTRIBUTE.getValue().length() + 1).trim(); Matcher m = REG_EXP_ATTRIBUTE_DESCRIPTION.matcher(attributeDefinitionSplit); if (!m.find()) { throw new IllegalArgumentException("Cannot parse attribute definition: " + line); } String name = m.group(1); String type = m.group(2); name = name.trim(); if ((name.startsWith("'") && name.endsWith("'")) || (name.startsWith("\"") && name.endsWith("\""))) { name = name.substring(1, name.length() - 1); } EArffAttributeType attType; String[] values = null; if (type.startsWith("{") && type.endsWith("}")) { values = type.substring(1, type.length() - 1).split(SEPARATOR_DENSE_INSTANCE_VALUES); attType = EArffAttributeType.NOMINAL; } else { try { attType = EArffAttributeType.valueOf(type.toUpperCase()); } catch (IllegalArgumentException e) { throw new UnsupportedAttributeTypeException("The attribute type " + type.toUpperCase() + " is not supported in the EArffAttributeType ENUM. (line: " + line + ")"); } } switch (attType) { case NUMERIC: case REAL: case INTEGER: return new NumericAttribute(name); case STRING: return new StringAttribute(name); case TIMESERIES: return new SensorTimeSeriesAttribute(name); case NOMINAL: if (values != null) { return new IntBasedCategoricalAttribute(name, Arrays.stream(values).map(String::trim).map(x -> (((x.startsWith("'") && x.endsWith("'")) || x.startsWith("\"") && x.endsWith("\"")) ? x.substring(1, x.length() - 1) : x)).collect(Collectors.toList())); } else { throw new IllegalStateException("Identified a nominal attribute but it seems to have no values."); } default: throw new UnsupportedAttributeTypeException("Can not deal with attribute type " + type); } } ArffDatasetAdapter(final boolean sparseMode, final IDatasetDescriptor datasetDescriptor);  ArffDatasetAdapter(final boolean sparseMode);  ArffDatasetAdapter(); IAttribute getAttributeWithName(final IFileDatasetDescriptor datasetFile, final String nameOfAttribute); ILabeledDataset<ILabeledInstance> deserializeDataset(final IFileDatasetDescriptor datasetFile, final String nameOfClassAttribute); ILabeledDataset<ILabeledInstance> deserializeDataset(final IFileDatasetDescriptor datasetDescriptor, final int columnWithClassIndex); @Override ILabeledDataset<ILabeledInstance> deserializeDataset(final IDatasetDescriptor datasetDescriptor); ILabeledDataset<ILabeledInstance> deserializeDataset(); static ILabeledDataset<ILabeledInstance> readDataset(final File datasetFile); static ILabeledDataset<ILabeledInstance> readDataset(final boolean sparseMode, final File datasetFile); static ILabeledDataset<ILabeledInstance> readDataset(final boolean sparseMode, final File datasetFile, final int columnWithClassIndex); static void serializeDataset(final File arffOutputFile, final ILabeledDataset<? extends ILabeledInstance> data); static final String V_MISSING_VALUE; static final String K_RELATION_NAME; static final String K_CLASS_INDEX; }
@Test public void testParseNumericAttribute() throws UnsupportedAttributeTypeException { IAttribute attribute = ArffDatasetAdapter.parseAttribute(NUMERIC_ATTRIBUTE_STRING); assertTrue("Returned attribute is not of type INumericAttribtue", attribute instanceof INumericAttribute); assertEquals("Name of attribute could not be extracted correctly", ATTRIBUTE_NAME, attribute.getName()); }
@Test public void testParseCategoricalAttribute() throws UnsupportedAttributeTypeException { IAttribute attribute = ArffDatasetAdapter.parseAttribute(NOMINAL_ATTRIBUTE_STRING); assertTrue("Returned attribute is not of type INumericAttribtue", attribute instanceof ICategoricalAttribute); assertEquals("Name of attribute could not be extracted correctly", ATTRIBUTE_NAME, attribute.getName()); ICategoricalAttribute catAtt = (ICategoricalAttribute) attribute; assertEquals("Number of categories extracted does not match the real number of categories", CATEGORICAL_VALUES.size(), catAtt.getLabels().size()); assertTrue("Extracted list of categories does not contain all ground truth values", CATEGORICAL_VALUES.containsAll(catAtt.getLabels())); }
@Test public void testParseSensorTimeSeriesAttribute() throws UnsupportedAttributeTypeException { IAttribute attribute = ArffDatasetAdapter.parseAttribute(SENSOR_TIME_SERIES_ATTRIBUTE_STRING); assertTrue("Returned attribute is not of type IObjectAttribute", attribute instanceof IObjectAttribute); assertEquals("Name of attribute could not be extracted correctly", ATTRIBUTE_NAME, attribute.getName()); }