Java Code Examples for org.apache.uima.cas.FSIterator#moveToNext()

The following examples show how to use org.apache.uima.cas.FSIterator#moveToNext() . 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: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public T singleOrNull() {
  FSIterator<T> it = fsIterator();
  if (it.isValid()) {
    T v = it.getNvc();
    if (shift >= 0) {
      it.moveToNext();
    } else {
      it.moveToPrevious();
    }
    if (it.isValid()) {
      throw new CASRuntimeException(CASRuntimeException.SELECT_GET_TOO_MANY_INSTANCES, ti.getName(), maybeMsgPosition());
    }
    return v;
  }
  return null;
}
 
Example 2
Source File: IteratorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void verifyConcurrantModificationDetected(FSIterator<?> it) {
    boolean caught = false;
    try {
      it.moveToNext();
    } catch (Exception e) {
      caught = true;
    }
    
    assertFalse(caught); // because of copy-on-write
    
//    if (it.isValid()) {
//      it.isValid(); // debug
//      assertTrue(caught);  // v3: it becomes invalid
//    }
//    if (caught != true) {
//      System.out.println("Debug");
//    }
  }
 
Example 3
Source File: CasTreeViewer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Recursive method called by {@link buildTree(DefaultMutableTreeNode,FSIterator)}.
 * 
 * @param aParentNode
 *          root node of tree to be built
 * @param aIterator
 *          iterator over all annotation in CAS
 * @param aStartPos
 *          text position at which to begin processing
 * @param aEndPos
 *          text position at which to end processing
 */
private void _buildTree(DefaultMutableTreeNode aParentNode, FSIterator aIterator, int aStartPos,
        int aEndPos) {
  while (aIterator.isValid()) {
    AnnotationFS curAnnot = (AnnotationFS) aIterator.get();
    int curAnnotStart = curAnnot.getBegin();
    int curAnnotEnd = curAnnot.getEnd();
    if (curAnnotEnd <= aEndPos) {
      // move iterator to next annotation
      aIterator.moveToNext();

      if (curAnnotStart < curAnnotEnd) // account for bug in JTalent
      {
        // add this annotation as a child of aParentNode
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new AnnotationTreeNodeObject(
                curAnnot));
        aParentNode.add(newNode);
        // recursively add children to this node
        _buildTree(newNode, aIterator, curAnnotStart, curAnnotEnd);
      }
    } else
      break;
  }
}
 
Example 4
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void assertEqualsInner(CAS c1, CAS c2) {
  alreadyCompared.clear();
  
  // this code handles initial views with no SofaFS
  CAS initialView1 = c1.getView(CAS.NAME_DEFAULT_SOFA);
  CAS initialView2 = c2.getView(CAS.NAME_DEFAULT_SOFA);
  assertEqualViewsInner(initialView1, initialView2);
  // this code skips the initial view, if it doesn't have a sofa FS
  FSIterator<Sofa> sofaIter = c1.getSofaIterator();
  int c1Sofas = 0;
  while (sofaIter.hasNext()) {
    SofaFS sofa = sofaIter.next();
    CAS tcas1 = c1.getView(sofa);
    CAS tcas2 = c2.getView(tcas1.getViewName());
    assertEqualViewsInner(tcas1, tcas2);
    c1Sofas++;
  }
  sofaIter = c2.getSofaIterator();
  int c2Sofas = 0;
  while (sofaIter.hasNext()) {
    c2Sofas++;
    sofaIter.moveToNext();
  }
  Assert.assertTrue(c1Sofas == c2Sofas);
}
 
Example 5
Source File: FilteredIteratorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void iterAndCount1a() {
  // create filtered iterator over Tokens only
  FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();
  FSTypeConstraint constraint = cas.getConstraintFactory().createTypeConstraint();
  constraint.add(tokenType.getName());
  it = cas.createFilteredIterator(it, constraint);

  // do iteration
  while (it.isValid()) {
    AnnotationFS a = it.get();
    assertTrue(a.getType().equals(tokenType));
    // System.out.println("Annotation type: " + a.getType().getName());
    // System.out.println("Covered text: " + a.getCoveredText());
    it.moveToNext();
  }

}
 
