@Test public void testGetLastOverlappingAnnotation() throws Exception { JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); jcas.setDocumentText("aaa aabb bbbb baa aa"); Token t1 = new Token(jcas, 0, 3); t1.addToIndexes(); Token t2 = new Token(jcas, 4, 8); t2.addToIndexes(); Token t3 = new Token(jcas, 9, 13); t3.addToIndexes(); Token t4 = new Token(jcas, 14, 18); t4.addToIndexes(); Token t5 = new Token(jcas, 19, 21); t5.addToIndexes(); EntityMention em = new EntityMention(jcas, 6, 15); em.addToIndexes(); Token result = JCoReAnnotationTools.getLastOverlappingAnnotation(jcas, em, Token.class); assertEquals(t4, result); }
@SuppressWarnings("unchecked") public static <T extends Annotation> T getLastOverlappingAnnotation(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.getEnd()) { cursor.moveToNext(); } if (!cursor.isValid()) cursor.moveToLast(); else cursor.moveToPrevious(); while (cursor.isValid()) { Annotation currentAnnotation = cursor.get(); if (!cls.isInstance(currentAnnotation)) { cursor.moveToPrevious(); continue; } if (cursor.isValid() && currentAnnotation.getBegin() < focusAnnotation.getEnd() && currentAnnotation.getEnd() > focusAnnotation.getBegin()) { return (T) currentAnnotation; } cursor.moveToPrevious(); } return null; }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> T getLastOverlappingAnnotation(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.getEnd()) { cursor.moveToNext(); } if (!cursor.isValid()) cursor.moveToLast(); else cursor.moveToPrevious(); while (cursor.isValid()) { Annotation currentAnnotation = cursor.get(); if (!cls.isInstance(currentAnnotation)) { cursor.moveToPrevious(); continue; } if (cursor.isValid() && currentAnnotation.getBegin() < focusAnnotation.getEnd() && currentAnnotation.getEnd() > focusAnnotation.getBegin()) { return (T) currentAnnotation; } cursor.moveToPrevious(); } return null; } }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> T getLastOverlappingAnnotation(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.getEnd()) { cursor.moveToNext(); } if (!cursor.isValid()) cursor.moveToLast(); else cursor.moveToPrevious(); while (cursor.isValid()) { Annotation currentAnnotation = cursor.get(); if (!cls.isInstance(currentAnnotation)) { cursor.moveToPrevious(); continue; } if (cursor.isValid() && currentAnnotation.getBegin() < focusAnnotation.getEnd() && currentAnnotation.getEnd() > focusAnnotation.getBegin()) { return (T) currentAnnotation; } cursor.moveToPrevious(); } return null; }  }
JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> T getLastOverlappingAnnotation(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.getEnd()) { cursor.moveToNext(); } if (!cursor.isValid()) cursor.moveToLast(); else cursor.moveToPrevious(); while (cursor.isValid()) { Annotation currentAnnotation = cursor.get(); if (!cls.isInstance(currentAnnotation)) { cursor.moveToPrevious(); continue; } if (cursor.isValid() && currentAnnotation.getBegin() < focusAnnotation.getEnd() && currentAnnotation.getEnd() > focusAnnotation.getBegin()) { return (T) currentAnnotation; } cursor.moveToPrevious(); } return null; }  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> T getLastOverlappingAnnotation(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.getEnd()) { cursor.moveToNext(); } if (!cursor.isValid()) cursor.moveToLast(); else cursor.moveToPrevious(); while (cursor.isValid()) { Annotation currentAnnotation = cursor.get(); if (!cls.isInstance(currentAnnotation)) { cursor.moveToPrevious(); continue; } if (cursor.isValid() && currentAnnotation.getBegin() < focusAnnotation.getEnd() && currentAnnotation.getEnd() > focusAnnotation.getBegin()) { return (T) currentAnnotation; } cursor.moveToPrevious(); } return null; }  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);  }