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

The following examples show how to use org.apache.uima.cas.FSIterator#isValid() . 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: AnnotationIteratorTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private int assertCountCmn(String msg, int expected, FSIterator<? extends Annotation> it) {
  msg = flatStateMsg(msg);   // add with-flattened-index if isSave is false
  int count = 0;
  callCount  ++;
  int fssStart;
  if (isSave) {
    fssStarts.add(fssStart = fss.size());
  } else {
    fssStart = fssStarts.get(callCount);
  }
  while (it.isValid()) {
    ++count;
    Annotation fs = it.next();
    if (showFSs) {
      System.out.format("assertCountCmn: %2d " + msg + "   %10s  %d - %d%n", count, fs.getType().getName(), fs.getBegin(), fs.getEnd() );
    }
    if (isSave) {
      fss.add(fs);
    } else {
      assertEquals(msg, fss.get(fssStart + count -1).hashCode(), fs.hashCode());
    }
  }
  assertEquals(msg, expected, count);
  return fssStart;
}
 
Example 2
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 3
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 4
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * This method exists simply as a convenience method for unit testing. It is not very efficient
 * and should not, in general be used outside the context of unit testing.
 * 
 * @param cas
 *          a CAS containing the annotation.
 * @param type
 *          a UIMA type.
 * @param index
 *          this can be either positive (0 corresponds to the first annotation of a type) or
 *          negative (-1 corresponds to the last annotation of a type.)
 * @return an annotation of the given type
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
public static AnnotationFS selectByIndex(CAS cas, Type type, int index) {
  if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) {
    throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type");
  }
  // withSnapshotIterators() not needed here since we return only one result   
  FSIterator<AnnotationFS> i = cas.getAnnotationIndex(type).iterator();
  int n = index;
  i.moveToFirst();
  if (n > 0) {
    while (n > 0 && i.isValid()) {
      i.moveToNext();
      n--;
    }
  }
  if (n < 0) {
    i.moveToLast();
    while (n < -1 && i.isValid()) {
      i.moveToPrevious();
      n++;
    }
  }

  return i.isValid() ? i.get() : null;
}
 
Example 5
Source File: FsIterator_aggregation_common.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void moveToNextNvc() {
  FSIterator<T> it = nonEmptyIterators[current_it_idx];
  it.moveToNextNvc();

  if (it.isValid()) {
    return;
  }
  
  final int nbrIt = nonEmptyIterators.length;
  for (int i = current_it_idx + 1; i < nbrIt; i++) {
    it = nonEmptyIterators[i];
    it.moveToFirst();
    if (it.isValid()) {
      current_it_idx = i;
      return;
    }
  }
  current_it_idx = -1;  // invalid position
}
 
Example 6
Source File: AnnotationWriter.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * print the cas content to the output file
 * 
 * @param aCAS
 *          CasContainer which has been populated by the TAEs
 * 
 * @throws ResourceProcessException
 *           if there is an error in processing the Resource
 * 
 * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(CAS)
 */
public synchronized void processCas(CAS aCAS) throws ResourceProcessException {
  try {
    // iterate and print annotations
    FSIterator<Annotation> typeIterator = aCAS.getCurrentView().<Annotation>getAnnotationIndex().iterator();

    for (typeIterator.moveToFirst(); typeIterator.isValid(); typeIterator.moveToNext()) {
      AnnotationFS annot = typeIterator.get();

      this.fileWriter.write(annot.getCoveredText());
      this.fileWriter.write(System.getProperty("line.separator"));
      this.fileWriter.write(annot.toString());
    }
    this.fileWriter.flush();
  } catch (Exception ex) {
    ex.printStackTrace();
  }
}
 
Example 7
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 8
Source File: FSTreeModel.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Update.
 *
 * @param indexName the index name
 * @param index the index
 * @param cas1 the cas 1
 */
public void update(String indexName, FSIndex index, CAS cas1) {
  // this.indexName = indexName;
  this.cas = (CASImpl) cas1;
  final int size = index.size();
  this.rootString = "<html><font color=green>" + indexName + "</font> - <font color=blue>"
          + index.getType().getName() + "</font> [" + size + "]</html>";
  this.root = new FSNode(this, FSNode.DISPLAY_NODE, null, 0, null);
  this.fss = new ArrayList<>();
  FSIterator<TOP> it = index.iterator();
  int count = 0;
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    TOP fs = it.get();
    this.fss.add(new FSNode(this, getNodeType(fs.getType()), fs, fs._id(), count));
    ++count;
  }
  List<FSTreeNode> kids = createArrayChildren(0, size, this.fss, this);
  this.root.setChildren(kids);
  Object[] path = new Object[1];
  path[0] = this.root;
  TreeModelEvent event = new TreeModelEvent(this.root, path);
  for (int i = 0; i < this.treeModelListeners.size(); i++) {
    this.treeModelListeners.get(i).treeStructureChanged(event);
  }
}
 
