001 /*
002 * TestJape.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Hamish Cunningham, 23/Feb/00
013 *
014 * $Id: TestJape.java 13726 2011-04-20 16:31:46Z ian_roberts $
015 */
016
017 package gate.jape.functest;
018
019 import gate.Annotation;
020 import gate.AnnotationSet;
021 import gate.Corpus;
022 import gate.CorpusController;
023 import gate.Document;
024 import gate.Factory;
025 import gate.FeatureMap;
026 import gate.creole.ExecutionException;
027 import gate.creole.ResourceInstantiationException;
028 import gate.persist.PersistenceException;
029 import gate.util.AnnotationDiffer;
030 import gate.util.Files;
031 import gate.util.GateException;
032 import gate.util.InvalidOffsetException;
033 import gate.util.Out;
034 import gate.util.persistence.PersistenceManager;
035
036 import java.io.IOException;
037 import java.io.PrintStream;
038 import java.net.URL;
039 import java.util.Set;
040
041 import junit.extensions.TestSetup;
042 import junit.framework.Test;
043 import junit.framework.TestSuite;
044
045 import org.apache.commons.io.output.NullOutputStream;
046 import org.apache.log4j.Logger;
047
048 /**
049 * Tests for the Corpus classes
050 */
051 public class TestJape extends BaseJapeTests {
052 private static final Logger logger = Logger.getLogger(TestJape.class);
053
054 public TestJape(String name) {
055 super(name);
056 }
057
058 /** Batch run */
059 public void testSimple() throws Exception {
060 AnnotationCreator ac = new BaseAnnotationCreator() {
061
062 public AnnotationSet createAnnots(Document doc) throws InvalidOffsetException {
063 // defaultAS.add(new Long( 0), new Long( 2), "A",feat);
064 add(2, 4, "A");
065 // defaultAS.add(new Long( 4), new Long( 6), "A",feat);
066 // defaultAS.add(new Long( 6), new Long( 8), "A",feat);
067 add(4, 6, "B");
068 // defaultAS.add(new Long(10), new Long(12), "B",feat);
069 // defaultAS.add(new Long(12), new Long(14), "B",feat);
070 // defaultAS.add(new Long(14), new Long(16), "B",feat);
071 // defaultAS.add(new Long(16), new Long(18), "B",feat);
072 add(6, 8, "C");
073 add(8, 10, "C");
074 // defaultAS.add(new Long(22), new Long(24), "C",feat);
075 // defaultAS.add(new Long(24), new Long(26), "C",feat);
076 return doc.getAnnotations();
077 }
078 };
079 Set<Annotation> res = doTest("texts/doc0.html", "/jape/TestABC.jape", ac);
080 Out.println(res);
081 assertEquals(res.toString(), 3, res.size());
082 compareStartOffsets(res, 2, 2, 2);
083 compareEndOffsets(res, 6, 8, 10);
084 } // testBatch()
085
086
087 /**
088 * This test loads a saved application which runs several JAPE grammars
089 * using different application modes on a specially prepared document.
090 * The resulting annotations are checked against gold-standard versions
091 * saved in the test document.
092 * @throws IOException
093 * @throws ResourceInstantiationException
094 * @throws PersistenceException
095 * @throws ExecutionException
096 */
097 public void testApplicationModes() throws PersistenceException, ResourceInstantiationException,
098 IOException, ExecutionException {
099 //load the application
100 URL applicationURL = Files.getGateResource("gate.ac.uk/tests/jape/jape-test.xgapp");
101 CorpusController application = (CorpusController) PersistenceManager
102 .loadObjectFromUrl(applicationURL);
103 //load the test file
104 Document testDoc = Factory.newDocument(Files.getGateResource("gate.ac.uk/tests/jape/test-doc.xml"),
105 "UTF-8");
106 Corpus testCorpus = Factory.newCorpus("JAPE Test Corpus");
107 testCorpus.add(testDoc);
108 //run the application
109 application.setCorpus(testCorpus);
110 application.execute();
111 //check the results
112 AnnotationDiffer annDiff = new AnnotationDiffer();
113 annDiff.setSignificantFeaturesSet(null);
114 for (String testName : new String[] { "appelt", "brill", "all", "once" }) {
115 AnnotationSet keySet = testDoc.getAnnotations(testName);
116 AnnotationSet responseSet = testDoc.getAnnotations(testName + "-test");
117 annDiff.calculateDiff(keySet, responseSet);
118 double fMeasure = annDiff.getFMeasureStrict(1);
119 assertEquals("Incorrect F-measure for test " + testName, (double) 1, fMeasure);
120 }
121 //cleanup
122 application.setCorpus(null);
123 Factory.deleteResource(application);
124 testCorpus.remove(0);
125 Factory.deleteResource(testDoc);
126 Factory.deleteResource(testCorpus);
127 }
128
129 /**
130 * This test sets up a JAPE transducer based on a grammar
131 * (RhsError.jape) that will throw a null pointer exception.
132 * The test succeeds so long as we get that exception.
133 */
134 public void disabled_testRhsErrorMessages() {
135 try {
136
137 // a document with an annotation
138 Document doc = Factory.newDocument("This is a Small Document.");
139 FeatureMap features = Factory.newFeatureMap();
140 features.put("orth", "upperInitial");
141 doc.getAnnotations().add(new Long(0), new Long(8), "Token", features);
142
143 doTest(doc, "tests/RhsError.jape", null, null);
144 fail("Bad JAPE grammar didn't throw an exception");
145 } catch (Exception e) {
146 if (logger.isDebugEnabled())
147 logger.info("Exception in Jape rule: " + e);
148 }
149
150 } // testRhsErrorMessages
151
152 public void testBrill() throws IOException, GateException, Exception {
153 String japeFile = "/gate.ac.uk/tests/jape/control_mode_tests/brill_test.jape";
154 String[] expectedResults = { "Find_A", "Find_A", "Find_A_B", "Find_A_B", "Find_A_B_C" };
155
156 AnnotationCreator annotCreator = new BaseAnnotationCreator() {
157 public AnnotationSet createAnnots(Document doc) throws InvalidOffsetException {
158 add(2, 4, "A");
159 add(2, 5, "A");
160 add(3, 5, "A");
161 add(4, 6, "B");
162 add(5, 7, "B");
163 add(6, 8, "C");
164 add(8, 10, "D");
165
166 return as;
167 }
168 };
169
170 Set<Annotation> actualResults = doTest(DEFAULT_DATA_FILE, japeFile, annotCreator);
171 Out.println(actualResults);
172 compareResults(expectedResults, actualResults);
173
174 } // testBrill()
175
176 public void testAppeltMode() throws IOException, GateException, Exception {
177 String japeFile = "/gate.ac.uk/tests/jape/control_mode_tests/appelt_test.jape";
178 String[] expectedResults = { "Find_A_B_C" };
179
180 AnnotationCreator annotCreator = new BaseAnnotationCreator() {
181 public AnnotationSet createAnnots(Document doc) throws InvalidOffsetException {
182 add(2, 4, "A");
183 add(4, 6, "B");
184 add(2, 3, "C");
185 add(3, 8, "D");
186 add(2, 3, "A");
187 add(3, 4, "B");
188 add(4, 9, "C");
189 return as;
190 }
191 };
192
193 Set<Annotation> actualResults = doTest(DEFAULT_DATA_FILE, japeFile, annotCreator);
194 Out.println(actualResults);
195 compareResults(expectedResults, actualResults);
196
197 } // testAppelt()
198
199 public void testAllMode() throws IOException, GateException, Exception {
200 String japeFile = "/gate.ac.uk/tests/jape/control_mode_tests/all_mode_test.jape";
201 String[] expectedResults = { "Find_A", "Find_A", "Find_A_B", "Find_A_B", "Find_A_B_C", "Find_A",
202 "Find_A_B", "Find_B_C" };
203
204 AnnotationCreator annotCreator = new BaseAnnotationCreator() {
205 public AnnotationSet createAnnots(Document doc) throws InvalidOffsetException {
206 add(2, 4, "A");
207 add(2, 5, "A");
208 add(3, 5, "A");
209 add(4, 6, "B");
210 add(5, 7, "B");
211 add(6, 8, "C");
212 add(8, 10, "D");
213 return as;
214 }
215 };
216
217 Set<Annotation> actualResults = doTest(DEFAULT_DATA_FILE, japeFile, annotCreator);
218 Out.println(actualResults);
219 compareResults(expectedResults, actualResults);
220
221 } // testAppelt()
222
223 /**
224 * This test sets up a JAPE transducer based on a grammar
225 * (RhsError2.jape) that will throw a compiler error.
226 * The test succeeds so long as we get that exception.
227 */
228 public void disabled_testRhsErrorMessages2() {
229
230 PrintStream sysout = System.out;
231 System.setOut(new PrintStream(new NullOutputStream()));
232
233 // run a JAPE batch on the faulty grammar
234 try {
235 Document doc = Factory.newDocument("This is a Small Document.");
236 FeatureMap features = Factory.newFeatureMap();
237 features.put("orth", "upperInitial");
238 doc.getAnnotations().add(new Long(0), new Long(8), "Token", features);
239 doTest(doc, "tests/RhsError2.jape", null, null);
240 fail("Bad JAPE grammar (2) didn't throw an exception");
241 } catch (Exception e) {
242 // success
243 } finally {
244 System.setOut(sysout);
245 }
246
247 } // testRhsErrorMessages2
248
249 public static Test suite() {
250 Test suite = new TestSetup(new TestSuite(TestJape.class)) {
251 protected void setUp() {
252 setUpGate();
253 logger.info("GATE initialized and fixure set up.");
254 }
255 };
256 return suite;
257 }
258
259 public static void main(String... args) {
260 junit.textui.TestRunner.run(TestJape.suite());
261 }
262 } // class TestJape
|