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

The following examples show how to use org.apache.uima.jcas.tcas.Annotation#getEnd() . 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: 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 2
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 3
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public SelectFSs<T> preceding(Annotation annotation, int offset) {
  if (annotation.getEnd() < Integer.MAX_VALUE) {
    annotation = makePosAnnot(annotation.getBegin(), Integer.MAX_VALUE);
  }
  return commonPreceding(annotation, offset);
}
 
Example 4
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 5
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 6
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 7
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 8
Source File: Subiterator.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Assume: on entry the subiterator might not be in a "valid" position
 *   Case: moveToJustPastBoundsAndBackup...
 * 
 * Adjust: skip over annotations whose "end" is < bound end, or
 *   whose end is == to bound end and begin == bound begin 
 *     and linearTypeOrder exists, 
 *     and annotation lessThan the bound type.
 * Marks iterator invalid if moves beyond bound
 * 
 * when covering (which is different from coveredBy and sameBeginEnd,
 *  means get all annotations which span the bounds), 
 *  skip items where the end < bounds end,
 *    because those items don't span the bounding FS
 * 
 * Edge case: item with same begin and end is considered "covering"
 *              but subject to exclusion based on equalToBounds skipping
 * Cases:
 *   position of begin is after span begin - mark "invalid"
 *   position of begin is before or == span begin:
 *     position of end is == or > span end: OK (except if begin is ==, then do lto check)
 *     position of end is < span end:
 *       if backward: moveToPrev until get valid position or run out.  if run out, mark invalid
 *       if forward: move to next while position of begin is <= span begin.
 *      
 * @param forward true if moving forward
 */
private void adjustForCovering_forward() {
  if (!it.isValid()) {
    return;
  }
  Annotation a = it.getNvc();
  int begin = a.getBegin();
  int end = a.getEnd();
  
  // moveTo may move to invalid position
  // if the cur pos item has a begin beyond the bound, it cannot be a covering annotation
  if (begin > this.boundBegin) {
    makeInvalid();
    return;
  }
  
  // begin is <= bound begin
    
  // skip until get an FS whose end >= boundEnd, it is a candidate.
  //   stop if begin gets too large (going forwards)
  // while test: is true if need to move to skip over a too-small "end"
  while (it.isValid() && 
         (begin = (a = it.getNvc()).getBegin()) <= this.boundBegin &&
         ( (end = a.getEnd()) < this.boundEnd || 
           (end == this.boundEnd && lto != null && lto.lessThan(a._getTypeImpl(), this.boundType)))) {
    it.moveToNextNvc();
  }
}
 
Example 9
Source File: AnnotationUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if an annotation is in between the source and target entities (in the sentence).
 *
 * <p>The order of source and target is not important (target could be earlier in the sentence
 * than source).
 *
 * <p>Overlapping annotations are not considered between.
 *
 * @param between the entity to test if in the middle
 * @param source an entity which will be on the left/right
 * @param target an entity which will be on the left/right
 * @return true, if is in between
 */
public static boolean isInBetween(
    final Annotation between, final Annotation source, final Annotation target) {
  int left;
  int right;
  if (source.getEnd() <= target.getBegin()) {
    left = source.getEnd();
    right = target.getBegin();
  } else {
    left = target.getEnd();
    right = source.getBegin();
  }

  return left <= between.getBegin() && between.getEnd() <= right;
}
 
Example 10
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 11
Source File: JCasUtils.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
public static boolean contains(Annotation container, Annotation subAnnotation) {
	return container.getBegin() <= subAnnotation.getBegin() && container.getEnd()>= subAnnotation.getEnd();
}
 
