1
15
16
17 package gate.util;
18
19 import java.awt.Color;
20 import java.io.*;
21 import java.net.URL;
22 import java.util.*;
23 import java.util.prefs.Preferences;
24 import java.util.zip.GZIPInputStream;
25 import java.util.zip.GZIPOutputStream;
26
27 import javax.swing.UIManager;
28
29 import gate.*;
30 import gate.creole.ANNIEConstants;
31 import gate.creole.Transducer;
32 import gate.creole.gazetteer.DefaultGazetteer;
33 import gate.creole.ir.*;
34 import gate.creole.tokeniser.DefaultTokeniser;
35 import gate.gui.MainFrame;
36 import gate.gui.docview.AnnotationSetsView;
37 import gate.persist.SerialDataStore;
38
39
41 public class Scratch
42 {
43
44 private static final boolean DEBUG = false;
45
46 public static void main(String args[]) throws Exception {
47 File file = new File("Z:/gate/bin");
48 System.out.println("Canonical path: " + file.getCanonicalPath());
49 System.out.println("URL: " + file.toURL());
50
51 URL url = new URL("jar:file:/Z:/gate/bin/gate.jar!/gate/Gate.class");
52 System.out.println(url);
53 System.out.println("Path: " + url.getPath());
54 System.out.println("File: " + url.getFile());
55 System.out.println("Host: " + url.getHost());
56 System.out.println("Proto: " + url.getProtocol());
57
58 url = Thread.currentThread().getContextClassLoader().
59 getResource("gate/Gate.class");
60 System.out.println(url);
61 System.out.println("Path: " + url.getPath());
62 System.out.println("File: " + url.getFile());
63 System.out.println("Host: " + url.getHost());
64 System.out.println("Proto: " + url.getProtocol());
65
66 Map defaultsMap = UIManager.getLookAndFeelDefaults();
67 System.out.println(defaultsMap.keySet());
68
69
70
72 Gate.init();
73 Document doc = Factory.newDocument("ala bala portocala");
74 AnnotationSet set = doc.getAnnotations();
75 Integer annId =
76 set.add(new Long(3), new Long(5), "FooBar", Factory.newFeatureMap());
77 Annotation ann = set.get(annId);
78 set.remove(ann);
80
81 AnnotationSet resSet = set.get(new Long(0), new Long(10));
82
83 System.out.println(resSet);
85
86 System.out.println("==============================================");
87
88
89 Map listsMap = new HashMap();
90 listsMap.put("blah", new ArrayList());
91 List theList = (List)listsMap.get("blah");
92 System.out.println(theList);
93 theList.add("object");
94 theList = (List)listsMap.get("blah");
95 System.out.println(theList);
96
97
98
99 File home = new File("z:/gate/plugins");
100 File tok = new File(home, "ANNIE/resources/tokeniser/Default.rul");
101 System.out.println(tok);
102
103 Preferences prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
104 System.out.println(prefRoot.keys().length);
105 prefRoot.removeNode();
106 prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
107 System.out.println(prefRoot.keys().length);
108 Color col = new Color(100, 101, 102, 103);
109 int rgb = col.getRGB();
110 int alpha = col.getAlpha();
111 int rgba = rgb | (alpha << 24);
112 Color col1 = new Color(rgba, true);
113 System.out.println(col + " a: " + col.getAlpha());
114 System.out.println(col1+ " a: " + col1.getAlpha());
115 System.out.println(col.equals(col1));
116
119
120
125
130
194
199
206
207
211
214
216 }
218
219 public static void exitTimeHook() {
220 Runtime.getRuntime().addShutdownHook(new Thread() {
221 public void run() {
222 System.out.println("shutting down");
223 System.out.flush();
224
225 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
227
228 try {
230 ObjectOutputStream oos = new ObjectOutputStream(
231 new GZIPOutputStream(new FileOutputStream(stateFile))
232 );
233 System.out.println("writing main frame");
234 System.out.flush();
235 oos.writeObject(Main.getMainFrame());
236 oos.close();
237 } catch(Exception e) {
238 System.out.println("Couldn't write to state file: " + e);
239 }
240
241 System.out.println("done");
242 System.out.flush();
243 }
244 });
245 }
247
253 public static void dumpGuiState() {
254 System.out.println("dumping gui state...");
255 System.out.flush();
256
257 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
259
260 try {
262 ObjectOutputStream oos = new ObjectOutputStream(
263 new GZIPOutputStream(new FileOutputStream(stateFile))
264 );
265 MainFrame mf = Main.getMainFrame();
266
267 long startTime = System.currentTimeMillis();
269 long timeNow = System.currentTimeMillis();
270 while(timeNow - startTime < 3000){
271 try {
272 Thread.sleep(150);
273 timeNow = System.currentTimeMillis();
274 } catch(InterruptedException ie) {}
275 }
276
277 System.out.println("writing main frame");
278 System.out.flush();
279 oos.writeObject(mf);
280 oos.close();
281 } catch(Exception e) {
282 System.out.println("Couldn't write to state file: " + e);
283 }
284
285 System.out.println("...done gui dump");
286 System.out.flush();
287 }
289
294 public void runNerc() throws Exception {
295 long startTime = System.currentTimeMillis();
296
297 Out.prln("gate init");
298 Gate.setLocalWebServer(false);
299 Gate.setNetConnected(false);
300 Gate.init();
301
302 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
303 Out.prln("creating resources");
304
305 Controller c1 = (Controller) Factory.createResource(
307 "gate.creole.SerialController",
308 Factory.newFeatureMap()
309 );
310 c1.setName("Scratch controller");
311
312 FeatureMap params = Factory.newFeatureMap();
314 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
315 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
316 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
317 params);
318
319 params = Factory.newFeatureMap();
321 params.put(DefaultTokeniser.DEF_TOK_TOKRULES_URL_PARAMETER_NAME,
322 "gate:/creole/tokeniser/DefaultTokeniser.rules");
323 params.put(DefaultTokeniser.DEF_TOK_ENCODING_PARAMETER_NAME, "UTF-8");
324 params.put(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
325 ProcessingResource tokeniser = (ProcessingResource) Factory.createResource(
326 "gate.creole.tokeniser.DefaultTokeniser", params
327 );
328
329 params = Factory.newFeatureMap();
331 params.put(DefaultGazetteer.DEF_GAZ_DOCUMENT_PARAMETER_NAME, doc);
332 params.put(DefaultGazetteer.DEF_GAZ_LISTS_URL_PARAMETER_NAME,
333 "gate:/creole/gazeteer/default/lists.def");
334 ProcessingResource gaz = (ProcessingResource) Factory.createResource(
335 "gate.creole.gazetteer.DefaultGazetteer", params
336 );
337
338 params = Factory.newFeatureMap();
340 params.put(Transducer.TRANSD_DOCUMENT_PARAMETER_NAME, doc);
341 ProcessingResource trans = (ProcessingResource) Factory.createResource(
343 "gate.creole.Transducer", params
344 );
345
346 c1.getPRs().add(tokeniser);
348 c1.getPRs().add(gaz);
349 c1.getPRs().add(trans);
350
351 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
352 Out.prln("dumping state");
353
354 File stateFile = new File("z:\\tmp", "SerialisedGateState.gzsr");
356
357 try {
359 ObjectOutputStream oos = new ObjectOutputStream(
360 new GZIPOutputStream(new FileOutputStream(stateFile))
361 );
362 oos.writeObject(new SessionState());
363 oos.close();
364 } catch(IOException e) {
365 throw new GateException("Couldn't write to state file: " + e);
366 }
367
368 Out.prln(System.getProperty("user.home"));
369
370 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
371 Out.prln("reinstating");
372
373 try {
374 FileInputStream fis = new FileInputStream(stateFile);
375 GZIPInputStream zis = new GZIPInputStream(fis);
376 ObjectInputStream ois = new ObjectInputStream(zis);
377 SessionState state = (SessionState) ois.readObject();
378 ois.close();
379 } catch(IOException e) {
380 throw
381 new GateException("Couldn't read file "+stateFile+": "+e);
382 } catch(ClassNotFoundException ee) {
383 throw
384 new GateException("Couldn't find class: "+ee);
385 }
386
387 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
388 Out.prln("done");
389 }
391
392
393 class SessionState implements Serializable {
394 SessionState() {
395 cr = Gate.getCreoleRegister();
396 dsr = Gate.getDataStoreRegister();
397 }
398
399 CreoleRegister cr;
400
401 DataStoreRegister dsr;
402
403 }
406
407 protected static int random() {
408 return randomiser.nextInt(9999);
409 }
411
415 public static void createIndex() throws Exception{
416 String dsURLString = "file:///d:/temp/ds";
417 String indexLocation = "d:/temp/ds.idx";
418
419 Gate.init();
420
421 SerialDataStore sds = (SerialDataStore)Factory.openDataStore(
423 "gate.persist.SerialDataStore", dsURLString);
424 sds.open();
425 List corporaIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
426 IndexedCorpus corpus = (IndexedCorpus)
427 sds.getLr("gate.corpora.SerialCorpusImpl",
428
429 corporaIds.get(0));
430 DefaultIndexDefinition did = new DefaultIndexDefinition();
431 did.setIrEngineClassName(gate.creole.ir.lucene.
432 LuceneIREngine.class.getName());
433
434 did.setIndexLocation(indexLocation);
435 did.addIndexField(new IndexField("body", new ContentPropertyReader(), false));
436
437 corpus.setIndexDefinition(did);
438
439 Out.prln("removing old index");
440 corpus.getIndexManager().deleteIndex();
441 Out.prln("building new index");
442 corpus.getIndexManager().createIndex();
443 Out.prln("optimising new index");
444 corpus.getIndexManager().optimizeIndex();
445 Out.prln("saving corpus");
446 sds.sync(corpus);
447 Out.prln("done!");
448 }
449
450
454 public static void tokeniseFile(File file) throws Exception{
455 Gate.init();
457 Document doc = Factory.newDocument(file.toURL());
459 DefaultTokeniser tokeniser = (DefaultTokeniser)Factory.createResource(
461 "gate.creole.tokeniser.DefaultTokeniser");
462
463 tokeniser.setParameterValue(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
465 tokeniser.execute();
466
467 Set annotationTypes = new HashSet();
470 annotationTypes.add(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
471 annotationTypes.add(ANNIEConstants.SPACE_TOKEN_ANNOTATION_TYPE);
472
473 List tokenList = new ArrayList(doc.getAnnotations().get(annotationTypes));
474 Collections.sort(tokenList, new OffsetComparator());
475
476 Iterator tokIter = tokenList.iterator();
478 while(tokIter.hasNext()){
479 Annotation anAnnotation = (Annotation)tokIter.next();
480 System.out.println("Annotation: (" +
481 anAnnotation.getStartNode().getOffset().toString() +
482 ", " + anAnnotation.getEndNode().getOffset().toString() +
483 "[type: " + anAnnotation.getType() +
484 ", features: " + anAnnotation.getFeatures().toString()+
485 "]" );
486 }
487 }
488
489
490 public static class ContentPropertyReader implements PropertyReader{
491 public String getPropertyValue(gate.Document doc){
492 return doc.getContent().toString();
493 }
494 }
495
496
497 protected static Random randomiser = new Random();
498
499 }
501
502