You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Katherine Enderling <ka...@cognia.com> on 2007/05/10 11:19:00 UTC
Creating a JCas for unit testing
In order to do unit testing on my Analysis Engines, I would like to create a
basic JCas object from scratch and set its document text. What is the
correct way to do this?
Thanks for your help!
Katherine
Re: Creating a JCas for unit testing
Posted by "J. William Murdock" <bi...@murdocks.org>.
I don't agree with Marshall's example. I've tried to do exactly what
you have proposed below, and I believe there is just no way to employ
Java 5's new for loops on an FSIndex or FSIterator without building a
new wrapper class. In particular:
1) The new Java 5 syntax applies only to arrays and to classes that
implement the Iterable interface (which defines the Iterator<T>
iterator() method).
2) The Java Iterator class does not implement Iterable; it does not have
an iterator() method (if they had put this into Java 5, it would have
broken all of the user defined iterators that already existed).
3) Java 5 Collection implements Iterable, but an FSIndex is not a
Collection and it does not implement Iterable (even though it does have
an iterator() method, as Iterable does).
However, if you are doing this often, it may be worth making such a
wrapper class, e.g.:
public class FSIndexIterable<T extends FeatureStructure> implements
Iterable<T> {
FSIndex index;
public FSIndexIterable(FSIndex index) {
this.index = index;
}
public Iterator<T> iterator() {
return (Iterator<T>)index.iterator();
}
}
And then making the following changes to Marshall's example:
FSIndex anIndex = aCas.getAnnotationIndex(Email.type);
* FSIndexIterable<AnnotationFS> anIndexIterable = new
FSIndexIterable<AnnotationFS>(anIndex);*
Iterator<AnnotationFS> anIter =
(Iterator<AnnotationFS>)anIndex.iterator();
assertTrue(anIter.hasNext());
int i = 0;
for (AnnotationFS annot : *anIndexIterable*) {
assertEquals("it.celi.type.Email",
annot.getType().getName()); assertEquals(mail[i],
annot.getCoveredText());
i++;
}
As with Marshall's example, this is untested so corrections are welcome.
Of course, it would be convenient if FSIndex just implemented Iterable
so we didn't need a wrapper here. However, I do not know if there is
any way to do that without breaking compatibility with earlier versions
of Java.
- Bill Murdock (IBM internal UIMA user but not a UIMA framework
developer or official spokesperson)
In particular, Java 5's Iterator interface does
Marshall Schor wrote:
> Nice example, Roberto :-)
>
> If you're using Java 5 or later, since the FSIterator is a
> sub-interface of the Java Iterator, you can use the Java 5 style of
> iterating. Here's an example (not tested - so there may be silly
> mistakes - if so, please correct :-) :
>
>
> // get annotation iterator for this CAS (Email is my type
> FSIndex anIndex = aCas.getAnnotationIndex(Email.type);
> Iterator<AnnotationFS> anIter =
> (Iterator<AnnotationFS>)anIndex.iterator();
> assertTrue(anIter.hasNext());
> int i = 0;
>
> for (AnnotationFS annot : anIter) {
> assertEquals("it.celi.type.Email",
> annot.getType().getName());
> assertEquals(mail[i], annot.getCoveredText());
> i++;
> }
>
> -Marshall
>
Re: Creating a JCas for unit testing
Posted by Marshall Schor <ms...@schor.com>.
Roberto Franchini wrote:
> On 5/10/07, Katherine Enderling <ka...@cognia.com> wrote:
>> In order to do unit testing on my Analysis Engines, I would like to
>> create a
>> basic JCas object from scratch and set its document text. What is the
>> correct way to do this?
>> Thanks for your help!
>
> I'm learning UIMA in a TDL way (Test Driven Learning :) ), and this is
> a snippet of a test method:
> ...
>
> File file = getFile("it/celi/ae/emailAeDescriptor.xml"); //get a
> file from classpath
>
> XMLInputSource in = new XMLInputSource(file);
> ResourceSpecifier specifier;
> specifier =
> UIMAFramework.getXMLParser().parseResourceSpecifier(in);
> AnalysisEngine analysisEngine =
> UIMAFramework.produceAnalysisEngine(specifier);
>
> JCas aCas = analysisEngine.newJCas();
>
> String document= "This is a test document with, this is an email:
> frank@domain.it";
>
> aCas.setDocumentText(document);
> analysisEngine.process(aCas);
>
> // get annotation iterator for this CAS (Email is my type
> FSIndex anIndex = aCas.getAnnotationIndex(Email.type);
> FSIterator anIter = anIndex.iterator();
> assertTrue(anIter.hasNext());
> int i = 0;
> while (anIter.isValid()) {
> AnnotationFS annot = (AnnotationFS) anIter.get();
> assertEquals("it.celi.type.Email",
> annot.getType().getName());
>
> assertEquals(mail[i], annot.getCoveredText());
>
> anIter.moveToNext();
> i++;
> }
> ......
> Take a look to the uima surces and test sources too to learn how to
> test uima artifact in isolation.
> I hope this could help,
> Roberto
>
>
Nice example, Roberto :-)
If you're using Java 5 or later, since the FSIterator is a sub-interface
of the Java Iterator, you can use the Java 5 style of iterating. Here's
an example (not tested - so there may be silly mistakes - if so, please
correct :-) :
// get annotation iterator for this CAS (Email is my type
FSIndex anIndex = aCas.getAnnotationIndex(Email.type);
Iterator<AnnotationFS> anIter =
(Iterator<AnnotationFS>)anIndex.iterator();
assertTrue(anIter.hasNext());
int i = 0;
for (AnnotationFS annot : anIter) {
assertEquals("it.celi.type.Email",
annot.getType().getName());
assertEquals(mail[i], annot.getCoveredText());
i++;
}
-Marshall
Re: Creating a JCas for unit testing
Posted by Roberto Franchini <ro...@gmail.com>.
On 5/10/07, Katherine Enderling <ka...@cognia.com> wrote:
> In order to do unit testing on my Analysis Engines, I would like to create a
> basic JCas object from scratch and set its document text. What is the
> correct way to do this?
> Thanks for your help!
I'm learning UIMA in a TDL way (Test Driven Learning :) ), and this is
a snippet of a test method:
...
File file = getFile("it/celi/ae/emailAeDescriptor.xml"); //get a
file from classpath
XMLInputSource in = new XMLInputSource(file);
ResourceSpecifier specifier;
specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
AnalysisEngine analysisEngine =
UIMAFramework.produceAnalysisEngine(specifier);
JCas aCas = analysisEngine.newJCas();
String document= "This is a test document with, this is an email:
frank@domain.it";
aCas.setDocumentText(document);
analysisEngine.process(aCas);
// get annotation iterator for this CAS (Email is my type
FSIndex anIndex = aCas.getAnnotationIndex(Email.type);
FSIterator anIter = anIndex.iterator();
assertTrue(anIter.hasNext());
int i = 0;
while (anIter.isValid()) {
AnnotationFS annot = (AnnotationFS) anIter.get();
assertEquals("it.celi.type.Email", annot.getType().getName());
assertEquals(mail[i], annot.getCoveredText());
anIter.moveToNext();
i++;
}
......
Take a look to the uima surces and test sources too to learn how to
test uima artifact in isolation.
I hope this could help,
Roberto
--
Roberto "FRANK" Franchini
CELI s.r.l. (http://www.celi.it) - C.so Moncalieri 21 - 10131 Torino - ITALY
Tel +39-011-6600814 - Fax +39-011-6600687
jabber:ro.franchini@gmail.com skype:ro.franchini