@Test public void dictionaryJSON() throws IOException { ARFFModel model = new MapBackedARFFModel(); ARFFVectorIterableTest.getVectors("sample-dense.arff", model); StringWriter writer = new StringWriter(); Driver.writeLabelBindingsJSON(writer, model); String expected1 = Resources.toString(Resources.getResource("expected-arff-schema.json"), Charsets.UTF_8); String expected2 = Resources.toString(Resources.getResource("expected-arff-schema-2.json"), Charsets.UTF_8); assertTrue(expected1.equals(writer.toString()) || expected2.equals(writer.toString())); }
protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = new ArrayList<>(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = new LinkedList<>(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = new HashMap<>(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); }
Driver { protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = new ArrayList<>(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = new LinkedList<>(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = new HashMap<>(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); } }
Driver { protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = new ArrayList<>(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = new LinkedList<>(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = new HashMap<>(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); } private  Driver(); }
Driver { protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = new ArrayList<>(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = new LinkedList<>(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = new HashMap<>(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); } private  Driver(); static void main(String[] args); }
Driver { protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = new ArrayList<>(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = new LinkedList<>(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = new HashMap<>(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); } private  Driver(); static void main(String[] args);  }