Example 6
Source File: AnnotationIndexImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void addChildren(AnnotationTreeNodeImpl<T> node, FSIterator<T> it) {
  AnnotationTreeNodeImpl<T> dtr;
  T annot;
  while (it.isValid()) {
    annot = it.get();
    it.moveToNext();
    dtr = new AnnotationTreeNodeImpl<T>();
    dtr.set(annot);
    node.addChild(dtr);
    addChildren(dtr, subiterator(annot, false, true));
  }
}
 
Example 7
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private <T extends AnnotationFS> T createDocumentAnnotation(int length) {
  final TypeSystemImpl ts = getTypeSystemImpl();
  // Remove any existing document annotations.
  FSIterator<T> it = this.<T>getAnnotationIndex(ts.docType).iterator();
  List<T> list = new ArrayList<>();
  while (it.isValid()) {
    list.add(it.get());
    it.moveToNext();
  }
  for (int i = 0; i < list.size(); i++) {
    getIndexRepository().removeFS(list.get(i));
  }
  
  return (T) createDocumentAnnotationNoRemove(length);
}
 
Example 8
Source File: ConsumerCasUtils.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public static String getStringFeatValue(CAS aCasView, String aTypeS, String aFeatS) {
  String result = null;
  Type type = aCasView.getTypeSystem().getType(aTypeS);
  if (type != null) {
    FSIterator<AnnotationFS> idIter = aCasView.getAnnotationIndex(type).iterator();
    while (idIter.isValid()) {
      org.apache.uima.cas.FeatureStructure idFS = idIter.get();
      result = idFS.getStringValue(aCasView.getTypeSystem().getFeatureByFullName(
              aTypeS + ":" + aFeatS));
      idIter.moveToNext();
    }
  }
  return result;
}
 
Example 9
Source File: VinciAnalysisEngineService_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Analyzes a given document by a AnalysisEngine. When completed this method returns a VinciFrame
 * containing XCAS translated into a set of Vinci subFrames. Each subframe containing one
 * annotation with all its attributes.
 *
 * @param ct the ct
 * @return VinciFrame containing XCAS translated into a set of Vinci subframes.
 * @exception Exception              if there is an error during processing
 */
private Transportable analyze(CASTransportable ct) throws Exception {
  CAS cas = ct.getCas();
  try {
    long annotStartTime = System.currentTimeMillis();
    mAE.process(cas);
    int annotationTime = (int) (System.currentTimeMillis() - annotStartTime);
    if (debug) {
      System.out.println("Annotation took: " + annotationTime + "ms");
    }
    ct.getExtraDataFrame().fset(Constants.ANNOTATION_TIME, annotationTime);
    // Extract CAS
    // UIMAFramework.getLogger().log("CAS ACount::" +
    // cas.getAnnotationIndex().size());
    int totalAnnots = 0;
    SofaFS sofa;
    FSIterator sItr = cas.getSofaIterator();
    while (sItr.isValid()) {
      sofa = (SofaFS) sItr.get();
      totalAnnots += cas.getView(sofa).getAnnotationIndex().size();
      sItr.moveToNext();
    }
    UIMAFramework.getLogger().log(Level.FINEST, "CAS ACount::" + totalAnnots);
    ct.setCommand(null);
    return ct;
  } catch (Exception ex) {
    ct.cleanup();
    throw ex;
  }
}
 
Example 10
Source File: ConsumerCasUtils.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Feature Structure of a given type.
 *
 * @param aCasView -
 *          CAS instance to retrieve data from
 * @param aTypeS -
 *          Feature Type
 * @return the first Feature Structure of a given type
 */
