|
| 1 | +package org.texttechnologylab.DockerUnifiedUIMAInterface.tools; |
| 2 | + |
| 3 | +import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine; |
| 4 | +import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.net.UnknownHostException; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.apache.uima.analysis_engine.AnalysisEngine; |
| 14 | +import org.apache.uima.analysis_engine.AnalysisEngineProcessException; |
| 15 | +import org.apache.uima.cas.CASException; |
| 16 | +import org.apache.uima.fit.factory.JCasFactory; |
| 17 | +import org.apache.uima.fit.util.JCasUtil; |
| 18 | +import org.apache.uima.jcas.JCas; |
| 19 | +import org.apache.uima.jcas.cas.Sofa; |
| 20 | +import org.apache.uima.resource.ResourceInitializationException; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.texttechnologylab.DockerUnifiedUIMAInterface.DUUIComposer; |
| 27 | +import org.texttechnologylab.DockerUnifiedUIMAInterface.driver.DUUIUIMADriver; |
| 28 | +import org.xml.sax.SAXException; |
| 29 | + |
| 30 | +import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence; |
| 31 | +import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token; |
| 32 | + |
| 33 | +public class TestAnnotationDropper { |
| 34 | + static JCas jCas; |
| 35 | + static DUUIComposer composer; |
| 36 | + |
| 37 | + static final List<String[]> sentences = Arrays.asList( |
| 38 | + new String[] { "This", "is", "a", "sentence", "." }, |
| 39 | + new String[] { "This", "is", "another", "sentence", "." }, |
| 40 | + new String[] { "This", "is", "a", "third", "sentence", "." }); |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void setUp() throws ResourceInitializationException { |
| 44 | + try { |
| 45 | + jCas = JCasFactory.createJCas(); |
| 46 | + } catch (ResourceInitializationException | CASException e) { |
| 47 | + throw new ResourceInitializationException(e); |
| 48 | + } |
| 49 | + resetCas(); |
| 50 | + |
| 51 | + Assertions.assertEquals(3, JCasUtil.select(jCas, Sentence.class).size()); |
| 52 | + Assertions.assertEquals(16, JCasUtil.select(jCas, Token.class).size()); |
| 53 | + |
| 54 | + try { |
| 55 | + composer = new DUUIComposer() |
| 56 | + .withSkipVerification(true) |
| 57 | + .withWorkers(1); |
| 58 | + } catch (URISyntaxException e) { |
| 59 | + throw new ResourceInitializationException(e); |
| 60 | + } |
| 61 | + |
| 62 | + DUUIUIMADriver uimaDriver = new DUUIUIMADriver().withDebug(false); |
| 63 | + composer.addDriver(uimaDriver); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + public void afterEach() throws IOException, SAXException { |
| 68 | + composer.resetPipeline(); |
| 69 | + resetCas(); |
| 70 | + } |
| 71 | + |
| 72 | + static void resetCas() { |
| 73 | + jCas.reset(); |
| 74 | + jCas.setDocumentText(sentences.stream().flatMap(Arrays::stream).collect(Collectors.joining(" "))); |
| 75 | + int tokenOffset = 0; |
| 76 | + int sentenceOffset = 0; |
| 77 | + for (String[] sentence : sentences) { |
| 78 | + String text = String.join(" ", sentence); |
| 79 | + jCas.addFsToIndexes(new Sentence(jCas, sentenceOffset, sentenceOffset + text.length())); |
| 80 | + sentenceOffset += text.length() + 1; |
| 81 | + for (String token : sentence) { |
| 82 | + jCas.addFsToIndexes(new Token(jCas, tokenOffset, tokenOffset + token.length())); |
| 83 | + tokenOffset += token.length() + 1; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @AfterAll |
| 89 | + static void afterAll() throws UnknownHostException { |
| 90 | + composer.shutdown(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testTypesToRetain() throws ResourceInitializationException, CASException { |
| 95 | + try { |
| 96 | + AnalysisEngine dropper = createEngine( |
| 97 | + AnnotationDropper.class, |
| 98 | + AnnotationDropper.PARAM_TYPES_TO_RETAIN, |
| 99 | + new String[] { |
| 100 | + Sofa._TypeName, |
| 101 | + org.apache.uima.jcas.tcas.DocumentAnnotation._TypeName, |
| 102 | + org.texttechnologylab.annotation.DocumentAnnotation._TypeName, |
| 103 | + Sentence._TypeName, |
| 104 | + }); |
| 105 | + |
| 106 | + try { |
| 107 | + dropper.process(jCas); |
| 108 | + } catch (AnalysisEngineProcessException e) { |
| 109 | + throw new RuntimeException(e); |
| 110 | + } |
| 111 | + |
| 112 | + Assertions.assertEquals(3, JCasUtil.select(jCas, Sentence.class).size()); |
| 113 | + Assertions.assertEquals(0, JCasUtil.select(jCas, Token.class).size()); |
| 114 | + } catch (Exception e) { |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testTypesToDrop() throws ResourceInitializationException, CASException { |
| 121 | + try { |
| 122 | + AnalysisEngine dropper = createEngine( |
| 123 | + AnnotationDropper.class, |
| 124 | + AnnotationDropper.PARAM_TYPES_TO_DROP, |
| 125 | + new String[] { |
| 126 | + Token._TypeName, |
| 127 | + }); |
| 128 | + |
| 129 | + try { |
| 130 | + dropper.process(jCas); |
| 131 | + } catch (AnalysisEngineProcessException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + |
| 135 | + Assertions.assertEquals(3, JCasUtil.select(jCas, Sentence.class).size()); |
| 136 | + Assertions.assertEquals(0, JCasUtil.select(jCas, Token.class).size()); |
| 137 | + } catch (Exception e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testTypesToRetainDUUI() { |
| 144 | + try { |
| 145 | + composer.add(new DUUIUIMADriver.Component(createEngineDescription( |
| 146 | + AnnotationDropper.class, |
| 147 | + AnnotationDropper.PARAM_TYPES_TO_RETAIN, |
| 148 | + new String[] { |
| 149 | + Sofa._TypeName, |
| 150 | + Sentence._TypeName, |
| 151 | + }))); |
| 152 | + |
| 153 | + try { |
| 154 | + composer.run(jCas); |
| 155 | + } catch (Exception e) { |
| 156 | + Assertions.fail("DUUIComposer failed", e); |
| 157 | + } |
| 158 | + |
| 159 | + Assertions.assertEquals(3, JCasUtil.select(jCas, Sentence.class).size()); |
| 160 | + Assertions.assertEquals(0, JCasUtil.select(jCas, Token.class).size()); |
| 161 | + } catch (Exception e) { |
| 162 | + throw new RuntimeException(e); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testTypesToDropDUUI() { |
| 168 | + try { |
| 169 | + composer.add(new DUUIUIMADriver.Component(createEngineDescription( |
| 170 | + AnnotationDropper.class, |
| 171 | + AnnotationDropper.PARAM_TYPES_TO_DROP, |
| 172 | + new String[] { |
| 173 | + Token._TypeName, |
| 174 | + }))); |
| 175 | + |
| 176 | + try { |
| 177 | + composer.run(jCas); |
| 178 | + } catch (Exception e) { |
| 179 | + Assertions.fail("DUUIComposer failed", e); |
| 180 | + } |
| 181 | + |
| 182 | + Assertions.assertEquals(3, JCasUtil.select(jCas, Sentence.class).size()); |
| 183 | + Assertions.assertEquals(0, JCasUtil.select(jCas, Token.class).size()); |
| 184 | + } catch (Exception e) { |
| 185 | + throw new RuntimeException(e); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments