uima-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marshall Schor <...@schor.com>
Subject Re: Getting parent annotation
Date Wed, 07 Jan 2009 18:32:55 GMT
Hi Roberto -

It might be nice to extract this onto the UIMA Wiki - maybe start a new
section for things like this :-)

-Marshall

Roberto Franchini wrote:
> On Wed, Jan 7, 2009 at 11:55 AM, Roberto Franchini
> <ro.franchini@gmail.com> wrote:
>   
>> Hi,
>> I need to get a parent annotation of a given one.
>> Example: given a Token annotation, I want to know the parent Sentence.
>> I need to extract it from a cas. Maybe filters or constraint?
>> I mean a sort of "parentIterator", as the subiterator, but "reversed" :)
>> Regards,
>> Roberto
>>     
>
> Here's the answer with filtered iterator:
>
> public static FSIterator getAnnotationParent(JCas jcas, Annotation
> son, Type parent) {
> 		ConstraintFactory cf = jcas.getConstraintFactory();
> 		
> 		FSIntConstraint beginCons = cf.createIntConstraint();
> 		beginCons.leq(son.getBegin());
> 		
> 		FSIntConstraint endCons = cf.createIntConstraint();
> 		endCons.geq(son.getEnd());
> 		
> 		FSTypeConstraint typeCons = cf.createTypeConstraint();
> 		
> 		Type type = jcas.getTypeSystem().getType(parent.getName());
> 		typeCons.add(type);
> 		
> 		Feature beginFeature = type.getFeatureByBaseName("begin");
> 		FeaturePath beginPath = jcas.createFeaturePath();
> 		beginPath.addFeature(beginFeature);
>
> 		Feature endFeature = type.getFeatureByBaseName("end");
> 		FeaturePath endPath = jcas.createFeaturePath();
> 		endPath.addFeature(endFeature);
>
> 		FSMatchConstraint begin = cf.embedConstraint(beginPath, beginCons);
> 		
> 		FSMatchConstraint end  = cf.embedConstraint(endPath, endCons);
> 		
> 		FSMatchConstraint beginAndEnd  = cf.and(begin, end);
> 		
> 		FSMatchConstraint beginAndEndAndType  = cf.and(beginAndEnd, typeCons);
> 		FSIterator filteredIterator =
> jcas.createFilteredIterator(jcas.getAnnotationIndex(parent).iterator(),
> beginAndEndAndType);
> 		
> 		return filteredIterator;
>
> 	}
>
>
> Here's a test (junit).
>
> 	@Test
> 	public void getParent() throws Exception {
>
> 		AnalysisEngine tokenizer = AnnotatorFactory.createTokenizer();
> //this is my own factory for tests!!!
> 		JCas jcas = tokenizer.newJCas();
> 		jcas.setDocumentText("My name is .");
> 		Sentence sentence = new Sentence(jcas, 0, jcas.getDocumentText().length());
> 		sentence.addToIndexes();
> 		tokenizer.process(jcas);
> 		FSIterator iterator = jcas.getAnnotationIndex(Token.type).iterator();
> 		iterator.moveToNext();
> 		Token token = (Token) iterator.get();
>
> 		FSIterator parentIt = CasUtil.getAnnotationParent(jcas, token,
> sentence.getType());
> 		assertNotNull(parentIt);
>
> 		parentIt.moveToFirst();
> 		Sentence sentence2 = (Sentence) parentIt.get();
> 		assertNotNull(sentence2);
> 		assertEquals(sentence.getCoveredText(), sentence2.getCoveredText());
>
> 	}
>
> It works :)
> Roberto
>   

Mime
View raw message