Example 9
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 10
Source File: ConsumerCasUtils.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public static FeatureStructure getTcasFS(CAS aCasView, String aTypeS) {
  org.apache.uima.cas.FeatureStructure idFS = null;
  Type type = aCasView.getTypeSystem().getType(aTypeS);
  if (type != null) {
    FSIterator<AnnotationFS> idIter = aCasView.getAnnotationIndex(type).iterator();
    while (idIter.isValid()) {
      idFS = idIter.get();
      idIter.moveToNext();
    }
  }
  return idFS;
}
 
Example 11
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 12
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 13
Source File: PrintAnnotations.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Prints all Annotations of a specified Type to a PrintStream.
 * 
 * @param aCAS
 *          the CAS containing the FeatureStructures to print
 * @param aAnnotType
 *          the Type of Annotation to be printed
 * @param aOut
 *          the PrintStream to which output will be written
 */
public static void printAnnotations(CAS aCAS, Type aAnnotType, PrintStream aOut) {
  // get iterator over annotations
  FSIterator iter = aCAS.getAnnotationIndex(aAnnotType).iterator();

  // iterate
  while (iter.isValid()) {
    FeatureStructure fs = iter.get();
    printFS(fs, aCAS, 0, aOut);
    iter.moveToNext();
  }
}
 
Example 14
Source File: FilteredIteratorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void iterAndCount2() {
  String lemma = "the";
  // create filtered iterator over Tokens of type 1
  FSIterator<AnnotationFS> it = cas.getAnnotationIndex(tokenType).iterator();
  FSStringConstraint type1Constraint = cas.getConstraintFactory().createStringConstraint();
  type1Constraint.equals(lemma);
  FeaturePath path = cas.createFeaturePath();
  path.addFeature(lemmaFeat);
  FSMatchConstraint cons = cas.getConstraintFactory().embedConstraint(path, type1Constraint);
  it = cas.createFilteredIterator(it, cons);

  int count = 0;
  for (it.moveToFirst(); it.isValid(); it.moveToNext()) {
    ++count;
  }

  // /////////////////////////////////////////////////////////////
  // Count instances of tokens with lemma "the".

  // Create an iterator over Token annotations.
  FSIndex<AnnotationFS> tokenIndex = cas.getAnnotationIndex(tokenType);
  FSIterator<AnnotationFS> tokenIt = tokenIndex.iterator();
  // Create a counter.
  int theCount = 0;
  // Iterate over the tokens.
  for (tokenIt.moveToFirst(); tokenIt.isValid(); tokenIt.moveToNext()) {
    AnnotationFS tok = tokenIt.get();
    if (tok.getStringValue(lemmaFeat).equals(lemma)) {
      ++theCount;
      // System.out.println("Found token: " + tok.getCoveredText());
    }
  }
  assertTrue(count == theCount);
  // System.out.println(
  // "Number of tokens with \"" + lemma + "\": " + theCount);
  // System.out.println("Number of tokens overall: " + tokenIndex.size());

  // System.out.println("Count: " + count);
  // assertTrue(count == 4);
}
 
Example 15
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 16
Source File: StatusCallbackListenerImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Called when the processing of a Document is completed. <br>
 * The process status can be looked at and corresponding actions taken.
 * 
 * @param aCas
 *          CAS corresponding to the completed processing
 * @param aStatus
 *          EntityProcessStatus that holds the status of all the events for aEntity
 */

public void entityProcessComplete(CAS aCas, EntityProcessStatus aStatus) {
  // if there is an error above the individual document level,
  // an entityProcessStatus is created with a null value for entity
  if (aCas == null) {
    for (int i = 0; i < aStatus.getFailedComponentNames().size(); i++) {
      if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
        UIMAFramework.getLogger(this.getClass()).logrb(
                Level.FINEST,
                this.getClass().getName(),
                "process",
                CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
                "UIMA_CPM_failed_component__FINEST",
                new Object[] { Thread.currentThread().getName(),
                    ((String) aStatus.getFailedComponentNames().get(i)) });
      }
    }
    for (int i = 0; i < aStatus.getExceptions().size(); i++) {
      if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
        UIMAFramework.getLogger(this.getClass()).logrb(
                Level.FINEST,
                this.getClass().getName(),
                "process",
                CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
                "UIMA_CPM_component_exception__FINEST",
                new Object[] { Thread.currentThread().getName(),
                    (aStatus.getExceptions().get(i)).toString() });
      }
    }
    return;
  }
  try {
    entityCount++;
    int dataSize = 0;
    // get size here
    Type t = aCas.getTypeSystem().getType("uima.cpm.FileLocation");
    Feature f = t.getFeatureByBaseName("DocumentSize");
    FSIterator fsI = aCas.getAnnotationIndex(t).iterator();
    if (fsI.isValid()) {
      dataSize = fsI.get().getIntValue(f);
    }

    size += dataSize;
    // to handle exceptions occured in any of the components for the entity
    if (aStatus.isException()) {
      for (int q = 0; q < aStatus.getExceptions().size(); q++) {
        Exception e = (Exception) aStatus.getExceptions().get(q);
        e.printStackTrace();
      }
    }
  } catch (Exception io) {
    UIMAFramework.getLogger(this.getClass()).log(Level.WARNING, "", io);
  }
}
 
