Java Code Examples for org.apache.uima.jcas.tcas.Annotation#getBegin()

The following examples show how to use org.apache.uima.jcas.tcas.Annotation#getBegin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LLUnambiguousIteratorImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private static Annotation[] createItemsArray(LowLevelIterator<FeatureStructure> it) {
  List<Annotation> items = new ArrayList<>();
  int lastSeenEnd = 0;
  it.moveToFirst();
  // Iterate over the input iterator.
  while (it.isValid()) {
    FeatureStructure fs = it.nextNvc();
    if (!(fs instanceof Annotation)) {
      continue;  // skip until get an annotation
    }
    
    Annotation annot = (Annotation) fs;
    if (annot.getBegin() >= lastSeenEnd) {
      items.add(annot);
      lastSeenEnd = annot.getEnd();
    }
  }
  
  return items.toArray(new Annotation[items.size()]);    
}
 
Example 2
Source File: Subiterator.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param forward
 * @return true if iterator still valid, false if not valid
 */
private boolean adjustForStrictNvc_forward() {    
  if (isStrict) {
    Annotation item = it.getNvc();
    while (item.getEnd() > this.boundEnd) {
      
      it.moveToNextNvc();
      if (!isValid()) {
        return false;
      }
      item = it.getNvc();
      if (item.getBegin() > this.boundEnd) { // not >= because could of 0 length annot at end
        makeInvalid();
        return false;
      }
      
    }
    return true;
  } else {
    return true;
  }
}
 
Example 3
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 6 votes vote down vote up
/**
 * @param annot1
 * @param annot2
 * @return the distance (gap) between these two annotation, or -1 if they
 *         overlap
 */
public static int distance(Annotation a1, Annotation a2) {

    // a1 then a2
    int dist = a2.getBegin() - a1.getEnd();
    if (dist > 0)
        return dist;

    // a2 then a1
    dist = a1.getBegin() - a2.getEnd();
    if (dist > 0)
        return dist;

    // overlap --> dist not defined
    return -1;
}
 
Example 4
Source File: CasAssert.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
public CasAssert containsAnnotation(Class<? extends Annotation> annotationClass, int begin, int end) {
	List<Annotation> closedAnnotations = Lists.newArrayList();
	for(Annotation a:getAnnotationList(annotationClass)) {
		if(annotationClass.isInstance(a)
				&& a.getBegin() == begin && a.getEnd() == end)
			return this;
		else {
			/*
			 * Adds this annotation to possible annotations if indexes are close
			 */
			if(a.getBegin() >= begin - 30 && a.getEnd() <= end + 30)
				closedAnnotations.add(a);
		}
	}
	failWithMessage("Expected to contain annotation <%s[%s,%s]> but does not contain it. Close annotations: <%s>",
			annotationClass.getSimpleName(),begin, end,
			toString(closedAnnotations)
		);
	return this;
}
 
Example 5
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * Util to print an annotation for inspection.Prints some text before and
 * after this annotation, and add '{', '}' around the annotation text.
 * 
 * @return
 **/
