uima-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Eckart de Castilho <eckar...@tk.informatik.tu-darmstadt.de>
Subject Re: Iterate over annotations with multiple types
Date Wed, 07 Sep 2011 13:06:47 GMT
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

-- 
------------------------------------------------------------------- 
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab 
FB 20 Computer Science Department      
Technische Universität Darmstadt 
Hochschulstr. 10, D-64289 Darmstadt, Germany 
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckartde@tk.informatik.tu-darmstadt.de 
www.ukp.tu-darmstadt.de 
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
------------------------------------------------------------------- 





Mime
View raw message