public static FeatureStructure getTcasFS(CAS aCasView, String aTypeS) {
  org.apache.uima.cas.FeatureStructure idFS = null;
  FSIterator idIter = aCasView.getAnnotationIndex(aCasView.getTypeSystem().getType(aTypeS)).iterator();
  while (idIter != null && idIter.isValid()) {
    idFS = idIter.get();
    idIter.moveToNext();
  }
  return idFS;
}
 
Example 11
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the n annotations following the given annotation
 * 
 * @param cas
 *          a CAS.
 * @param type
 *          a UIMA type.
 * @param annotation
 *          anchor annotation
 * @param count
 *          number of annotations to collect
 * @return List of aType annotations following anchor annotation
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static List<AnnotationFS> selectFollowing(CAS cas, Type type, AnnotationFS annotation,
        int count) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }

  // Seek annotation in index
  // withSnapshotIterators() not needed here since we copy the FSes to a list anyway    
  FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator();
  itr.moveTo(annotation);

  // When seeking forward, there is no need to check if the insertion point is beyond the
  // index. If it was, there would be nothing beyond it that could be found and returned.
  // The moveTo operation also does not yield an iterator being invalid because it points
  // *before the first* index entry, at max it points *to the first* index entry, so this
  // case also does not need to be handled.

  // No need to do additional seeks here (as done in selectCovered) because the current method
  // does not have to worry about type priorities - it never returns annotations that have
  // the same offset as the reference annotation.

  // make sure we're past the end of the reference annotation
  while (itr.isValid() && itr.get().getBegin() < annotation.getEnd()) {
    itr.moveToNext();
  }

  // add annotations from the iterator into the result list
  List<AnnotationFS> followingAnnotations = new ArrayList<AnnotationFS>();
  for (int i = 0; i < count && itr.isValid(); i++, itr.moveToNext()) {
    followingAnnotations.add(itr.get());
  }
  return followingAnnotations;
}
 
Example 12
Source File: AbbreviationsExpanderAnnotator.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * REM: modified from CasUtils..
 * 
 * Get a list of annotations of the given annotation type constraint by a
 * start and end. It's good to provide an annotation that is close to the
 * region, to quickly move the iterator
 * 
 * @param cas
 *            a CAS.
 * @param type
 *            a UIMA type.
 * @param annotationCloseToTheRegion
 *            the covering annotation.
 * @return a return value.
 * @see Subiterator
 */
private static List<Annotation> selectMatching(CAS cas, final int begin,
        final int end, AnnotationFS annotationCloseToTheRegion) {

    final List<Annotation> list = new ArrayList<Annotation>();
    final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();

    // Try to seek the insertion point.
    it.moveTo(annotationCloseToTheRegion);

    // If the insertion point is beyond the index, move back to the last.
    if (!it.isValid()) {
        it.moveToLast();
        if (!it.isValid()) {
            return list;
        }
    }

    // Ignore type priorities by seeking to the first that has the same
    // begin
    boolean moved = false;
    while (it.isValid() && (it.get()).getBegin() >= begin) {
        it.moveToPrevious();
        moved = true;
    }

    // If we moved, then we are now on one starting before the requested
    // begin, so we have to
    // move one ahead.
    if (moved) {
        it.moveToNext();
    }

    // If we managed to move outside the index, start at first.
    if (!it.isValid()) {
        it.moveToFirst();
    }

    // Skip annotations whose start is before the start parameter.
    while (it.isValid() && (it.get()).getBegin() < begin) {
        it.moveToNext();
    }

    while (it.isValid()) {
        AnnotationFS a = it.get();
        if (!(a instanceof Annotation))
            continue;

        // If the start of the current annotation is past the end parameter,
        // we're done.
        if (a.getBegin() > end) {
            break;
        }
        it.moveToNext();

        if (a.getBegin() == begin && a.getEnd() == end) {
            list.add((Annotation) a);
        }
    }
    return unmodifiableList(list);
}
 
Example 13
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
/**
 * Return an annotation preceding or following of a given reference annotation. If the type
 * parameter corresponds to the type or a subtype of the anchor annotation and the relative
 * position is 0, then the anchor annotation is returned.
 * 
 * @param cas
 *          a CAS containing the annotation.
 * @param type
 *          a type.
 * @param aAnchor
 *          anchor annotation
 * @param aPosition
 *          relative position to access. A negative value selects a preceding annotation while a
 *          positive number selects a following annotation.
 * @return the addressed annotation.
 * @throws IndexOutOfBoundsException
 *           if the relative position points beyond the type index bounds.
 * @throws IllegalArgumentException
 *           if the relative position is {@code 0} and the anchor type does not subsume the
 *           selected type.
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static AnnotationFS selectSingleRelative(CAS cas, Type type, AnnotationFS aAnchor,
        int aPosition) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }

  // move to first previous annotation
  FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator();
  itr.moveTo(aAnchor);

  if (aPosition < 0) {
    // If the insertion point is beyond the index, move back to the last.
    if (!itr.isValid()) {
      itr.moveToLast();
      if (!itr.isValid()) {
        throw new IndexOutOfBoundsException("Reached end of index while seeking.");
      }
    }

    // No need to do additional seeks here (as done in selectCovered) because the current method
    // does not have to worry about type priorities - it never returns annotations that have
    // the same offset as the reference annotation.

    // make sure we're past the beginning of the reference annotation
    while (itr.isValid() && itr.get().getEnd() > aAnchor.getBegin()) {
      itr.moveToPrevious();
    }

    for (int i = 0; i < (-aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) {
      // Seeking
    }

    if (!itr.isValid()) {
      throw new IndexOutOfBoundsException("Reached end of index while seeking.");
    } else {
      return itr.get();
    }
  } else if (aPosition > 0) {
    // When seeking forward, there is no need to check if the insertion point is beyond the
    // index. If it was, there would be nothing beyond it that could be found and returned.
    // The moveTo operation also does not yield an iterator being invalid because it points
    // *before the first* index entry, at max it points *to the first* index entry, so this
    // case also does not need to be handled.
    
    // No need to do additional seeks here (as done in selectCovered) because the current method
    // does not have to worry about type priorities - it never returns annotations that have
    // the same offset as the reference annotation.
    
    // make sure we're past the end of the reference annotation
    while (itr.isValid() && itr.get().getBegin() < aAnchor.getEnd()) {
      itr.moveToNext();
    }

    for (int i = 0; i < (aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) {
      // Seeking
    }

    if (!itr.isValid()) {
      throw new IndexOutOfBoundsException("Reached end of index while seeking.");
    } else {
      return itr.get();
    }
  } else if (cas.getTypeSystem().subsumes(aAnchor.getType(), type)) {
    return aAnchor;
  }
  else {
    throw new IllegalArgumentException(
            "Relative position cannot be 0 if the type of the anchor annotator does not subsume "
            + "the selected type.");
  }
}
 
Example 14
Source File: SerializationReinitTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void createSentences() throws CASException {
  // TypeSystem ts = cas.getTypeSystem();
  // Type eosType = ts.getType(EOS_TYPE);
  // Type tokenType = ts.getType(TOKEN_TYPE);
  // //assert(tokenType != null);
  // Type sentenceType = ts.getType(SENT_TYPE);
  // Feature tokenTypeFeature = ts.getFeature(TOKEN_TYPE_FEAT);
  // Feature startFeature = ts.getFeature(CAS.START_FEAT);
  // Feature endFeature = ts.getFeature(CAS.END_FEAT);

  // System.out.println("\nCreating sentence annotations.");

  // Get a handle to the index repository.
  FSIndexRepository indexRepository = cas.getIndexRepository();
  // assert(indexRepository != null);
  Iterator<String> labelIt = indexRepository.getLabels();
  assertTrue(labelIt != null);
  // Get the standard index for tokens.
  FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType);
  // assert(tokenIndex != null);
  // Get an iterator over tokens.
  FSIterator<AnnotationFS> it = tokenIndex.iterator();
  // assert(it != null);
  // Now create sentences. We do this as follows: a sentence starts where
  // the first token after an EOS starts, and ends with an EOS.
  long time = System.currentTimeMillis();
  int endOfSentenceCounter = 0;
  it.moveToFirst();
  boolean lookForStart = true;
  int start = 0, end; // Initialize start to pacify compiler.
  FeatureStructure tokenFS, sentFS;
  while (it.isValid()) {
    if (lookForStart) {
      // If we're looking for the start of a sentence, just grab the start
      // of the current FS.
      start = it.get().getIntValue(startFeature);
      lookForStart = false;
    } else {
      // Check if we've reached the end of a sentence.
      tokenFS = it.get();
      if (tokenFS.getFeatureValue(tokenTypeFeature).getType() == eosType) {
        end = tokenFS.getIntValue(endFeature);
        sentFS = cas.createFS(sentenceType);
        sentFS.setIntValue(startFeature, start);
        sentFS.setIntValue(endFeature, end);
        cas.getIndexRepository().addFS(sentFS);
        ++endOfSentenceCounter;
        lookForStart = true;
      }
    }
    it.moveToNext();
  }
  time = System.currentTimeMillis() - time;
  // System.out.println("Created " + endOfSentenceCounter + " sentences: " + new TimeSpan(time));
}
 
Example 15
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void incr(FSIterator<T> it) {
  it.moveToNext();    
}
 
Example 16
Source File: GetAllIndexedTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Test driver.
 */
