/** * Swaps eligible arguments in relations to address symmetry. * * @param newArgs * @return */ private static Map<Class<? extends Markable>, List<? extends Markable>> swapSymmetricalArgs( Map<Class<? extends Markable>, List<? extends Markable>> newArgs) { Map<Class<? extends Markable>, List<? extends Markable>> result = new HashMap<Class<? extends Markable>, List<? extends Markable>>(newArgs); Set<Class<? extends Markable>> labels = SYMMETRIC_CLASSES.keySet(); for (Class<? extends Markable> label : labels) { Class<? extends Markable> swapWithLabel = SYMMETRIC_CLASSES.get(label); List<? extends Markable> origMarkables = newArgs.get(label); Markable firstMarkable = JAXBUtil.unwrapFromList(origMarkables); // first element to swap if (firstMarkable == null) firstMarkable = createMarkable(label); Markable firstClone = JAXBUtil.copyObj(firstMarkable); origMarkables = newArgs.get(swapWithLabel); Markable secondMarkable = JAXBUtil.unwrapFromList(origMarkables); // second element to swap if (secondMarkable == null) secondMarkable = createMarkable(swapWithLabel); Markable secondClone = JAXBUtil.copyObj(secondMarkable); swap(firstClone, secondClone); // set those which received value from nulls to nulls. if (firstClone.getStart() == null) firstClone = null; if (secondClone.getStart() == null) secondClone = null; result.put(label, JAXBUtil.wrapToList(firstClone)); result.put(swapWithLabel, JAXBUtil.wrapToList(secondClone)); } return result; }
/** * @param arg1 * @param arg2 * @throws IllegalAccessException * @throws InstantiationException */ private static void swap(Markable arg1, Markable arg2) { BigInteger start = null, end = null; String text = null; start = arg1.getStart(); end = arg1.getEnd(); text = arg1.getText(); arg1.setStart(arg2.getStart()); arg1.setEnd(arg2.getEnd()); arg1.setText(arg2.getText()); arg2.setStart(start); arg2.setEnd(end); arg2.setText(text); }
/** * Validates markables: if the span start < end * * @param annotations * @param filename */ private static <T extends Comparable<? super T>> void validate( List<T> annotations, String filename) { for (T obj : annotations) { Markable m = (Markable) obj; if (m.getEnd().intValue() > -1 && m.getStart().intValue() > -1 && m.getStart().intValue() >= m.getEnd().intValue()) System.err.println( new IllegalArgumentException( String.format( "Wrong annotation span. File: %s Markable: %s Start:%s End:%s", filename, m.getClass().getSimpleName(), m.getStart().intValue(), m.getEnd().intValue()))); } }
/** * Retrieves all label of the textual span provided by the start and end offsets. The labels are * derived from JAXB-class names. * * @param startOffset - start offset of the span. * @param endOffset - end offset of the span. * @param annotations - list of annotations. * @return label */ private static <T extends Comparable<? super T>> String[] getLabels( int startOffset, int endOffset, List<T> annotations) { List<String> labels = null; if (annotations != null && annotations.size() != 0) { Markable annotation = null; annotation = (Markable) annotations.get( annotations.size() - 1); // Optimisation. Very last annotation in the text if (startOffset > annotation.getStart().intValue() && startOffset >= annotation.getEnd().intValue()) { /* System.out.println(String.format("Offset: %s-%s is farther than the very last annotation offset: %s-%s", startOffset, endOffset, annotation.getStart().intValue(), annotation.getEnd().intValue())); */ ; } else { for (T t : annotations) { annotation = (Markable) t; if (annotation.getStart().intValue() == -1 && annotation.getEnd().intValue() == -1) continue; else if (annotation.getStart().intValue() <= startOffset && annotation.getEnd().intValue() > endOffset) { if (labels == null) labels = new ArrayList<String>(); String label = annotation.getClass().getSimpleName(); label = truncateTo(label, MAX_LEN); labels.add(label); // save all labels available for this span } } } } String[] result = null; if (labels != null) { result = new String[labels.size()]; labels.toArray(result); } return result; }