Example 17
Source File: VinciBinaryAnalysisEngineService_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Analyzes a given document by a CasObjectProcessor. 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 aRequestFrame          request frame
 * @return VinciFrame containing XCAS translated into a set of Vinci subframes.
 * @throws ServiceException the service exception
 */
private Transportable analyze(AFrame aRequestFrame) throws ServiceException {
  CAS cas = null;
  try {
    // get CAS object from pool
    cas = mCasPool.getCas(0);

    // deserialize into CAS object
    byte[] casBytes = aRequestFrame.fgetTrueBinary("BinaryCAS");
    CASCompleteSerializer serializer = (CASCompleteSerializer) SerializationUtils
            .deserialize(casBytes);
    Serialization.deserializeCASComplete(serializer, (CASMgr) cas);

    long annotStartTime = System.currentTimeMillis();
    // invoke Analysis Engine
    mAE.processCas(cas);
    int annotationTime = (int) (System.currentTimeMillis() - annotStartTime);
    if (debug) {
      System.out.println("Annotation took: " + annotationTime + "ms");
    }

    // serialize CAS
    AFrame responseFrame = new AFrame();
    CASSerializer responseSerializer = Serialization.serializeCAS(cas);
    byte[] responseCasBytes = SerializationUtils.serialize(responseSerializer);
    responseFrame.fsetTrueBinary("BinaryCAS", responseCasBytes);
    // also add annotation time
    responseFrame.fset(Constants.ANNOTATION_TIME, annotationTime);

    // 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.FINE, "CAS Annotation Count::" + totalAnnots);

    return responseFrame;
  } catch (Throwable ex) {
    UIMAFramework.getLogger().log(Level.SEVERE, "", ex);
    throw new ServiceException("Unexpected exception in analyze(): " + ex);
  } finally {
    // release CAS back to pool
    if (cas != null) {
      mCasPool.releaseCas(cas);
    }
  }
}
 
Example 18
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 19
Source File: SerializationNoMDTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void checkSentences() {
  TypeSystem ts = cas.getTypeSystem();
  Type localSentenceType = ts.getType(SENT_TYPE);
  // Feature tokenTypeFeature = ts.getFeatureByFullName(TOKEN_TYPE_FEAT);
  // Feature startFeature = ts.getFeatureByFullName(CAS.FEATURE_BASE_NAME_BEGIN);
  // Feature endFeature = ts.getFeatureByFullName(CAS.FEATURE_BASE_NAME_END);

  // Print the first few sentences.
  // System.out.println("\nThe first 10 sentences:\n");
  FSIndex<AnnotationFS> sentenceIndex = cas.getAnnotationIndex(localSentenceType);
  FSIterator<AnnotationFS> it = sentenceIndex.iterator();
  AnnotationFS sentFS;
  if (it.isValid()) {
    sentFS = it.get();
    assertTrue(sentFS.getCoveredText() != null);
  }
  // int counter = 0;
  String text = cas.getDocumentText();
  assertTrue(text != null);
  // while (it.isValid() && counter < 10) {
  // sentFS = (AnnotationFS)it.get();
  // System.out.println(
  // "Sentence: "
  // + sentFS.getCoveredText());
  // it.moveToNext();
  // ++counter;
  // }

  // Now get an iterator over all annotations.
  FSIndex<AnnotationFS> annotIndex = cas.getAnnotationIndex();
  // System.out.println("\nNumber of annotations in index: " + annotIndex.size());

  // Print the first few sentences.
  // System.out.println("The first 50 annotations:\n");

  it = annotIndex.iterator();
  // assert(it.isValid());
  // counter = 0;
  // AnnotationFS fs;
  // while (it.isValid() && counter < 50) {
  // fs = (AnnotationFS)it.get();
  // System.out.print(fs.getType().getName() + ": ");
  // if (fs.getType().getName().equals(CASMgr.DOCUMENT_TYPE)) {
  // // When we see the document, we don't print the whole text ;-)
  // System.out.println("...");
  // } else {
  // System.out.println(
  // fs.getCoveredText());
  // }
  // it.moveToNext();
  // ++counter;
  // }
}
 
Example 20
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));
}