public void testGetAllIndexed() throws Exception {
  initTest();
  FeatureStructure docAnnotation = this.cas.getDocumentAnnotation();
	assertNotNull(docAnnotation);
  ++this.fsCount;
  assertTrue(getIteratorSize(getAllIndexed()) == this.fsCount);
	final FeatureStructure otherAnnotationFS = this.cas.createFS(this.otherAnnotationType);
	FeatureStructure annotationFS = this.cas.createFS(this.annotationType);
	final FeatureStructure annotationBaseFS = this.cas.createFS(this.annotationBaseType);
	addFS(annotationFS);
  addFS(otherAnnotationFS);
  addFS(annotationBaseFS);
  addFS(this.cas.createFS(this.cas.getTypeSystem().getTopType()));
  assertTrue(getIteratorSize(this.cas.getAnnotationIndex().iterator()) == 2);
  addFS(createAnnot(0, 1));
  addFS(createAnnot(1, 2));
  addFS(createAnnot(2, 3));
  addFS(createAnnot(3, 4));
  
  // Iterate backwards, check only that it returns correct number of FSs
  FSIterator<FeatureStructure> it = getAllIndexed();
  int down = this.fsCount;
  for (it.moveToLast(); it.isValid(); it.moveToPrevious()) {
    --down;
  }
  assertTrue(down == 0);

  // Get all indexed, create copy and iterate in parallel.
  it = getAllIndexed();
  FSIterator<FeatureStructure> copy = it.copy();
  copy.moveToFirst();
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    assertTrue(copy.isValid());
    assertTrue(it.get().equals(copy.get()));
    copy.moveToNext();
  }
  assertFalse(copy.isValid());
  
  // Iterate over all indexed, create a copy at each stage, check that it gets same FS.
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    copy = it.copy();
    assertTrue(it.get().equals(copy.get()));
  }
  copy = it.copy();
  assertFalse(it.isValid());
  assertFalse(copy.isValid());
  
  //test getAllIndexed(Type)
  Type tokenType = this.cas.getTypeSystem().getType(TOKEN_TYPE);
  assertNotNull(tokenType);
  FSIterator<FeatureStructure> tokenIter = this.cas.getIndexRepository().getAllIndexedFS(tokenType);
  assertFalse(tokenIter.hasNext());
  Iterator<TOP> tokenIter2 = this.cas.getIndexedFSs(tokenType).iterator();
  assertFalse( tokenIter2.hasNext());
}
 