public static String inspect(Annotation a) {

    final int begin = 40, after = 50;
    try {
        JCas jCas = a.getCAS().getJCas();

        String text = jCas.getDocumentText();

        StringBuilder sb = new StringBuilder();
        if (a.getBegin() - begin < 0) {
            for (int i = 0; i < (begin - a.getBegin()); i++) {
                sb.append(' ');
            }
            sb.append(text.substring(0, a.getBegin()));
        } else {
            sb.append(text.substring(a.getBegin() - begin, a.getBegin()));
        }
        sb.append('{');
        sb.append(a.getCoveredText());
        sb.append('}');

        if (a.getEnd() < text.length()) {
            sb.append(text.substring(a.getEnd(),
                    Math.min(a.getEnd() + after, text.length())));
        }

        sb.append("[pmid:" + getHeaderIntDocId(jCas) + ", " + a.getBegin()
                + ":" + a.getEnd() + "]");

        return sb.toString();

    } catch (Throwable e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 6
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public SelectFSs<T> following(Annotation fs, int offset) {
  if (fs.getBegin() < fs.getEnd()) {
    fs = makePosAnnot(fs.getEnd(), fs.getEnd());
  }
  return commonFollowing(fs, offset);
}
 
Example 7
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * @param annot1
 * @param annot2
 * @return whether annot1 is before annot2
 */
public static Position isBefore(Annotation a1, Annotation a2) {

    // a1 then a2
    if (a2.getBegin() - a1.getEnd() > 0)
        return Position.before;

    // a2 then a1
    if (a1.getBegin() - a2.getEnd() > 0)
        return Position.after;

    // overlap
    return Position.overlap;
}
 
Example 8
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
/**
 * @return all annotations of this jcas that start and end at the same char
 *         as annotIn
 */
public static List<Annotation> findOverlapping(JCas jcas, Annotation annotIn) {

    List<Annotation> overlappings = new ArrayList<Annotation>();
    for (Annotation a : jcas.getAnnotationIndex()) {

        if (a.getBegin() == annotIn.getBegin()
                && a.getEnd() == annotIn.getEnd()) {
            overlappings.add(a);
        }
    }

    return overlappings;
}
 
Example 9
Source File: CasAssert.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public CasAssert doesNotContainAnnotation(Class<? extends Annotation> annotationClass, int begin, int end) {
	for(Annotation a:getAnnotationList(annotationClass)) 
		if(annotationClass.isInstance(a)
				&& a.getBegin() == begin && a.getEnd() == end)
			failWithMessage("Expected to not contain annotation <%s[%s,%s]> but actually contains it:  <%s>",
					annotationClass.getSimpleName(),begin, end,
					a.getCoveredText()
					);
	return this;
}
 
Example 10
Source File: Subiterator.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private boolean is_beyond_bounds_chk_coveringNvc() {
  Annotation a = it.getNvc();
  int begin = a.getBegin();
  int end = a.getEnd();

  if (begin > this.boundBegin ||
      (begin == this.boundBegin &&
       (end < boundEnd ||
        (end == boundEnd && lto != null && lto.lessThan(a._getTypeImpl(),  boundType))))) {
    makeInvalid();
    return true;
  } else { 
    return false;
  }
}
 
Example 11
Source File: AnnotationVerifier.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
  try {
    Iterator<JCas> viewIterator = aJCas.getViewIterator();

    while (viewIterator.hasNext()) {
      JCas view = viewIterator.next();

      String sofaDataString = view.getSofaDataString();
      int length = sofaDataString != null ? sofaDataString.length() : -1;

      AnnotationIndex<Annotation> annotationIndex = view.getAnnotationIndex();

      for (Annotation annotation : annotationIndex) {
        int begin = annotation.getBegin();
        int end = annotation.getEnd();

        if (begin > end) {
          LOGGER
              .error("Annotation {} begin {} after end {}", annotation.getType().getName(), begin,
                  end);
        }

        if (begin < 0) {
          LOGGER.error("Annotation {} begin {} before 0", annotation.getType().getName(), begin);
        }

        if (end > length) {
          LOGGER.error("Annotation {} end {} after length of sofa {}",
              annotation.getType().getName(),
              end, length);
        }

      }
    }

  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 12
Source File: JCasUtils.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
public static boolean sameIndexes(Annotation anno1, Annotation anno2) {
	return anno1.getBegin() == anno2.getBegin() && anno1.getEnd() == anno2.getEnd();		
}
 
Example 13
Source File: AnnotationIndexTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void vall(int outerIter, int innerIter) {
    long start = System.nanoTime();
    FSIterator<Annotation> it = ai.iterator();
   
//    if (innerIter == 55555) {
//      System.out.println("debug 55555");
//    }

    for (int i = 0; i < SZ; i ++) {
      Annotation fs = as[i];
//      startIter.get()[0] = innerIter > 10000 ? System.nanoTime() : -1;
//      long iterStart = System.nanoTime();
      it.moveTo(fs);
//      long inter2 = System.nanoTime();
//      long inter = inter2 - iterStart;
//      if (innerIter == 55555) {
//        System.out.format("moveTo for innerIter:         %,d item: %d took: %,5d %s%n", innerIter, i, inter, fs);
//      }
//      inter2 = System.nanoTime();
      it.moveToPrevious();
//      inter = System.nanoTime() - inter2;
//      if (innerIter == 55555) {
//        System.out.format("moveToPrevious for innerIter: %,d item: %d took: %,5d %s%n", innerIter, i, inter, fs);
//      }
//      if (innerIter > 10000) {
//        iterTimes.add(new Miter(outerIter, innerIter, i, System.nanoTime() - startIter.get()[0]));
//      }
      if (it.isValid()) {
        if (fs.getBegin() != it.get().getBegin() + 1) {
          System.out.println("debug mismatch");
          fail();
        }
      } else {
        if (fs.getBegin() != 0) {
          System.out.println("debug mismatch");
          fail();
        }
      }
    }
    long inc = System.nanoTime() - start;
    valTime += inc;
    
//    TOP[] cc = a.getInternalArrayDebug();
//    for (int i = 0; i < SZ; i++) {
//      if (cc[i] == null) {
//        System.out.println("debug found null");
//      }
//    }    
  }
 
Example 14
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 4 votes vote down vote up
public static boolean haveSameBeginEnd(Annotation a, Annotation b) {
    return a.getBegin() == b.getBegin() && a.getEnd() == b.getEnd();
}
 
Example 15
Source File: Subiterator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/** 
   * copy constructor - no move to start
   * @param it -
   * @param boundingAnnot -
   * @param ambiguous -
   * @param strict -
   * @param boundsUse -
   * @param isUseTypePriority -
   * @param isSkipSameBeginEndType -
   * @param startId -
   * @param isEmpty -
   * @param converingStartPos -
   * @param converingEndPos -
   */
  Subiterator(
      FSIterator<Annotation> it, 
      Annotation boundingAnnot, 
      boolean ambiguous, 
      boolean strict,    // omit FSs whose end > bounds
      BoundsUse boundsUse, // null if boundingAnnot being used for starting position in unambiguous iterator
      boolean isUseTypePriority,
      boolean isSkipSameBeginEndType,
      int startId,
      boolean isEmpty,
      Annotation coveringStartPos,
      boolean isDoEqualsTest
      ) {
    
    this.it = (LowLevelIterator<Annotation>) it;
    this.boundingAnnot = boundingAnnot;  // could be same begin/end, coveredby, or covering
    this.isBounded = boundsUse != null && boundsUse != BoundsUse.notBounded;
    this.boundsUse = (boundsUse == null) ? BoundsUse.notBounded : boundsUse;
    this.isUnambiguous = !ambiguous;
    if (strict) {
      if (BoundsUse.coveredBy != boundsUse && BoundsUse.sameBeginEnd != boundsUse) {
        throw new IllegalArgumentException("Strict requires BoundsUse.coveredBy or BoundsUse.sameBeginEnd");
      }
    }
    this.isStrict = strict;
    this.isSkipSameBeginEndType = isSkipSameBeginEndType;

    this.boundBegin = isBounded ? boundingAnnot.getBegin() : -1;
    this.boundEnd = isBounded ? boundingAnnot.getEnd(): -1;
    this.boundType = isBounded ? (TypeImpl) boundingAnnot.getType() : null;

    FSIndexRepositoryImpl ir = this.it.ll_getIndex().getCasImpl().indexRepository;
//    underlying_iterator_using_typepriorities =  ir.isAnnotationComparator_usesTypeOrder();

    this.isUseTypePriority = isUseTypePriority;
    lto = isUseTypePriority ? ir.getDefaultTypeOrder() : null;

    this.comparatorMaybeNoTypeWithoutId = ir.getAnnotationFsComparator(
        FSComparators.WITHOUT_ID, 
        isUseTypePriority ?  FSComparators.WITH_TYPE_ORDER : FSComparators.WITHOUT_TYPE_ORDER);
    this.annotationComparator_withId = ir.getAnnotationFsComparatorWithId();
    
    this.jcas = (JCasImpl) ll_getIndex().getCasImpl().getJCas();
    
    this.coveringStartPos = coveringStartPos;
    this.startId = startId;
    this.isEmpty = isEmpty;
    if (isEmpty) {
      makeInvalid();
    }    
    this.isDoEqualsTest = isDoEqualsTest;
  }
 
Example 16
Source File: PersonTitleAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * Annotates a document. This annotator searches for person titles using simple string matching.
   * 
   * @param aCAS
   *          CAS containing document text and previously discovered annotations, and to which new
   *          annotations are to be written.
   * 
   * @see CasAnnotator_ImplBase#process(CAS)
   */
  public void process(CAS aCAS) throws AnalysisEngineProcessException {
    try {
      // If the ResultSpec doesn't include the PersonTitle type, we have
      // nothing to do.
      if (!getResultSpecification().containsType("example.PersonTitle",aCAS.getDocumentLanguage())) {
        if (!warningMsgShown) {
          String m = String.format(
              "No output is being produced by the PersonTitleAnnotator because the Result Specification did not contain" +
              " a request for the type example.PersonTitle with the language '%s'%n" +
              "  (Note: this message will only be shown once.)%n", 
              aCAS.getDocumentLanguage());               
          System.err.println(m);
          logger.log(Level.WARNING, m);
          warningMsgShown = true;
        }
        return;
      }

      if (mContainingType == null) {
        // Search the whole document for PersonTitle annotations
        String text = aCAS.getDocumentText();
        annotateRange(aCAS, text, 0);
      } else {
        // Search only within annotations of type mContainingType

        //v3
        
        for (Annotation annot : aCAS.<Annotation>select(mContainingType)) {
          
          String coveredText = annot.getCoveredText();  // Get text covered by this annotation
          int annotBegin = annot.getBegin();            // Get begin position of this annotation
          annotateRange(aCAS, coveredText, annotBegin); // search for matches within this
        
        }
        
        // v2
//        // Get an iterator over the annotations of type mContainingType.
//        FSIterator it = aCAS.getAnnotationIndex(mContainingType).iterator();
//        // Loop over the iterator.
//        while (it.isValid()) {
//          // Get the next annotation from the iterator
//          AnnotationFS annot = (AnnotationFS) it.get();
//          // Get text covered by this annotation
//          String coveredText = annot.getCoveredText();
//          // Get begin position of this annotation
//          int annotBegin = annot.getBegin();
//          // search for matches within this
//          annotateRange(aCAS, coveredText, annotBegin);
//          // Advance the iterator.
//          it.moveToNext();
//        }
      }
    } catch (Exception e) {
      throw new AnalysisEngineProcessException(e);
    }
  }
 
Example 17
Source File: WriteCoocurrencesToLoadfile2.java    From bluima with Apache License 2.0 4 votes vote down vote up
public static String snippet(JCas jCas, int snippetBegin, int snippetEnd,
        Annotation a1, Annotation a2) {

    final String T_BEGIN = "<strong class=\"";
    final String T_END = "</strong>";

    String s = jCas.getDocumentText().substring(snippetBegin, snippetEnd);
    String sError = s;

    try {

        int relA1Begin = a1.getBegin() - snippetBegin;
        int relA1End = a1.getEnd() - snippetBegin;
        int relA2Begin = a2.getBegin() - snippetBegin;
        int relA2End = a2.getEnd() - snippetBegin;

        // highlight
        Position position = BlueCasUtil.isBefore(a1, a2);
        if (position == Position.before) {
            // start w/ a2 (at the end)
            s = s.substring(0, relA2End) + T_END + s.substring(relA2End);
            s = s.substring(0, relA2Begin) + T_BEGIN
                    + a2.getClass().getSimpleName() + "\">"
                    + s.substring(relA2Begin);
            // now with a1
            s = s.substring(0, relA1End) + T_END + s.substring(relA1End);
            s = s.substring(0, relA1Begin) + T_BEGIN
                    + a1.getClass().getSimpleName() + "\">"
                    + s.substring(relA1Begin);

        } else if (position == Position.after) {
            // start w/ a1 (at the end)
            s = s.substring(0, relA1End) + T_END + s.substring(relA1End);
            s = s.substring(0, relA1Begin) + T_BEGIN
                    + a1.getClass().getSimpleName() + "\">"
                    + s.substring(relA1Begin);
            // now with a2
            s = s.substring(0, relA2End) + T_END + s.substring(relA2End);
            s = s.substring(0, relA2Begin) + T_BEGIN
                    + a2.getClass().getSimpleName() + "\">"
                    + s.substring(relA2Begin);

        } else {// overlap LATER
            LOG.warn(
                    "don't know how to highlight snippet that overlaps '{}' and '{}', pmid:{}"
                            + getHeaderDocId(jCas), a1.getCoveredText(),
                    a2.getCoveredText());
        }

        return s.replaceAll("[\r\t\n]", "");

    } catch (Exception e) {
        LOG.warn(
                "Could not extract snippet on pmid {} beg {} end {} a1 {} a2 {}",
                new Object[] { getHeaderDocId(jCas), snippetBegin,
                        snippetEnd, To.string(a1), To.string(a2) });
        return sError;
    }
}
 
Example 18
Source File: Subiterator.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Special equalToBounds used only for having bounded iterators 
 * skip returning the bounding annotation
 * 
 * Two styles: uimaFIT style: only skip the exact one (id's the same)
 *             uima style: skip all that compare equal using the AnnotationIndex comparator
 * @param fs -
 * @return true if should be skipped
 */
private boolean equalToBounds(Annotation fs) {
  return isDoEqualsTest && fs._id == boundingAnnot._id ||
         (isSkipSameBeginEndType &&
          fs.getBegin() == boundBegin &&
          fs.getEnd() == boundEnd &&
          fs.getType() == boundType); 
}
 
Example 19
Source File: ComparableEntitySpanUtils.java    From baleen with Apache License 2.0 2 votes vote down vote up
/**
 * Overlaps.
 *
 * @param a the a
 * @param b the b
 * @return true, if successful
 */
public static boolean overlaps(Annotation a, Annotation b) {
  return !(a.getEnd() < b.getBegin() || b.getEnd() < a.getBegin());
}
 
Example 20
Source File: AnnotationUtils.java    From baleen with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if an annotation is covering another annotation.
 *
 * <p>Overlapping annotations are not considered covered.
 *
 * @param covering the annotation covering
 * @param covered the annotation covered
 * @return true, if is in covering
 */
public static boolean isCovering(final Annotation covering, final Annotation covered) {
  return covering.getBegin() <= covered.getBegin() && covering.getEnd() >= covered.getEnd();
}