Example 12
Source File: StringMatchingRecommender.java    From inception with Apache License 2.0 4 votes vote down vote up
private List<Sample> predict(int aDocNo, CAS aCas, Trie<DictEntry> aDict)
{
    boolean requireEndAtTokenBoundary = !CHARACTERS
            .equals(getRecommender().getLayer().getAnchoringMode());

    boolean requireSingleSentence = !getRecommender().getLayer().isCrossSentence();

    Type sentenceType = getType(aCas, Sentence.class);
    Type tokenType = getType(aCas, Token.class);
    
    List<Sample> data = new ArrayList<>();
    String text = aCas.getDocumentText();   
        
    for (Annotation sentence : aCas.<Annotation>select(sentenceType)) {
        List<Span> spans = new ArrayList<>();
        List<Annotation> tokens = aCas.<Annotation>select(tokenType).coveredBy(sentence)
                .asList();
        for (Annotation token : tokens) {
            Trie<DictEntry>.Node node = aDict.getNode(text, token.getBegin());
            if (node != null) {
                int begin = token.getBegin();
                int end = begin + node.level;

                // If the end is not in the same sentence as the start, skip
                if (requireSingleSentence && !(end <= sentence.getEnd())) {
                    continue;
                }

                // Need to check that the match actually ends at a token boundary!
                if (
                        requireEndAtTokenBoundary && 
                        !aCas.<Annotation>select(tokenType).startAt(token)
                                .filter(t -> t.getEnd() == end).findAny().isPresent()
                ) {
                    continue;
                }
                
                for (LabelStats lc : node.value.getBest(maxRecommendations)) {
                    String label = lc.getLabel();
                    // check instance equality to avoid collision with user labels
                    if (label == UNKNOWN_LABEL) {
                        label = null;
                    }
                    spans.add(new Span(begin, end, text.substring(begin, end), label,
                            lc.getRelFreq()));
                }
            }
        }
        
        data.add(new Sample(aDocNo, aCas.getDocumentText(), tokens, spans));
    }
    
    return data;
}
 
Example 13
Source File: PrintAnnotationInSentenceWriter.java    From bluima with Apache License 2.0 4 votes vote down vote up
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    String pmid = getHeaderDocId(jcas);
    try {
        int sentenceId = 0;
        for (Sentence s : select(jcas, Sentence.class)) {
            String txt = s.getCoveredText();

            // selects annots contained in this sentence, and adds {{ }}
            // around annot, in reverse order (so it starts at end)
            ArrayList<Annotation> annots = newArrayList(selectCovered(
                    annotationClass, s));
            if (!annots.isEmpty()) {
                for (Annotation a : reverse(annots)) {

                    int begin = a.getBegin() - s.getBegin(), end = a
                            .getEnd() - s.getBegin();

                    txt = txt.substring(0, begin) + startTag
                            + a.getCoveredText() + endTag
                            + txt.substring(end);
                }
                // remove newlines & normalize space
                txt.replaceAll("\\r\\n|\\r|\\n", " ").replaceAll(" +", " ");

                if (htmlOutput) {
                    writer.append("<p><a href=\"http://www.ncbi.nlm.nih.gov/pubmed/"
                            + pmid
                            + "\">"
                            + pmid
                            + "["
                            + sentenceId
                            + "]</a> " + txt + "</p>\n");
                } else
                    writer.append(pmid + ":" + sentenceId + "\t" + txt
                            + "\n");
            }
            sentenceId++;
        }
        writer.flush();
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    }
}
 
Example 14
Source File: BartWriter.java    From bluima with Apache License 2.0 4 votes vote down vote up
private void write(String pmid, JCas jCas, String path,
        Iterable<Annotation> annotations) throws IOException {

    // write sentences
    TextFileWriter textWriter = new TextFileWriter(path + ".txt");
    for (Annotation s : filterStrict(annotations, Sentence.class)) {
        textWriter.addLine(s.getCoveredText());
    }
    textWriter.close();
    if (debug)
        System.out.println("\nAnnotations for " + pmid);

    // write annotations
    BufferedWriter annotWriter = new BufferedWriter(new FileWriter(
            new File(path + ".ann")));
    int id = 0;
    String notes = "";
    for (Annotation a : annotations) {
        String aName = a.getClass().getName();
        if (!NON_CONTENT_ANNOTATIONS.contains(aName)) {
            try {
                annotationsSet.add(a.getClass().getSimpleName());
                String annotStr = "T" + id + "\t"
                        + a.getClass().getSimpleName() + " " + a.getBegin()
                        + " " + a.getEnd() + "\t" + a.getCoveredText()
                        + "\n";
                if (debug)
                    System.out.println(annotStr);
                annotWriter.append(annotStr);
                notes += "#" + id + "\tAnnotatorNotes T" + id + "\t"
                        + To.string(a) + "\n"; // TODO better
                id++;
            } catch (Exception e) {
                LOG.warn("could not extract " + aName + " from " + pmid
                        + " {}", e.getMessage());
            }
        }
    }
    if (writeNotes)
        annotWriter.append(notes);
    annotWriter.close();

    // TODO write relations
}
 
Example 15
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 16
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 17
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 18
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 19
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();
}
 
Example 20
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 2 votes vote down vote up
/**
 * @param annot1
 * @param annot2
 * @return whether annot1 is contained in annot2. true if has same begin-end
 */
public static boolean isContained(Annotation a1, Annotation a2) {
    return a1.getBegin() >= a2.getBegin() && a1.getEnd() <= a2.getEnd();
}