Scratch.java
001 /*
002  *  Scratch.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, 22/03/00
013  *
014  *  $Id: Scratch.java 12801 2010-07-02 10:12:32Z murfffi $
015  */
016 
017 
018 package gate.util;
019 
020 import java.awt.Color;
021 import java.awt.event.WindowAdapter;
022 import java.awt.event.WindowEvent;
023 import java.io.*;
024 import java.net.*;
025 import java.util.*;
026 import java.util.prefs.Preferences;
027 import java.util.zip.GZIPInputStream;
028 import java.util.zip.GZIPOutputStream;
029 
030 import javax.swing.*;
031 
032 import gate.*;
033 import gate.creole.*;
034 import gate.creole.gazetteer.DefaultGazetteer;
035 import gate.creole.ir.*;
036 import gate.creole.tokeniser.DefaultTokeniser;
037 import gate.gui.MainFrame;
038 import gate.gui.OkCancelDialog;
039 import gate.gui.docview.AnnotationSetsView;
040 import gate.persist.SerialDataStore;
041 import gate.util.persistence.PersistenceManager;
042 
043 /** A scratch pad for experimenting.
044   */
045 public class Scratch
046 {
047   /** Debug flag */
048   private static final boolean DEBUG = false;
049 
050   
051   public static void docFromString(){
052     try{
053       Gate.init();
054       SerialAnalyserController annie = (SerialAnalyserController)
055         PersistenceManager.loadObjectFromFile(new File("d:/tmp/annie.gapp"));
056       
057       Corpus corpus = Factory.newCorpus("A Corpus");
058       Document doc = Factory.newDocument("US President George W Bush has said he is seeking a $600m (£323m) boost in aid to nations hit by the Asian tsunami.");
059       corpus.add(doc);
060       annie.setCorpus(corpus);
061       annie.execute();
062       
063       //get the annotations
064       Iterator annIter = doc.getAnnotations().iterator();
065       while(annIter.hasNext()){
066         System.out.println(annIter.next());
067       }
068       
069     }catch(Exception e){
070       e.printStackTrace();
071     }
072   }
073   
074     
075   public static void main(String args[]) throws Exception {
076     String input = "âÄôâÄúâÄù";
077     for(int i = 0; i< input.length(); i++){
078       char c = input.charAt(i);
079       System.out.println("Character: '" + c + "', type: " + Character.getType(input.charAt(i)));
080     }
081     
082     if(truereturn;
083     System.out.println(System.getenv());
084     
085     System.out.println("Text fg: " + UIManager.getColor("Tree.textForeground"));
086     System.out.println("Text bg: " + UIManager.getColor("Tree.textBackground"));
087     System.out.println("Tree fg: " + UIManager.getColor("Tree.foreground"));
088     System.out.println("Tree bg: " + UIManager.getColor("Tree.background"));
089     System.out.println("Tree DC fg: " + UIManager.getColor("Tree.dropCellForeground"));
090     System.out.println("Tree DC bg: " + UIManager.getColor("Tree.dropCellBackground"));
091     System.out.println("Tree Sel bg: " + UIManager.getColor("Tree.selectionBackground"));
092     
093     Map defaultsMap = UIManager.getLookAndFeelDefaults();
094     System.out.println(defaultsMap.keySet());
095     System.out.println(defaultsMap);
096     if(truereturn;
097     boolean value = OkCancelDialog.showDialog(null, new JLabel("OK?")"Answer please!");
098     System.out.println(value ? "Yes!" "No!");
099     
100     if(truereturn;
101     final JFrame aFrame = new JFrame("Scratch");
102     aFrame.addWindowListener(new WindowAdapter(){
103       @Override
104       public void windowClosing(WindowEvent e) {
105         aFrame.dispose();
106       }
107       
108     });
109     aFrame.setSize(800600);
110     aFrame.setVisible(true);
111     Gate.init();
112     MainFrame mf = MainFrame.getInstance();
113     mf.setSize(800600);
114     mf.setVisible(true);
115     
116     if(truereturn;
117     
118     File file = new File("Z:/gate/bin");
119     System.out.println("Canonical path: " + file.getCanonicalPath());
120     System.out.println("URL: " + file.toURI().toURL());
121     
122     URL url = new URL("jar:file:/Z:/gate/bin/gate.jar!/gate/Gate.class");
123     System.out.println(url);
124     System.out.println("Path: " + url.getPath());
125     System.out.println("File: " + url.getFile());
126     System.out.println("Host: " + url.getHost());
127     System.out.println("Proto: " + url.getProtocol());
128     
129     url = Thread.currentThread().getContextClassLoader().
130       getResource("gate/Gate.class");
131     System.out.println(url);
132     System.out.println("Path: " + url.getPath());
133     System.out.println("File: " + url.getFile());
134     System.out.println("Host: " + url.getHost());
135     System.out.println("Proto: " + url.getProtocol());
136     
137 //    Map defaultsMap = UIManager.getLookAndFeelDefaults();
138 //    System.out.println(defaultsMap.keySet());
139 
140     
141     //test for a bug reported by Luc Plamondon
142     
143     Gate.init();
144     Document doc = Factory.newDocument("ala bala portocala");
145     AnnotationSet set = doc.getAnnotations();
146     Integer annId = 
147       set.add(new Long(3)new Long(5)"FooBar", Factory.newFeatureMap());
148     Annotation ann = set.get(annId);
149     //remove the annotation 
150     set.remove(ann);
151     
152     AnnotationSet resSet = set.get(new Long(0)new Long(10));
153     
154     //this set is empty so the bug was fixed.
155     System.out.println(resSet);
156     
157     System.out.println("==============================================");
158     
159     
160     Map listsMap = new HashMap();
161     listsMap.put("blah"new ArrayList());
162     List theList = (List)listsMap.get("blah");
163     System.out.println(theList);
164     theList.add("object");
165     theList = (List)listsMap.get("blah");
166     System.out.println(theList);
167     
168     
169     
170     File home = new File("z:/gate/plugins");
171     File tok = new File(home, "ANNIE/resources/tokeniser/Default.rul");
172     System.out.println(tok);
173     
174     Preferences prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
175     System.out.println(prefRoot.keys().length);
176     prefRoot.removeNode();
177     prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
178     System.out.println(prefRoot.keys().length);
179     Color col = new Color(100101102103);
180     int rgb = col.getRGB();
181     int alpha = col.getAlpha();
182     int rgba = rgb | (alpha << 24);
183     Color col1 = new Color(rgba, true);
184     System.out.println(col + " a: " + col.getAlpha());
185     System.out.println(col1+ " a: " + col1.getAlpha());
186     System.out.println(col.equals(col1));
187 //    Map defaultsMap = UIManager.getLookAndFeelDefaults();
188 //    System.out.println(defaultsMap.keySet());
189     
190     
191 //    double a = 16.99;
192 //    double b = 9.99;
193 //    double c = a - b;
194 //    System.out.println(c);
195 
196 //    Runtime.getRuntime().exec(new String[]{"cmd",
197 //                                           "C:\\Program Files\\GATE 2.2\\bin\\gate.bat"},
198 //                              null,
199 //                              new File("C:\\Program Files\\GATE 2.2\\bin"));
200 
201 //    Gate.init();
202 //    Document doc = Factory.newDocument("The quick brown fox jumped over the lazy dog");
203 //    AnnotationSet annSet1 = doc.getAnnotations("Set1");
204 //    annSet1.add(new Long(1), new Long(5), "Foo", Factory.newFeatureMap());
205 //
206 //    AnnotationSet annSet2 = doc.getAnnotations("Set2");
207 //    annSet2.add(new Long(1), new Long(5), "Bar", Factory.newFeatureMap());
208 //    annSet2.addAll(annSet1);
209 //
210 //    List annotations = new ArrayList(annSet2);
211 //    Collections.sort(annotations, new OffsetComparator());
212 //    Iterator annIter = annotations.iterator();
213 //    while(annIter.hasNext()){
214 //      Annotation ann =(Annotation)annIter.next();
215 //      System.out.print("Start node: ID = " + ann.getStartNode().getId());
216 //      System.out.println(" Offset = " + ann.getStartNode().getOffset());
217 //      System.out.print("End node: ID = " + ann.getEndNode().getId());
218 //      System.out.println(" Offset = " + ann.getEndNode().getOffset());
219 //
220 //    }
221 //    File tempFile = File.createTempFile("gaga", "");
222 //    tempFile.delete();
223 //    tempFile.mkdir();
224 //    tempFile.deleteOnExit();
225 //    File tempFile2 = File.createTempFile("fil", ".tmp", tempFile);
226 //    tempFile2.deleteOnExit();
227 //System.out.println(tempFile.getCanonicalPath());
228 //    Thread.sleep(100000);
229 //
230 //    Map charsets = java.nio.charset.Charset.availableCharsets();
231 //    Iterator namesIter = charsets.keySet().iterator();
232 //    while(namesIter.hasNext()){
233 //      String name = (String)namesIter.next();
234 //      System.out.println(name + " : " + charsets.get(name));
235 //    }
236 //    System.out.println(System.getProperty("file.encoding"));
237 //    System.out.println(java.nio.charset.Charset.forName(System.getProperty("file.encoding")).name());
238 //    System.out.println(new Character((char)0xa3));
239 //    Gate.init();
240 //
241 //    List classes = Tools.findSubclasses(gate.creole.ir.Search.class);
242 //    if(classes != null) for(int i = 0; i < classes.size(); i++){
243 //      Out.prln(classes.get(i).toString());
244 //    }
245 //    createIndex();
246 //    URL anURL = new URL("file:/z:/a/b/c/d.txt");
247 //    URL anotherURL = new URL("file:/z:/a/b/c/d.txt");
248 //    String relPath = gate.util.persistence.PersistenceManager.
249 //                     getRelativePath(anURL, anotherURL);
250 //    Out.prln("Context: " + anURL);
251 //    Out.prln("Target: " + anotherURL);
252 //    Out.prln("Relative path: " + relPath);
253 //    Out.prln("Result " + new URL(anURL, relPath));
254 //    javax.swing.text.FlowView fv;
255 //    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
256 //    Map uidefaults  = (Map)javax.swing.UIManager.getDefaults();
257 //    List keys = new ArrayList(uidefaults.keySet());
258 //    Collections.sort(keys);
259 //    Iterator keyIter = keys.iterator();
260 //    while(keyIter.hasNext()){
261 //      Object key = keyIter.next();
262 //      System.out.println(key + " : " + uidefaults.get(key));
263 //    }
264 
265     // initialise the thing
266 //    Gate.setNetConnected(false);
267 //    Gate.setLocalWebServer(false);
268 //    Gate.init();
269 
270 //    Scratch oneOfMe = new Scratch();
271 //    try{
272 //      oneOfMe.runNerc();
273 //    } catch (Exception e) {
274 //      e.printStackTrace(Out.getPrintWriter());
275 //    }
276 
277 
278 //    CreoleRegister reg = Gate.getCreoleRegister();
279 //System.out.println("Instances for " + reg.getLrInstances("gate.creole.AnnotationSchema"));
280 //System.out.println("Instances for " + reg.getAllInstances ("gate.creole.AnnotationSchema"));
281 
282 //System.out.println("VRs for " + reg.getAnnotationVRs("Tree"));
283 //System.out.println("VRs for " + reg.getAnnotationVRs());
284 
285 //System.out.println(reg.getLargeVRsForResource("gate.corpora.DocumentImpl"));
286   // main
287 
288   /** Example of using an exit-time hook. */
289   public static void exitTimeHook() {
290     Runtime.getRuntime().addShutdownHook(new Thread() {
291       public void run() {
292         System.out.println("shutting down");
293         System.out.flush();
294 
295         // create a File to store the state in
296         File stateFile = new File("z:\\tmp""GateGuiState.gzsr");
297 
298         // dump the state into the new File
299         try {
300           ObjectOutputStream oos = new ObjectOutputStream(
301             new GZIPOutputStream(new FileOutputStream(stateFile))
302           );
303           System.out.println("writing main frame");
304           System.out.flush();
305           oos.writeObject(Main.getMainFrame());
306           oos.close();
307         catch(Exception e) {
308           System.out.println("Couldn't write to state file: " + e);
309         }
310 
311         System.out.println("done");
312         System.out.flush();
313       }
314     });
315   // exitTimeHook()
316 
317   /**
318    * ***** <B>Failed</B> *****
319    * attempt to serialise whole gui state - various swing components
320    * don't like to be serialised :-(. might be worth trying again when
321    * jdk1.4 arrives.
322    */
323   public static void dumpGuiState() {
324     System.out.println("dumping gui state...");
325     System.out.flush();
326 
327     // create a File to store the state in
328     File stateFile = new File("z:\\tmp""GateGuiState.gzsr");
329 
330     // dump the state into the new File
331     try {
332       ObjectOutputStream oos = new ObjectOutputStream(
333         new GZIPOutputStream(new FileOutputStream(stateFile))
334       );
335       MainFrame mf = Main.getMainFrame();
336 
337       // wait for 1 sec
338       long startTime = System.currentTimeMillis();
339       long timeNow = System.currentTimeMillis();
340       while(timeNow - startTime < 3000){
341         try {
342           Thread.sleep(150);
343           timeNow = System.currentTimeMillis();
344         catch(InterruptedException ie) {}
345       }
346 
347       System.out.println("writing main frame");
348       System.out.flush();
349       oos.writeObject(mf);
350       oos.close();
351     catch(Exception e) {
352       System.out.println("Couldn't write to state file: " + e);
353     }
354 
355     System.out.println("...done gui dump");
356     System.out.flush();
357   // dumpGuiState
358 
359   /**
360    * Run NERC and print out the various stages (doesn't actually
361    * use Nerc but the individual bits), and serialise then deserialise
362    * the NERC system.
363    */
364   public void runNerc() throws Exception {
365     long startTime = System.currentTimeMillis();
366 
367     Out.prln("gate init");
368     Gate.setLocalWebServer(false);
369     Gate.setNetConnected(false);
370     Gate.init();
371 
372     Out.prln((System.currentTimeMillis() - startTime1000.0 " seconds");
373     Out.prln("creating resources");
374 
375     // a controller
376     Controller c1 = (ControllerFactory.createResource(
377       "gate.creole.SerialController",
378       Factory.newFeatureMap()
379     );
380     c1.setName("Scratch controller");
381 
382     //get a document
383     FeatureMap params = Factory.newFeatureMap();
384     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
385     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
386     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
387                                                     params);
388 
389     //create a default tokeniser
390     params = Factory.newFeatureMap();
391     params.put(DefaultTokeniser.DEF_TOK_TOKRULES_URL_PARAMETER_NAME,
392       "gate:/creole/tokeniser/DefaultTokeniser.rules");
393     params.put(DefaultTokeniser.DEF_TOK_ENCODING_PARAMETER_NAME, "UTF-8");
394     params.put(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
395     ProcessingResource tokeniser = (ProcessingResourceFactory.createResource(
396       "gate.creole.tokeniser.DefaultTokeniser", params
397     );
398 
399     //create a default gazetteer
400     params = Factory.newFeatureMap();
401     params.put(DefaultGazetteer.DEF_GAZ_DOCUMENT_PARAMETER_NAME, doc);
402     params.put(DefaultGazetteer.DEF_GAZ_LISTS_URL_PARAMETER_NAME,
403       "gate:/creole/gazeteer/default/lists.def");
404     ProcessingResource gaz = (ProcessingResourceFactory.createResource(
405       "gate.creole.gazetteer.DefaultGazetteer", params
406     );
407 
408     //create a default transducer
409     params = Factory.newFeatureMap();
410     params.put(Transducer.TRANSD_DOCUMENT_PARAMETER_NAME, doc);
411     //params.put("grammarURL", new File("z:\\tmp\\main.jape").toURI().toURL());
412     ProcessingResource trans = (ProcessingResourceFactory.createResource(
413       "gate.creole.Transducer", params
414     );
415 
416     // get the controller to encapsulate the tok and gaz
417     c1.getPRs().add(tokeniser);
418     c1.getPRs().add(gaz);
419     c1.getPRs().add(trans);
420 
421     Out.prln((System.currentTimeMillis() - startTime1000.0 " seconds");
422     Out.prln("dumping state");
423 
424     // create a File to store the state in
425     File stateFile = new File("z:\\tmp""SerialisedGateState.gzsr");
426 
427     // dump the state into the new File
428     try {
429       ObjectOutputStream oos = new ObjectOutputStream(
430         new GZIPOutputStream(new FileOutputStream(stateFile))
431       );
432       oos.writeObject(new SessionState());
433       oos.close();
434     catch(IOException e) {
435       throw new GateException("Couldn't write to state file: " + e);
436     }
437 
438     Out.prln(System.getProperty("user.home"));
439 
440     Out.prln((System.currentTimeMillis() - startTime1000.0 " seconds");
441     Out.prln("reinstating");
442 
443     try {
444       FileInputStream fis = new FileInputStream(stateFile);
445       GZIPInputStream zis = new GZIPInputStream(fis);
446       ObjectInputStream ois = new ObjectInputStream(zis);
447       SessionState state = (SessionStateois.readObject();
448       ois.close();
449     catch(IOException e) {
450       throw
451         new GateException("Couldn't read file "+stateFile+": "+e);
452     catch(ClassNotFoundException ee) {
453       throw
454         new GateException("Couldn't find class: "+ee);
455     }
456 
457     Out.prln((System.currentTimeMillis() - startTime1000.0 " seconds");
458     Out.prln("done");
459   // runNerc()
460 
461   
462  
463   /** Inner class for holding CR and DSR for serialisation experiments */
464   class SessionState implements Serializable {
465     SessionState() {
466       cr = Gate.getCreoleRegister();
467       dsr = Gate.getDataStoreRegister();
468     }
469 
470     CreoleRegister cr;
471 
472     DataStoreRegister dsr;
473 
474     // other state from Gate? and elsewhere?
475   // SessionState
476 
477   /** Generate a random integer for file naming. */
478   protected static int random() {
479     return randomiser.nextInt(9999);
480   // random
481 
482   /**
483    * Generates an index for a corpus in a datastore on Valy's computer in order
484    * to have some test data.
485    */
486   public static void createIndex() throws Exception{
487     String dsURLString = "file:///d:/temp/ds";
488     String indexLocation = "d:/temp/ds.idx";
489 
490     Gate.init();
491 
492     //open the datastore
493     SerialDataStore sds = (SerialDataStore)Factory.openDataStore(
494                             "gate.persist.SerialDataStore", dsURLString);
495     sds.open();
496     List corporaIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
497     IndexedCorpus corpus = (IndexedCorpus)
498                            sds.getLr("gate.corpora.SerialCorpusImpl",
499 
500                                      corporaIds.get(0));
501     DefaultIndexDefinition did = new DefaultIndexDefinition();
502     did.setIrEngineClassName(gate.creole.ir.lucene.
503                              LuceneIREngine.class.getName());
504 
505     did.setIndexLocation(indexLocation);
506     did.addIndexField(new IndexField("body"new ContentPropertyReader()false));
507 
508     corpus.setIndexDefinition(did);
509 
510     Out.prln("removing old index");
511     corpus.getIndexManager().deleteIndex();
512     Out.prln("building new index");
513     corpus.getIndexManager().createIndex();
514     Out.prln("optimising new index");
515     corpus.getIndexManager().optimizeIndex();
516     Out.prln("saving corpus");
517     sds.sync(corpus);
518     Out.prln("done!");
519   }
520 
521   /**
522    *
523    @param file a TXT file containing the text
524    */
525   public static void tokeniseFile(File filethrows Exception{
526     //initialise GATE (only call it once!!)
527     Gate.init();
528     //create the document
529     Document doc = Factory.newDocument(file.toURI().toURL());
530     //create the tokeniser
531     DefaultTokeniser tokeniser = (DefaultTokeniser)Factory.createResource(
532       "gate.creole.tokeniser.DefaultTokeniser");
533 
534     //tokenise the document
535     tokeniser.setParameterValue(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
536     tokeniser.execute();
537 
538     //extract data from document
539     //we need tokens and spaces
540     Set annotationTypes = new HashSet();
541     annotationTypes.add(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
542     annotationTypes.add(ANNIEConstants.SPACE_TOKEN_ANNOTATION_TYPE);
543 
544     List<Annotation> tokenList = new ArrayList<Annotation>(doc.getAnnotations().get(annotationTypes));
545     Collections.sort(tokenList, new OffsetComparator());
546 
547     //iterate through the tokens
548     Iterator<Annotation> tokIter = tokenList.iterator();
549     while(tokIter.hasNext()){
550       Annotation anAnnotation = tokIter.next();
551       System.out.println("Annotation: (" +
552                         anAnnotation.getStartNode().getOffset().toString() +
553                         ", " + anAnnotation.getEndNode().getOffset().toString() +
554                         "[type: " + anAnnotation.getType() +
555                          ", features: " + anAnnotation.getFeatures().toString()+
556                          "]" );
557     }
558   }
559 
560 
561   public static class ContentPropertyReader implements PropertyReader{
562     public String getPropertyValue(gate.Document doc){
563       return doc.getContent().toString();
564     }
565   }
566 
567   /** Random number generator */
568   protected static Random randomiser = new Random();
569 
570 // class Scratch
571