@SuppressWarnings("unchecked") public <T extends DocumentFeature> ArrayList<T> getFeatures(Class<T> classFilter) { ArrayList<T> items = new ArrayList<T>(); for (DocumentFeature feature : features) { if (classFilter.isAssignableFrom(feature.getClass())) items.add((T) feature); } return items; }
public static String getDocumentSetJson(Document doc) { try { JSONObject input = doc.getInputJson(); JSONObject aidr = input.getJSONObject("aidr"); // Add features if (!aidr.has("features")) { ArrayList<DocumentFeature> features = doc.getFeatures(DocumentFeature.class); JSONArray featureArray = new JSONArray(); for (DocumentFeature f : features) featureArray.put(f.toJSONObject()); aidr.put("features", featureArray); } // Add labels if (!aidr.has("nominal_labels")) { ArrayList<NominalLabelBC> labels = doc.getLabels(NominalLabelBC.class); JSONArray labelArray = new JSONArray(); if (!labels.isEmpty()) { // logger.info("labels field is non-empty"); for (NominalLabelBC l : labels) { try { JSONObject labelJson = getLabelJson(doc.crisisID.intValue(), l); labelArray.put(labelJson); // logger.info("Added label: " + l); } catch (RuntimeException e) { logger.error("Exception while converting document to JSON:" + l); logger.error(elog.toStringException(e)); } } } else { // logger.warn("Empty nominal_labels field! Inserting a dummy nominal label."); labelArray.put(createEmptyLabelJson()); } aidr.put("nominal_labels", labelArray); // logger.info("Added nominal_labels with size = " + labelArray.length()); } return unicodeEscaper.translate(input.toString()); } catch (JSONException e) { logger.error("Error in creating JSON from document: " + doc); logger.error(elog.toStringException(e)); throw new RuntimeException(e); } }