Isn't this slow? Because it then needs to iterate over every
single AnnotationFS inside my CAS.
Jörn
On 9/7/11 3:06 PM, Richard Eckart de Castilho wrote:
Hi Jörn,
what is the best way to iterate over annotations which have
different types?
you can use a filtered iterator - more or less like this:
CAS cas = jcas.getCas();
ConstraintFactory cf = ConstraintFactory.instance();
FSIterator<Annotation> iterator =
jcas.getAnnotationIndex().iterator();
Type tokenType = jcas.getCasType(Token.type);
Type sentenceType = jcas.getCasType(Sentence.type);
// Restrict to Tokens
FSTypeConstraint typeConstraint1 = cf.createTypeConstraint();
typeConstraint.add(tokenType);
// Restrict to Tokens
FSTypeConstraint typeConstraint2 = cf.createTypeConstraint();
typeConstraint.add(sentenceType);
// Combine both constraints using "or"
FSMatchConstraint disjunction = cf.or(typeConstraint1,
typeConstraint2);
// Create and use the filtered iterator
FSIterator<Annotation> filteredIterator =
cas.createFilteredIterator(iterator, disjunction);
while(filteredIterator.hasNext()) {
System.out.println(filteredIterator.next().getCoveredText());
}
Cheers,
Richard