1
15
16
17 package gate.util;
18
19 import java.awt.Color;
20 import java.io.*;
21 import java.util.*;
22 import java.util.prefs.Preferences;
23 import java.util.zip.GZIPInputStream;
24 import java.util.zip.GZIPOutputStream;
25
26 import javax.swing.UIManager;
27
28 import gate.*;
29 import gate.creole.ANNIEConstants;
30 import gate.creole.Transducer;
31 import gate.creole.gazetteer.DefaultGazetteer;
32 import gate.creole.ir.*;
33 import gate.creole.tokeniser.DefaultTokeniser;
34 import gate.gui.MainFrame;
35 import gate.gui.docview.AnnotationSetsView;
36 import gate.persist.SerialDataStore;
37
38
40 public class Scratch
41 {
42
43 private static final boolean DEBUG = false;
44
45 public static void main(String args[]) throws Exception {
46 Map listsMap = new HashMap();
47 listsMap.put("blah", new ArrayList());
48 List theList = (List)listsMap.get("blah");
49 System.out.println(theList);
50 theList.add("object");
51 theList = (List)listsMap.get("blah");
52 System.out.println(theList);
53
54
55
56 File home = new File("z:/gate/plugins");
57 File tok = new File(home, "ANNIE/resources/tokeniser/Default.rul");
58 System.out.println(tok);
59
60 Preferences prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
61 System.out.println(prefRoot.keys().length);
62 prefRoot.removeNode();
63 prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
64 System.out.println(prefRoot.keys().length);
65 Color col = new Color(100, 101, 102, 103);
66 int rgb = col.getRGB();
67 int alpha = col.getAlpha();
68 int rgba = rgb | (alpha << 24);
69 Color col1 = new Color(rgba, true);
70 System.out.println(col + " a: " + col.getAlpha());
71 System.out.println(col1+ " a: " + col1.getAlpha());
72 System.out.println(col.equals(col1));
73
76
77
82
87
151
156
163
164
168
171
173 }
175
176 public static void exitTimeHook() {
177 Runtime.getRuntime().addShutdownHook(new Thread() {
178 public void run() {
179 System.out.println("shutting down");
180 System.out.flush();
181
182 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
184
185 try {
187 ObjectOutputStream oos = new ObjectOutputStream(
188 new GZIPOutputStream(new FileOutputStream(stateFile))
189 );
190 System.out.println("writing main frame");
191 System.out.flush();
192 oos.writeObject(Main.getMainFrame());
193 oos.close();
194 } catch(Exception e) {
195 System.out.println("Couldn't write to state file: " + e);
196 }
197
198 System.out.println("done");
199 System.out.flush();
200 }
201 });
202 }
204
210 public static void dumpGuiState() {
211 System.out.println("dumping gui state...");
212 System.out.flush();
213
214 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
216
217 try {
219 ObjectOutputStream oos = new ObjectOutputStream(
220 new GZIPOutputStream(new FileOutputStream(stateFile))
221 );
222 MainFrame mf = Main.getMainFrame();
223
224 long startTime = System.currentTimeMillis();
226 long timeNow = System.currentTimeMillis();
227 while(timeNow - startTime < 3000){
228 try {
229 Thread.sleep(150);
230 timeNow = System.currentTimeMillis();
231 } catch(InterruptedException ie) {}
232 }
233
234 System.out.println("writing main frame");
235 System.out.flush();
236 oos.writeObject(mf);
237 oos.close();
238 } catch(Exception e) {
239 System.out.println("Couldn't write to state file: " + e);
240 }
241
242 System.out.println("...done gui dump");
243 System.out.flush();
244 }
246
251 public void runNerc() throws Exception {
252 long startTime = System.currentTimeMillis();
253
254 Out.prln("gate init");
255 Gate.setLocalWebServer(false);
256 Gate.setNetConnected(false);
257 Gate.init();
258
259 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
260 Out.prln("creating resources");
261
262 Controller c1 = (Controller) Factory.createResource(
264 "gate.creole.SerialController",
265 Factory.newFeatureMap()
266 );
267 c1.setName("Scratch controller");
268
269 FeatureMap params = Factory.newFeatureMap();
271 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
272 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
273 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
274 params);
275
276 params = Factory.newFeatureMap();
278 params.put(DefaultTokeniser.DEF_TOK_TOKRULES_URL_PARAMETER_NAME,
279 "gate:/creole/tokeniser/DefaultTokeniser.rules");
280 params.put(DefaultTokeniser.DEF_TOK_ENCODING_PARAMETER_NAME, "UTF-8");
281 params.put(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
282 ProcessingResource tokeniser = (ProcessingResource) Factory.createResource(
283 "gate.creole.tokeniser.DefaultTokeniser", params
284 );
285
286 params = Factory.newFeatureMap();
288 params.put(DefaultGazetteer.DEF_GAZ_DOCUMENT_PARAMETER_NAME, doc);
289 params.put(DefaultGazetteer.DEF_GAZ_LISTS_URL_PARAMETER_NAME,
290 "gate:/creole/gazeteer/default/lists.def");
291 ProcessingResource gaz = (ProcessingResource) Factory.createResource(
292 "gate.creole.gazetteer.DefaultGazetteer", params
293 );
294
295 params = Factory.newFeatureMap();
297 params.put(Transducer.TRANSD_DOCUMENT_PARAMETER_NAME, doc);
298 ProcessingResource trans = (ProcessingResource) Factory.createResource(
300 "gate.creole.Transducer", params
301 );
302
303 c1.getPRs().add(tokeniser);
305 c1.getPRs().add(gaz);
306 c1.getPRs().add(trans);
307
308 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
309 Out.prln("dumping state");
310
311 File stateFile = new File("z:\\tmp", "SerialisedGateState.gzsr");
313
314 try {
316 ObjectOutputStream oos = new ObjectOutputStream(
317 new GZIPOutputStream(new FileOutputStream(stateFile))
318 );
319 oos.writeObject(new SessionState());
320 oos.close();
321 } catch(IOException e) {
322 throw new GateException("Couldn't write to state file: " + e);
323 }
324
325 Out.prln(System.getProperty("user.home"));
326
327 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
328 Out.prln("reinstating");
329
330 try {
331 FileInputStream fis = new FileInputStream(stateFile);
332 GZIPInputStream zis = new GZIPInputStream(fis);
333 ObjectInputStream ois = new ObjectInputStream(zis);
334 SessionState state = (SessionState) ois.readObject();
335 ois.close();
336 } catch(IOException e) {
337 throw
338 new GateException("Couldn't read file "+stateFile+": "+e);
339 } catch(ClassNotFoundException ee) {
340 throw
341 new GateException("Couldn't find class: "+ee);
342 }
343
344 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
345 Out.prln("done");
346 }
348
349
350 class SessionState implements Serializable {
351 SessionState() {
352 cr = Gate.getCreoleRegister();
353 dsr = Gate.getDataStoreRegister();
354 }
355
356 CreoleRegister cr;
357
358 DataStoreRegister dsr;
359
360 }
363
364 protected static int random() {
365 return randomiser.nextInt(9999);
366 }
368
372 public static void createIndex() throws Exception{
373 String dsURLString = "file:///d:/temp/ds";
374 String indexLocation = "d:/temp/ds.idx";
375
376 Gate.init();
377
378 SerialDataStore sds = (SerialDataStore)Factory.openDataStore(
380 "gate.persist.SerialDataStore", dsURLString);
381 sds.open();
382 List corporaIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
383 IndexedCorpus corpus = (IndexedCorpus)
384 sds.getLr("gate.corpora.SerialCorpusImpl",
385
386 corporaIds.get(0));
387 DefaultIndexDefinition did = new DefaultIndexDefinition();
388 did.setIrEngineClassName(gate.creole.ir.lucene.
389 LuceneIREngine.class.getName());
390
391 did.setIndexLocation(indexLocation);
392 did.addIndexField(new IndexField("body", new ContentPropertyReader(), false));
393
394 corpus.setIndexDefinition(did);
395
396 Out.prln("removing old index");
397 corpus.getIndexManager().deleteIndex();
398 Out.prln("building new index");
399 corpus.getIndexManager().createIndex();
400 Out.prln("optimising new index");
401 corpus.getIndexManager().optimizeIndex();
402 Out.prln("saving corpus");
403 sds.sync(corpus);
404 Out.prln("done!");
405 }
406
407
411 public static void tokeniseFile(File file) throws Exception{
412 Gate.init();
414 Document doc = Factory.newDocument(file.toURL());
416 DefaultTokeniser tokeniser = (DefaultTokeniser)Factory.createResource(
418 "gate.creole.tokeniser.DefaultTokeniser");
419
420 tokeniser.setParameterValue(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
422 tokeniser.execute();
423
424 Set annotationTypes = new HashSet();
427 annotationTypes.add(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
428 annotationTypes.add(ANNIEConstants.SPACE_TOKEN_ANNOTATION_TYPE);
429
430 List tokenList = new ArrayList(doc.getAnnotations().get(annotationTypes));
431 Collections.sort(tokenList, new OffsetComparator());
432
433 Iterator tokIter = tokenList.iterator();
435 while(tokIter.hasNext()){
436 Annotation anAnnotation = (Annotation)tokIter.next();
437 System.out.println("Annotation: (" +
438 anAnnotation.getStartNode().getOffset().toString() +
439 ", " + anAnnotation.getEndNode().getOffset().toString() +
440 "[type: " + anAnnotation.getType() +
441 ", features: " + anAnnotation.getFeatures().toString()+
442 "]" );
443 }
444 }
445
446
447 public static class ContentPropertyReader implements PropertyReader{
448 public String getPropertyValue(gate.Document doc){
449 return doc.getContent().toString();
450 }
451 }
452
453
454 protected static Random randomiser = new Random();
455
456 }
458
459