001 /*
002 * TestJape2.java (Java Annotation Patterns Engine)
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/02/2000
013 *
014 * $Id: TestJape2.java 13726 2011-04-20 16:31:46Z ian_roberts $
015 *
016 * Description: Test class for JAPE.
017 */
018
019 package gate.jape.functest;
020
021 import java.io.File;
022 import java.util.ArrayList;
023 import java.util.Iterator;
024
025 import gate.*;
026 import gate.annotation.AnnotationSetImpl;
027 import gate.creole.ResourceInstantiationException;
028 import gate.util.Err;
029 import gate.util.Out;
030
031 /**
032 * Second test harness for JAPE.
033 * Uses the Sheffield Tokeniser and Gazetteer, and must be run
034 * from the gate directory.
035 * @author Hamish Cunningham
036 */
037 public class TestJape2 {
038
039 /** Debug flag */
040 private static final boolean DEBUG = false;
041
042 /** How much noise to make. */
043 static private boolean verbose = false;
044
045
046 /** Take a list of text files and a collection name, and
047 * call tokeniser/gazetteer/jape on them, creating the
048 * collection.
049 */
050 static public void main(String[] args) {
051
052 // turn debug output on/off
053 //Debug.setDebug(true);
054 //Debug.setDebug(AnnotationSet.class, true);
055 //Debug.setDebug(BasicPatternElement.class, true);
056 //Debug.setDebug(ComplexPatternElement.class, true);
057 //Debug.setDebug(ConstraintGroup.class, true);
058 //Debug.setDebug(SinglePhaseTransducer.class, true);
059
060 // variables to parse the command line options into
061 String collName = null;
062 String japeName = null;
063 ArrayList fileNames = null;
064
065 // process options
066 for(int i=0; i<args.length; i++) {
067 if(args[i].equals("-c") && ++i < args.length) // -c = coll name
068 collName = args[i];
069 else if(args[i].equals("-j") && ++i < args.length) // -j: .jape name
070 japeName = args[i];
071 else if(args[i].equals("-v")) // -v = verbose
072 verbose = true;
073 else { // a list of files
074 fileNames = new ArrayList();
075 do {
076 fileNames.add(args[i++]);
077 } while(i < args.length);
078 }
079 } // for each arg
080
081 // did they give valid options?
082 message("checking options");
083 if(collName == null || japeName == null || fileNames == null)
084 usage("you must supply collection, transducer and file names");
085
086 // create a collection and run the tokeniser
087 message("creating coll, tokenising and gazetteering");
088 Corpus coll = null;
089 try {
090 coll = tokAndGaz(collName, fileNames);
091 } catch(ResourceInstantiationException e) {
092 usage("couldn't open collection: " + e);
093 }
094 /*
095 // run the parser test
096 message("parsing the .jape file (or deserialising the .ser file)");
097 Batch batch = null;
098 try { batch = new Batch(japeName);
099 } catch(JapeException e) {
100 usage("can't create transducer " + e.getMessage());
101 }
102 */
103 /*Transducer transducer = parseJape(japeName);
104 //Out.println(transducer);
105 if(transducer == null)
106 System.exit(1);*/
107
108 // test the transducers from the parser
109 /*
110 message("running the transducer");
111 try { batch.transduce(coll); } catch(JapeException e) {
112 usage("couldn't run transducer " + e.getMessage());
113 }
114 //runTransducer(transducer, coll);
115 //Out.println(transducer);
116
117 message("done\n\r");
118 //System.exit(0);
119 */
120 } // main
121
122
123 /**
124 * Create a collection and put tokenised and gazetteered docs in it.
125 */
126 static public Corpus tokAndGaz(String collName, ArrayList fileNames)
127 throws ResourceInstantiationException {
128
129 // create or overwrite the collection
130 Corpus collection = null;
131 File collDir = new File(collName);
132 collection = Factory.newCorpus(
133 collDir.getAbsolutePath()
134 );
135
136 // add all the documents
137 for(Iterator i = fileNames.iterator(); i.hasNext(); ) {
138 String fname = (String) i.next();
139
140 File f = new File(fname);
141 FeatureMap attrs = Factory.newFeatureMap();
142 Document doc = null;
143
144 try {
145 AnnotationSet annots = new AnnotationSetImpl(doc);
146 collection.add(
147 Factory.newDocument(f.getAbsolutePath())
148 );
149 } catch(ResourceInstantiationException e) {
150 e.printStackTrace();
151 }
152
153 /*
154 // Tokenise the document
155 Tokeniser tokeniser = new Tokeniser(doc, Tokeniser.HMM);
156 try { tokeniser.hmmTokenSequence(); }
157 catch(sheffield.creole.tokeniser.ParseException ex) {
158 ex.printStackTrace();
159 return null;
160 } catch (CreoleException ex) {
161 ex.printStackTrace();
162 return null;
163 }
164
165 // Gazetteer the document
166 gate.creole.Annotator gazetteer = new GazetteerAnnotator();
167 gazetteer.annotate(doc, null);
168 */
169 } // for each doc name
170
171 // return the annotated collection
172 return collection;
173
174 } //tokAndGaz
175
176
177 /**
178 * Must be run from the gate directory.
179 * Parse the .jape file.
180 */
181 /*
182 static public Transducer parseJape(String japeName) {
183 Transducer transducer = null;
184
185 if(japeName.endsWith(".ser")) { // it's compiled already
186 message("deserialising " + japeName);
187 File f = new File(japeName);
188 if(! f.exists())
189 Out.println(japeName + " not found");
190
191 try {
192 FileInputStream fis = new FileInputStream(f.getPath());
193 ObjectInputStream ois = new ObjectInputStream(fis);
194 transducer = (Transducer) ois.readObject();
195 ois.close();
196 } catch (Exception ex) {
197 Err.println(
198 "Can't read from " + f.getName() + ": " + ex.toString()
199 );
200 }
201 } else { // parse it
202 message("parsing " + japeName);
203 try {
204 ParseCpsl cpslParser = new ParseCpsl(japeName);
205 transducer = cpslParser.MultiPhaseTransducer();
206 } catch(IOException e) {
207 e.printStackTrace();
208 } catch(gate.jape.parser.ParseException ee) {
209 Err.println("Error parsing transducer: " + ee.getMessage());
210 }
211 }
212
213 return transducer;
214 } // parseJape
215
216
217 static public void runTransducer(
218 Transducer transducer, Corpus coll
219 ) {
220
221 try {
222 Document doc = coll.firstDocument();
223 do {
224 message("doing document " + doc.getId());
225 transducer.transduce(doc);
226 // Out.println(transducer.toString());
227 } while( (doc = coll.nextDocument()) != null );
228 } catch(JdmException e) {
229 e.printStackTrace();
230 } catch(JapeException e) {
231 e.printStackTrace();
232 }
233 } // runTransducer
234 */
235
236 /** You got something wrong, dumbo. */
237 public static void usage(String errorMessage) {
238 String usageMessage =
239 "usage: java gate.jape.TestJape2.main [-v] " +
240 "-j JapePatternFile -c CollectionName FileName(s)";
241
242 Err.println(errorMessage);
243 Err.println(usageMessage);
244 //System.exit(1);
245
246 } // usage
247
248
249 /** Hello? Anybody there?? */
250 public static void message(String mess) {
251 if(verbose) Out.println("TestJape2: " + mess);
252 } // message
253
254 } // class TestJape2
255
256
257 // $Log$
258 // Revision 1.13 2005/01/11 13:51:36 ian
259 // Updating copyrights to 1998-2005 in preparation for v3.0
260 //
261 // Revision 1.12 2004/07/21 17:10:08 akshay
262 // Changed copyright from 1998-2001 to 1998-2004
263 //
264 // Revision 1.11 2004/03/25 13:01:14 valyt
265 // Imports optimisation throughout the Java sources
266 // (to get rid of annoying warnings in Eclipse)
267 //
268 // Revision 1.10 2001/09/13 12:09:50 kalina
269 // Removed completely the use of jgl.objectspace.Array and such.
270 // Instead all sources now use the new Collections, typically ArrayList.
271 // I ran the tests and I ran some documents and compared with keys.
272 // JAPE seems to work well (that's where it all was). If there are problems
273 // maybe look at those new structures first.
274 //
275 // Revision 1.9 2001/02/08 13:46:06 valyt
276 // Added full Unicode support for the gazetteer and Jape
277 // converted the gazetteer files to UTF-8
278 //
279 // Revision 1.8 2001/01/30 14:18:02 hamish
280 // fixed some hard-coded paths
281 //
282 // Revision 1.7 2000/11/08 16:35:04 hamish
283 // formatting
284 //
285 // Revision 1.6 2000/10/26 10:45:31 oana
286 // Modified in the code style
287 //
288 // Revision 1.5 2000/10/23 21:50:42 hamish
289 // cleaned up exception handling in gate.creole and added
290 // ResourceInstantiationException;
291 //
292 // changed Factory.newDocument(URL u) to use the new instantiation
293 // facilities;
294 //
295 // added COMMENT to resource metadata / ResourceData;
296 //
297 // changed Document and DocumentImpl to follow beans style, and moved
298 // constructor logic to init(); changed all the Factory newDocument methods to
299 // use the new resource creation stuff;
300 //
301 // added builtin document and corpus metadata to creole/creole.xml (copied from
302 // gate.ac.uk/tests/creole.xml);
303 //
304 // changed Corpus to the new style too;
305 //
306 // removed CreoleRegister.init()
307 //
308 // Revision 1.4 2000/10/18 13:26:48 hamish
309 // Factory.createResource now working, with a utility method that uses reflection (via java.beans.Introspector) to set properties on a resource from the
310 // parameter list fed to createResource.
311 // resources may now have both an interface and a class; they are indexed by interface type; the class is used to instantiate them
312 // moved createResource from CR to Factory
313 // removed Transients; use Factory instead
314 //
315 // Revision 1.3 2000/10/16 16:44:34 oana
316 // Changed the comment of DEBUG variable
317 //
318 // Revision 1.2 2000/10/10 15:36:37 oana
319 // Changed System.out in Out and System.err in Err;
320 // Added the DEBUG variable seted on false;
321 // Added in the header the licence;
322 //
323 // Revision 1.1 2000/02/23 13:46:12 hamish
324 // added
325 //
326 // Revision 1.1.1.1 1999/02/03 16:23:03 hamish
327 // added gate2
328 //
329 // Revision 1.9 1998/10/29 12:13:55 hamish
330 // reorganised to use Batch
331 //
332 // Revision 1.8 1998/10/01 16:06:41 hamish
333 // new appelt transduction style, replacing buggy version
334 //
335 // Revision 1.7 1998/09/26 09:19:21 hamish
336 // added cloning of PE macros
337 //
338 // Revision 1.6 1998/09/23 12:48:03 hamish
339 // negation added; noncontiguous BPEs disallowed
340 //
341 // Revision 1.5 1998/09/17 12:53:09 hamish
342 // fixed for new tok; new construction pattern
343 //
344 // Revision 1.4 1998/09/17 10:24:05 hamish
345 // added options support, and Appelt-style rule application
346 //
347 // Revision 1.3 1998/08/19 20:21:46 hamish
348 // new RHS assignment expression stuff added
349 //
350 // Revision 1.2 1998/08/18 14:37:45 hamish
351 // added some messages
352 //
353 // Revision 1.1 1998/08/18 12:43:11 hamish
354 // fixed SPT bug, not advancing newPosition
|