Example 17
Source File: SelectFSs_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * works for AnnotationIndex or general index
   * 
   * position taken from startingFs (not necessarily an Annotation subtype)
   *   - goes to left-most "equal" using comparator, or if none equal, to the first one &gt; startingFs
   *     -- using moveTo(fs)
   * 
   * special processing for AnnotationIndex (only):
   *   - typePriority - use or ignore
   *     -- ignored: after moveTo(fs), moveToPrevious while begin and end ==
   *       // REMOVED see https://issues.apache.org/jira/browse/UIMA-5536 --- and if isPositionUsesType types are == 
   * @param it iterator to position
   * @return it positioned if needed
   */
  private FSIterator<T> maybePosition(FSIterator<T> it) {
    if (!it.isValid() || startingFs == null || boundsUse != BoundsUse.notBounded) {
      return it;
    }
    
    it.moveTo(startingFs);
    
    // next commented out because the underlying iterator already does this
//    if (index != null && index instanceof AnnotationIndex && !isFollowing && !isPreceding) {
//      if (!isTypePriority) {
//        int begin = ((Annotation)startingFs).getBegin();
//        int end = ((Annotation)startingFs).getEnd();
//        Type type = startingFs.getType();
//        Annotation fs = (Annotation) it.get();
//        while (begin == fs.getBegin() && end == fs.getEnd() 
////               && (!isPositionUsesType || type == fs.getType())
//               ) {
//          it.moveToPreviousNvc();
//          if (!it.isValid()) {
//            it.moveToFirst();
//            return it;
//          }
//          fs = (Annotation) it.get();
//        }
//        it.moveToNext();
//      }
//    }
    
    if (isFollowing) {
      final int end = ((Annotation)startingFs).getEnd();
      while (it.isValid() && ((Annotation)it.get()).getBegin() < end) {
        it.moveToNext();
      }
    } else if (isPreceding) {
      final int begin = ((Annotation)startingFs).getBegin();
      while (it.isValid() && ((Annotation)it.get()).getEnd() > begin) {
        it.moveToPrevious();
      }
    }
    
    return it;
  }
 
Example 18
Source File: SerializationNoMDTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void createSentences() {
  // TypeSystem ts = cas.getTypeSystem();
  // Type eosType = ts.getType(EOS_TYPE);
  // Type tokenType = ts.getType(TOKEN_TYPE);
  // //assert(tokenType != null);
  // Type sentenceType = ts.getType(SENT_TYPE);
  // Feature tokenTypeFeature = ts.getFeature(TOKEN_TYPE_FEAT);
  // Feature startFeature = ts.getFeature(CAS.START_FEAT);
  // Feature endFeature = ts.getFeature(CAS.END_FEAT);

  // System.out.println("\nCreating sentence annotations.");

  // Get a handle to the index repository.
  FSIndexRepository indexRepository = cas.getIndexRepository();
  // assert(indexRepository != null);
  Iterator<String> labelIt = indexRepository.getLabels();
  assertTrue(labelIt != null);
  // Get the standard index for tokens.
  FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType);
  // assert(tokenIndex != null);
  // Get an iterator over tokens.
  FSIterator<AnnotationFS> it = tokenIndex.iterator();
  // assert(it != null);
  // Now create sentences. We do this as follows: a sentence starts where
  // the first token after an EOS starts, and ends with an EOS.
  long time = System.currentTimeMillis();
  int endOfSentenceCounter = 0;
  it.moveToFirst();
  boolean lookForStart = true;
  int start = 0, end; // Initialize start to pacify compiler.
  FeatureStructure tokenFS, sentFS;
  while (it.isValid()) {
    if (lookForStart) {
      // If we're looking for the start of a sentence, just grab the start
      // of the current FS.
      start = it.get().getIntValue(startFeature);
      lookForStart = false;
    } else {
      // Check if we've reached the end of a sentence.
      tokenFS = it.get();
      if (tokenFS.getFeatureValue(tokenTypeFeature).getType() == eosType) {
        end = tokenFS.getIntValue(endFeature);
        sentFS = cas.createFS(sentenceType);
        sentFS.setIntValue(startFeature, start);
        sentFS.setIntValue(endFeature, end);
        cas.getIndexRepository().addFS(sentFS);
        ++endOfSentenceCounter;
        lookForStart = true;
      }
    }
    it.moveToNext();
  }
  time = System.currentTimeMillis() - time;
  // System.out.println("Created " + endOfSentenceCounter + " sentences: " + new TimeSpan(time));
}
 
