DataConverter { public Instance convert(int id, String string) { int nball = dataset.nbAttributes() + dataset.getIgnored().length + 1; StringTokenizer tokenizer = new StringTokenizer(string, ", "); Preconditions.checkArgument(tokenizer.countTokens() == nball, "Wrong number of attributes in the string"); int nbattrs = dataset.nbAttributes(); DenseVector vector = new DenseVector(nbattrs); int aId = 0; int label = -1; for (int attr = 0; attr < nball; attr++) { String token = tokenizer.nextToken().trim(); if (ArrayUtils.contains(dataset.getIgnored(), attr)) { continue; } if ("?".equals(token)) { return null; } if (attr == dataset.getLabelId()) { label = dataset.labelCode(token); if (label == -1) { log.error("label token: {} dataset.labels: {}", token, Arrays.toString(dataset.labels())); throw new IllegalStateException("Label value (" + token + ") not known"); } } else if (dataset.isNumerical(aId)) { vector.set(aId++, Double.parseDouble(token)); } else { vector.set(aId, dataset.valueOf(aId, token)); aId++; } } if (label == -1) { log.error("Label not found, instance id : {}, string : {}", id, string); throw new IllegalStateException("Label not found!"); } return new Instance(id, vector, label); } DataConverter(Dataset dataset); Instance convert(int id, String string);  }
@Test public void testConvert() throws Exception { Random rng = RandomUtils.getRandom(); String descriptor = Utils.randomDescriptor(rng, ATTRIBUTE_COUNT); double[][] source = Utils.randomDoubles(rng, descriptor, INSTANCE_COUNT); String[] sData = Utils.double2String(source); Dataset dataset = DataLoader.generateDataset(descriptor, sData); Data data = DataLoader.loadData(dataset, sData); DataConverter converter = new DataConverter(dataset); for (int index = 0; index < data.size(); index++) { assertEquals(data.get(index), converter.convert(index, sData[index])); } }