Roberto Franchini wrote:
On 5/10/07, Katherine Enderling <[EMAIL PROTECTED]> 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:
[EMAIL PROTECTED]";
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

Reply via email to