/** * Create a new combo iterator. * * @param cas * The CAS we're operating on. * @param upper * The type of the upper iterator, e.g., sentence. * @param lower * The type of the lower iterator, e.g., token. */ public AnnotationComboIterator(CAS cas, Type upper, Type lower) { this.upperIt = cas.getAnnotationIndex(upper).iterator(); this.lowerIt = cas.getAnnotationIndex(lower).iterator(); this.upperIt.moveToFirst(); this.lowerIt.moveToFirst(); if (this.upperIt.isValid()) { final AnnotationFS upperFS = this.upperIt.get(); this.upperBegin = upperFS.getBegin(); this.upperEnd = upperFS.getEnd(); } else { this.nextLowerChecked = true; } }
@Override public void moveToFirst() { currentIt.moveToFirst(); }
public void moveToFirst() { currentIt.moveToFirst(); }
/** * Move the iterator so that the next call to {@link #next() next()} will return the first element * in the index (for a non-empty index). */ public void moveToStart() { // Move the forward pointer to the start. this.forward.moveToFirst(); // Move the back pointer past the start. this.back.moveToFirst(); this.back.moveToPrevious(); }
public FSIteratorAdapter(final FSIterator<T> aIterator) { index = aIterator.copy(); index.moveToFirst(); }
/** * @see org.apache.uima.cas.FSIterator#moveToFirst() */ public void moveToFirst() { this.nonFlatIterator.moveToFirst(); this.flatIterator.moveToFirst(); }
public void moveToFirst() { this.it.moveToFirst(); adjustForConstraintForward(); }
public void moveToFirst() { // Go through the iterators, starting with the first one this.iteratorIndex = 0; while (this.iteratorIndex < this.iterators.size()) { FSIterator<T> it = this.iterators.get(this.iteratorIndex); // Reset iterator to first position it.moveToFirst(); // If the iterator is valid (i.e., non-empty), return... if (it.isValid()) { return; } // ...else try the next one ++this.iteratorIndex; } // If we get here, all iterators are empty. }
public static <FST extends FeatureStructure> void fill(FSIterator<FST> srcIter, Collection<FST> destCol) { srcIter.moveToFirst(); while (srcIter.isValid()) { destCol.add(srcIter.get()); srcIter.moveToNext(); } }
public void moveToNext() { // No point in going anywhere if iterator is not valid. if (!isValid()) { return; } // Grab current iterator and inc. FSIterator<T> current = this.iterators.get(this.iteratorIndex); current.moveToNext(); // If we're ok with the current iterator, return. if (current.isValid()) { return; } ++this.iteratorIndex; while (this.iteratorIndex < this.iterators.size()) { current = this.iterators.get(this.iteratorIndex); current.moveToFirst(); if (current.isValid()) { return; } ++this.iteratorIndex; } // If we get here, the iterator is no longer valid, there are no more elements. }
@Override public int size() { // Unfortunately FSIterator does not expose the sizes of its internal collection, // neither the current position although FSIteratorAggregate has a private field // with that information. if (sizeCache == -1) { synchronized (this) { if (sizeCache == -1) { FSIterator<T> clone = index.copy(); clone.moveToFirst(); sizeCache = 0; while (clone.isValid()) { sizeCache++; clone.moveToNext(); } } } } return sizeCache; } }
/** * Return previous element if exists. Always save iterator position. * * @param iter * iterator * @return previous element if exists or null otherwise */ private static <T extends FeatureStructure> T lookupPrevious(FSIterator<T> iter) { iter.moveToPrevious(); T result; if (iter.isValid()) { result = iter.get(); iter.moveToNext(); } else { result = null; iter.moveToFirst(); } return result; }
/** * @see ListIterator#next() */ public T next() throws NoSuchElementException { // Throw exception if forward pointer is invalid. if (!this.forward.isValid()) { throw new NoSuchElementException(); } // Move the forward pointer. this.forward.moveToNext(); // Move the backward pointer. Need to check if it's valid. if (this.back.isValid()) { this.back.moveToNext(); } else { // This is guaranteed to yield a valid pointer, since otherwise, the // forward pointer couldn't have been valid. this.back.moveToFirst(); } // The back pointer is now pointing at the current forward element. return this.back.get(); }
/** * Create a new combo iterator. * * @param cas * The CAS we're operating on. * @param upper * The type of the upper iterator, e.g., sentence. * @param lower * The type of the lower iterator, e.g., token. */ public AnnotationComboIterator(CAS cas, Type upper, Type lower) { this.upperIt = cas.getAnnotationIndex(upper).iterator(); this.lowerIt = cas.getAnnotationIndex(lower).iterator(); this.upperIt.moveToFirst(); this.lowerIt.moveToFirst(); if (this.upperIt.isValid()) { final AnnotationFS upperFS = this.upperIt.get(); this.upperBegin = upperFS.getBegin(); this.upperEnd = upperFS.getEnd(); } else { this.nextLowerChecked = true; } }
wpIterator.moveToFirst(); while (wpIterator.hasNext()) { WordPair wp = (WordPair) wpIterator.next();
public static void checkForTheSameBoundaries(CAS cas, Class<? extends AnnotationFS> typeClass) { Type type = CasUtil.getType(cas, typeClass); FSIterator<AnnotationFS> iter = cas.getAnnotationIndex(type).iterator(); iter.moveToFirst(); if (!iter.isValid()) { return; } AnnotationOffsetComparator<AnnotationFS> cmp = AnnotationOffsetComparator.instance(AnnotationFS.class); AnnotationFS lastAnno = iter.get(); iter.moveToNext(); while (iter.isValid()) { AnnotationFS anno = iter.get(); if (cmp.compare(anno, lastAnno) == 0) { throw new IllegalStateException(String.format( "Annotations %s and %s have the same boundaries", lastAnno, anno)); } iter.moveToNext(); } }
protected RutaStream(CAS cas, Type basicType, NavigableMap<Integer, RutaBasic> beginAnchors, NavigableMap<Integer, RutaBasic> endAnchors, FilterManager filter, boolean lowMemoryProfile, boolean simpleGreedyForComposed, boolean emptyIsInvisible, TypeUsageInformation typeUsage, InferenceCrowd crowd) { super(); this.cas = cas; this.beginAnchors = beginAnchors; this.endAnchors = endAnchors; this.filter = filter; this.basicType = basicType; this.lowMemoryProfile = lowMemoryProfile; this.simpleGreedyForComposed = simpleGreedyForComposed; this.emptyIsInvisible = emptyIsInvisible; this.typeUsage = typeUsage; this.crowd = crowd; AnnotationFS additionalWindow = filter.getWindowAnnotation(); updateIterators(cas, basicType, filter, additionalWindow); // really an if? sub it of basic should fix this if (additionalWindow == null) { documentAnnotation = cas.getDocumentAnnotation(); documentAnnotationType = getCas().getDocumentAnnotation().getType(); basicIt.moveToFirst(); } else { documentAnnotation = additionalWindow; documentAnnotationType = filter.getWindowType(); } }
/** * We initially used the AnnotationIndex.subiterator() but ran into issues that we wanted to work around concerning * the use of type priorities when the window and windowed annotations have the same begin and end offsets. By using * our own iterator we can back up to the first annotation that has the same begining as the window annotation and * avoid having to set type priorities. * * @param jCas * @param windowAnnotation * @return an FSIterator that is at the correct position */ private static FSIterator<?> initializeWindowCursor(JCas jCas, Annotation windowAnnotation) { FSIterator<?> cursor = jCas.getAnnotationIndex().iterator(); cursor.moveTo(windowAnnotation); while (cursor.isValid() && ((Annotation) cursor.get()).getBegin() >= windowAnnotation.getBegin()) { cursor.moveToPrevious(); } if (cursor.isValid()) { cursor.moveToNext(); } else { cursor.moveToFirst(); } return cursor; }
/** * We initially used the AnnotationIndex.subiterator() but ran into issues * that we wanted to work around concerning the use of type priorities when * the window and windowed annotations have the same begin and end offsets. * By using our own iterator we can back up to the first annotation that has * the same begining as the window annotation and avoid having to set type * priorities. * * @param jCas * @param windowAnnotation * @return an FSIterator that is at the correct position */ private static FSIterator<?> initializeWindowCursor(final JCas jCas, final Annotation windowAnnotation) { final FSIterator<?> cursor = jCas.getAnnotationIndex().iterator(); cursor.moveTo(windowAnnotation); while (cursor.isValid() && (((Annotation) cursor.get()).getBegin() >= windowAnnotation .getBegin())) cursor.moveToPrevious(); if (cursor.isValid()) cursor.moveToNext(); else cursor.moveToFirst(); return cursor; }
private Annotation getNextBestAnnotation(Annotation source, JCas jcas) { FSIterator<Annotation> iterator = jcas.getAnnotationIndex().iterator(source); Annotation best = null; if (iterator.isValid()) { Annotation annotation = iterator.get(); best = annotation; } else { Annotation dummy = new Annotation(jcas, source.getBegin(), source.getBegin() + 1); iterator = jcas.getAnnotationIndex().iterator(dummy); if (!iterator.isValid()) { if ((jcas.getDocumentText().length() / 2) > source.getBegin()) { iterator.moveToFirst(); if (iterator.isValid()) { Annotation annotation = iterator.get(); best = annotation; } } else { iterator.moveToLast(); if (iterator.isValid()) { Annotation annotation = iterator.get(); best = annotation; } } } } return best; }