Log in Help
Print
HomewikiTrainingCourseJune2016 〉 mod5-samples.html
 

Code samples for module 5

Code samples from module 5, in a form that can be copied and pasted more easily than from the PDFs.

1. Introduction to GATE Embedded

Exercise 2 (slide 8/9)

package module5;

import gate.*;
import gate.gui.*;
import javax.swing.SwingUtilities;

public class Main {
  public static void main(String... args) throws Exception {
    Gate.init();
    SwingUtilities.invokeAndWait(() -> MainFrame.getInstance().setVisible(true));
    Factory.newDocument("This is a document");
  }
}

Slide 22 - creating a doc (take 3)

FeatureMap params = Factory.newFeatureMap();
params.put(
    Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, 
    "This is a document!");
FeatureMap feats = Factory.newFeatureMap();
feats.put("createdBy", "me!");
Factory.createResource("gate.corpora.DocumentImpl", 
    params, feats, "My first document");

Slide 25 - Exercise 2

import gate.*;
import java.net.URL;
import java.util.Date;
import javax.swing.SwingUtilities;

public class Main {
  public static void main(String[] args) 
      throws Exception{

    // prepare the library
    Gate.init();
    // show the main window
    SwingUtilities.invokeAndWait(() -> MainFrame.getInstance().setVisible(true));

    // init-time parameter for document
    FeatureMap params = Factory.newFeatureMap();
    params.put(Document.DOCUMENT_URL_PARAMETER_NAME, 
      new URL("https://www.gate.ac.uk"));
    params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, 
      "UTF-8");

    // document features
    FeatureMap feats = Factory.newFeatureMap();
    feats.put("date", new Date());
    Factory.createResource("gate.corpora.DocumentImpl", 
      params, feats, "This is home");
  }
}

Slide 34 - Exercise 3

Follows on from Exercise 2 - save the return value of createResource

   Document doc = (Document)Factory.createResource(....);

   // obtain a map of all named annotation sets
   Map<String, AnnotationSet> namedASes = 
     doc.getNamedAnnotationSets();
   System.out.println("No. of named Annotation Sets:" 
     + namedASes.size());

   // no of annotations each set contains
   for (String setName : namedASes.keySet()) {
     // annotation set
     AnnotationSet aSet = namedASes.get(setName);
     // no of annotations
     System.out.println("No. of Annotations for " + 
       setName + ":" + aSet.size());

     // all annotation types
     Set<String> annotTypes = aSet.getAllTypes();
     for(String aType : annotTypes) {
       System.out.println("  " + aType + ": " 
         + aSet.get(aType).size());
     }
   }

Slide 37 - Exercise 4

   // obtain the Original markups annotation set
   AnnotationSet origMarkupsSet = 
     doc.getAnnotations("Original markups");

   // obtain annotations of type 'a'
   AnnotationSet anchorSet = origMarkupsSet.get("a");
   
   // iterate over each annotation
   // obtain its features and print the value of href feature
   for (Annotation anchor : anchorSet) {
     String href = (String) anchor.getFeatures().get("href");
     if(href != null) {
       // resolving href value against the document's url
       System.out.println(new URL(doc.getSourceUrl(), href));
     }
   }