You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Benjamin Sznajder <be...@il.ibm.com> on 2007/09/24 21:41:40 UTC
Iterating over the annotations (or FS)
Hi,
I would like to write a CASMerger that knows to iterate over the
annotations in the CAS and to copy them to the "mergedCAS".
My question is : is there a way to iterate over these annotations if I do
not know the typeSystem and the annotators this CAS passed through? I would
like to write an as most generic as possible CASMerger class.
Many thanks
Benjamin
Re: Iterating over the annotations (or FS)
Posted by Thilo Goetz <tw...@gmx.de>.
Benjamin Sznajder wrote:
> Hi Matthew
>
> Thank you for your reply.
> Unfortunately, it does not iterate over the whole annotations.
It does iterate over all annotations, your code also
copies FSs that are not annotations.
>
> I wrote this one and it seems to work
>
> FSIndexRepository indexRep =
> aJCas.getFSIndexRepository();
> Iterator<String> labelIterators = indexRep.getLabels();
> while (labelIterators.hasNext()) {
> String label = labelIterators.next();
> FSIndex fsIndex = indexRep.getIndex(label);
> FSIterator fsiterator = fsIndex.iterator();
> while (fsiterator.hasNext()) {
> FeatureStructure fs = (FeatureStructure)
> fsiterator.next();
> /// Copy the FeatureStructure using the CasCopier class.
>
> }
> }
Your code is fine, but there is now a convenience method
for accessing all indexed FSs of a certain type. So you
could simply do:
FSIterator fsIterator = jcas.getJFSIndexRepository().getAllIndexedFS(TOP.type);
Note that in your code, if you have more than one index for the
same type, you will copy some FSs more than once. You can avoid
this by using the above convenience method.
Or even simpler:
CasCopier.copyCas(CAS aSrcCas, CAS aDestCas, boolean aCopySofa)
However, I assume you saw this and have good reasons for not
using it :-)
--Thilo
Re: Iterating over the annotations (or FS)
Posted by Benjamin Sznajder <be...@il.ibm.com>.
Hi Matthew
Thank you for your reply.
Unfortunately, it does not iterate over the whole annotations.
I wrote this one and it seems to work
FSIndexRepository indexRep =
aJCas.getFSIndexRepository();
Iterator<String> labelIterators = indexRep.getLabels();
while (labelIterators.hasNext()) {
String label = labelIterators.next();
FSIndex fsIndex = indexRep.getIndex(label);
FSIterator fsiterator = fsIndex.iterator();
while (fsiterator.hasNext()) {
FeatureStructure fs = (FeatureStructure)
fsiterator.next();
/// Copy the FeatureStructure using the CasCopier class.
}
}
Benjamin
Matthew Campbell
<mcampbell@syrres
.com> To
uima-user@incubator.apache.org
24/09/2007 22:04 cc
Subject
Please respond to Re: Iterating over the annotations
uima-user@incubat (or FS)
or.apache.org
Benjamin:
I'm doing something similar I think, and my method for copying
annotations generically is shown below. The UIMA gurus out there can
feel free to correct it, as I put it together as best I can from
wandering around the javadocs. The key is the copyFs() method, which
deep copies the annotation and doesn't require any sort of TypeSystem
knowledge to use. I'm using the CasCopier to move annotations between
two Sofa views here, but the same could be done to move from one Cas to
another I think. In that case I would remove the stuff about updating
the 'sofa' feature.
JCas originalContent = aJCAS.getView("originalContent");
JCas mergedContent = aJCAS.createView("mergedContent");
CasCopier copier = new CasCopier(originalContent .getCas(),
mergedContent.getCas());
//First, get all of the annotations in the originalContent so
//we can propagate them to the mergedContent view.
FSIterator iter = originalContent .getAnnotationIndex().iterator();
while (iter.hasNext()) {
Annotation currAnnot = (Annotation)iter.next();
//make a copy of the annotation
Annotation newAnnot = (Annotation)copier.copyFs(currAnnot);
Feature sofaFeature = newAnnot.getType().getFeatureByBaseName("sofa");
newAnnot.setFeatureValue(sofaFeature, mergedContent.getSofa());
mergedContent.addFsToIndexes(newAnnot);
}
As I said, it's a bit hacked together, but I think it's working fine for
me.
-Matt
Benjamin Sznajder wrote:
> Hi,
>
> I would like to write a CASMerger that knows to iterate over the
> annotations in the CAS and to copy them to the "mergedCAS".
>
> My question is : is there a way to iterate over these annotations if I do
> not know the typeSystem and the annotators this CAS passed through? I
would
> like to write an as most generic as possible CASMerger class.
>
> Many thanks
> Benjamin
>
>
>
Re: Iterating over the annotations (or FS)
Posted by Matthew Campbell <mc...@syrres.com>.
Benjamin:
I'm doing something similar I think, and my method for copying
annotations generically is shown below. The UIMA gurus out there can
feel free to correct it, as I put it together as best I can from
wandering around the javadocs. The key is the copyFs() method, which
deep copies the annotation and doesn't require any sort of TypeSystem
knowledge to use. I'm using the CasCopier to move annotations between
two Sofa views here, but the same could be done to move from one Cas to
another I think. In that case I would remove the stuff about updating
the 'sofa' feature.
JCas originalContent = aJCAS.getView("originalContent");
JCas mergedContent = aJCAS.createView("mergedContent");
CasCopier copier = new CasCopier(originalContent .getCas(),
mergedContent.getCas());
//First, get all of the annotations in the originalContent so
//we can propagate them to the mergedContent view.
FSIterator iter = originalContent .getAnnotationIndex().iterator();
while (iter.hasNext()) {
Annotation currAnnot = (Annotation)iter.next();
//make a copy of the annotation
Annotation newAnnot = (Annotation)copier.copyFs(currAnnot);
Feature sofaFeature = newAnnot.getType().getFeatureByBaseName("sofa");
newAnnot.setFeatureValue(sofaFeature, mergedContent.getSofa());
mergedContent.addFsToIndexes(newAnnot);
}
As I said, it's a bit hacked together, but I think it's working fine for me.
-Matt
Benjamin Sznajder wrote:
> Hi,
>
> I would like to write a CASMerger that knows to iterate over the
> annotations in the CAS and to copy them to the "mergedCAS".
>
> My question is : is there a way to iterate over these annotations if I do
> not know the typeSystem and the annotators this CAS passed through? I would
> like to write an as most generic as possible CASMerger class.
>
> Many thanks
> Benjamin
>
>
>