@Test public void testIncludeAnnotationsOnDocument() throws Exception { JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); XmlCasDeserializer.deserialize(new FileInputStream("src/test/resources/10803599.xmi"), jcas.getCas()); FSIterator<Annotation> it = jcas.getAnnotationIndex(Abbreviation.type).iterator(); assertTrue(it.hasNext()); Abbreviation pthrpAbbr = (Abbreviation) it.next(); assertEquals("PTHrP", pthrpAbbr.getCoveredText()); Annotation fullform = pthrpAbbr.getTextReference(); assertEquals("PTH-related peptide", fullform.getCoveredText()); FSIterator<Annotation> geneIt = jcas.getAnnotationIndex(Gene.type).iterator(); assertTrue(geneIt.hasNext()); Gene gene = (Gene) geneIt.next(); List<ConceptMention> includedAnnotations = JCoReAnnotationTools.getIncludedAnnotations(jcas, fullform, ConceptMention.class); assertEquals(1, includedAnnotations.size()); Gene geneAnnotation = (Gene) includedAnnotations.get(0); assertEquals(gene, geneAnnotation); }
@SuppressWarnings("unchecked") public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation); if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); Annotation currentAnnotation = null; List<T> includedAnnotations = new ArrayList<>(); while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange)) includedAnnotations.add((T) currentAnnotation); cursor.moveToNext(); } return includedAnnotations; }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation); if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); Annotation currentAnnotation = null; List<T> includedAnnotations = new ArrayList<>(); while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange)) includedAnnotations.add((T) currentAnnotation); cursor.moveToNext(); } return includedAnnotations; } }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation); if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); Annotation currentAnnotation = null; List<T> includedAnnotations = new ArrayList<>(); while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange)) includedAnnotations.add((T) currentAnnotation); cursor.moveToNext(); } return includedAnnotations; }  }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation); if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); Annotation currentAnnotation = null; List<T> includedAnnotations = new ArrayList<>(); while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange)) includedAnnotations.add((T) currentAnnotation); cursor.moveToNext(); } return includedAnnotations; }  static Annotation getAnnotationByClassName(JCas aJCas, String fullAnnotationClassName); static Annotation getAnnotationAtOffset(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                   int endOffset); @SuppressWarnings("unchecked") static T getAnnotationAtMatchingOffsets(JCas aJCas, Annotation focusAnnotation,
                                                                          Class<T> cls); static Annotation getOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                      int endOffset); static Annotation getPartiallyOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName,
                                                               int startOffset, int endOffset); @SuppressWarnings("unchecked") static T getPartiallyOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                             Class<T> cls); @SuppressWarnings("unchecked") static List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls); @SuppressWarnings("unchecked") static T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getNearestIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                         Class<T> cls); static List<T> getNearestOverlappingAnnotations(JCas aJCas, int begin, int end,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static List<T> getNearestOverlappingAnnotations(JCas aJCas, Annotation focus,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getLastOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls); }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveTo(focusAnnotation); if (!cursor.isValid()) throw new IllegalArgumentException( "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation); while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) { cursor.moveToPrevious(); } if (!cursor.isValid()) cursor.moveToFirst(); else cursor.moveToNext(); Annotation currentAnnotation = null; List<T> includedAnnotations = new ArrayList<>(); while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) { if (!cls.isInstance(currentAnnotation)) { cursor.moveToNext(); continue; } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange)) includedAnnotations.add((T) currentAnnotation); cursor.moveToNext(); } return includedAnnotations; }  static Annotation getAnnotationByClassName(JCas aJCas, String fullAnnotationClassName); static Annotation getAnnotationAtOffset(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                   int endOffset); @SuppressWarnings("unchecked") static T getAnnotationAtMatchingOffsets(JCas aJCas, Annotation focusAnnotation,
                                                                          Class<T> cls); static Annotation getOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                      int endOffset); static Annotation getPartiallyOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName,
                                                               int startOffset, int endOffset); @SuppressWarnings("unchecked") static T getPartiallyOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                             Class<T> cls); @SuppressWarnings("unchecked") static List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls); @SuppressWarnings("unchecked") static T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getNearestIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                         Class<T> cls); static List<T> getNearestOverlappingAnnotations(JCas aJCas, int begin, int end,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static List<T> getNearestOverlappingAnnotations(JCas aJCas, Annotation focus,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getLastOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls);  }