JsonObject writeTextAnnotation(TextAnnotation ta, boolean doWriteTokenOffsets) { // get rid of the views that are empty Set<String> viewNames = new HashSet<>(ta.getAvailableViews()); for (String vu : viewNames) { if (ta.getView(vu) == null) { logger.warn("View " + vu + " is null"); ta.removeView(vu); } } JsonObject json = new JsonObject(); writeString("corpusId", ta.getCorpusId(), json); writeString("id", ta.getId(), json); writeString("text", ta.getText(), json); writeStringArray("tokens", ta.getTokens(), json); if (doWriteTokenOffsets) writeTokenOffsets(TOKENOFFSETS, ta.getView(ViewNames.TOKENS), json); writeSentences(ta, json); JsonArray views = new JsonArray(); for (String viewName : Sorters.sortSet(ta.getAvailableViews())) { if (viewName.equals(ViewNames.SENTENCE)) continue; JsonObject view = new JsonObject(); writeString("viewName", viewName, view); views.add(view); JsonArray viewData = new JsonArray(); List<View> topKViews = ta.getTopKViews(viewName); for (View topKView : topKViews) { JsonObject kView = new JsonObject(); writeView(topKView, kView); viewData.add(kView); } view.add("viewData", viewData); } json.add("views", views); writeAttributes(ta, json); return json; }
/** * Static method to remove views from `TextAnnotation` objects. Each task variant has a number of * views necessary for the solver to solve it. All other views should be removed. */ private static List<TextAnnotation> removeViews( List<TextAnnotation> textAnnotations, List<String> viewsToKeep) { if (viewsToKeep == null) { viewsToKeep = new ArrayList<String>(); } viewsToKeep = new ArrayList<>(viewsToKeep); List<String> viewsToRemove = new ArrayList<>(); List<TextAnnotation> cleansed = new ArrayList<>(); for (TextAnnotation textAnnotation : textAnnotations) { for (String viewName : textAnnotation.getAvailableViews()) { if (!viewsToKeep.contains(viewName)) { viewsToRemove.add(viewName); } } TextAnnotation cleansedAnnotation; try { cleansedAnnotation = (TextAnnotation) textAnnotation.clone(); } catch (CloneNotSupportedException ce) { cleansed.add(null); continue; } for (String viewName : viewsToRemove) { cleansedAnnotation.removeView(viewName); } cleansed.add(cleansedAnnotation); } return cleansed; }
/** * Removes every {@code Relation} from the {@code RELATIONVIEW} in the list of text annotations. */ private static List<TextAnnotation> removeRelationsFromPredicateArgumentView( List<TextAnnotation> uncleansedAnnotations) { List<String> relationExtractionViews = new ArrayList<>(); relationExtractionViews.add(ViewNames.SENTENCE); relationExtractionViews.add(ViewNames.TOKENS); relationExtractionViews.add("RELATIONVIEW"); List<TextAnnotation> textAnnotations = removeViews(uncleansedAnnotations, relationExtractionViews); for (TextAnnotation textAnnotation : textAnnotations) { Set<String> viewNames = textAnnotation.getAvailableViews(); for (String viewName : viewNames) { View view = textAnnotation.getView(viewName); if (view instanceof PredicateArgumentView) { PredicateArgumentView predicateArgumentView = (PredicateArgumentView) view; predicateArgumentView.removeAllRelations(); for (Constituent c : predicateArgumentView.getConstituents()) { predicateArgumentView.removeConstituent(c); int start = c.getStartSpan(); int end = c.getEndSpan(); view.addConstituent(new Constituent("", "RELATIONVIEW", textAnnotation, start, end)); } } } } return textAnnotations; }
/** Removes all coreference relations from {@code COREF} View. */ private static List<TextAnnotation> removeCoreferenceRelations( List<TextAnnotation> uncleansedAnnotations) { List<String> coreferenceViews = new ArrayList<>(); coreferenceViews.add(ViewNames.SENTENCE); coreferenceViews.add(ViewNames.TOKENS); coreferenceViews.add(ViewNames.COREF); List<TextAnnotation> textAnnotations = removeViews(uncleansedAnnotations, coreferenceViews); for (TextAnnotation textAnnotation : textAnnotations) { Set<String> viewNames = textAnnotation.getAvailableViews(); for (String viewName : viewNames) { View view = textAnnotation.getView(viewName); if (view instanceof CoreferenceView) { CoreferenceView coreferenceView = (CoreferenceView) view; coreferenceView.removeAllRelations(); textAnnotation.addView(viewName, coreferenceView); } } } return textAnnotations; }