Example 19
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * REM: modified from CasUtils..
 * 
 * Get a list of annotations constraint by a certain annotation. Iterates
 * over all annotations to find the covered annotations. Does not use
 * subiterators and does not respect type prioritites. Was adapted from
 * {@link Subiterator}. Uses the same approach except that type priorities
 * are ignored.
 * 
 * @param cas
 *            a CAS.
 * @param coveringAnnotation
 *            the covering annotation.
 * @see Subiterator
 */
public static List<Annotation> selectCovered(CAS cas,
        AnnotationFS coveringAnnotation) {
    final int begin = coveringAnnotation.getBegin();
    final int end = coveringAnnotation.getEnd();

    final List<Annotation> list = new ArrayList<Annotation>();
    final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();

    // Try to seek the insertion point.
    it.moveTo(coveringAnnotation);

    // If the insertion point is beyond the index, move back to the last.
    if (!it.isValid()) {
        it.moveToLast();
        if (!it.isValid()) {
            return list;
        }
    }

    // Ignore type priorities by seeking to the first that has the same
    // begin
    boolean moved = false;
    while (it.isValid() && (it.get()).getBegin() >= begin) {
        it.moveToPrevious();
        moved = true;
    }

    // If we moved, then we are now on one starting before the requested
    // begin, so we have to
    // move one ahead.
    if (moved) {
        it.moveToNext();
    }

    // If we managed to move outside the index, start at first.
    if (!it.isValid()) {
        it.moveToFirst();
    }

    // Skip annotations whose start is before the start parameter.
    while (it.isValid() && (it.get()).getBegin() < begin) {
        it.moveToNext();
    }

    boolean strict = true;
    while (it.isValid()) {
        AnnotationFS a = it.get();
        if (!(a instanceof Annotation))
            continue;

        // If the start of the current annotation is past the end parameter,
        // we're done.
        if (a.getBegin() > end) {
            break;
        }
        it.moveToNext();
        if (strict && a.getEnd() > end) {
            continue;
        }

        checkArgument(a.getBegin() >= coveringAnnotation.getBegin(),
                "Illegal begin " + a.getBegin() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");
        checkArgument(
                a.getEnd() <= coveringAnnotation.getEnd(),
                "Illegal end " + a.getEnd() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");

        if (!a.equals(coveringAnnotation) && !BlueCasUtil.isDocAnnot(a)) {
            list.add((Annotation) a);
        }
    }
    return unmodifiableList(list);
}
 
Example 20
Source File: ConsumerCasUtils.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Returns an int value of a given Feature Structure.
 *
 * @param aCasView -
 *          CAS instance to retrieve data from
 * @param aTypeS -
 *          Feature Type
 * @param aFeatS -
 *          Feature Structure
 * @return - feature value as int
 */
public static int getIntFeatValue(CAS aCasView, String aTypeS, String aFeatS) {
  int result = 0;
  FSIterator idIter = aCasView.getAnnotationIndex(aCasView.getTypeSystem().getType(aTypeS)).iterator();
  while (idIter != null && idIter.isValid()) {
    org.apache.uima.cas.FeatureStructure idFS = idIter.get();
    result = idFS.getIntValue(aCasView.getTypeSystem().getFeatureByFullName(aTypeS + ":" + aFeatS));
    idIter.moveToNext();
  }
  return result;
}