Copy/pastable code samples for module 5
1. Basic GATE APIs
Slide 8/9 - Loading a doc (take 2)
package gatetutorial;
import gate.*;
import gate.gui.*;
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(new Runnable() {
public void run() {
MainFrame.getInstance().setVisible(true);
}
});
//create a document
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;
public class Main {
public static void main(String[] args)
throws Exception{
// prepare the library
Gate.init();
// show the main window
MainFrame.getInstance().setVisible(true);
// init-time parameter for document
FeatureMap params = Factory.newFeatureMap();
params.put(Document.DOCUMENT_URL_PARAMETER_NAME,
new URL("http://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));
}
}
Slide 48 - Exercise 6
//get the root plugins dir
File pluginsDir = Gate.getPluginsHome();
//Let's load the ANNIE plugin
File aPluginDir = new File(pluginsDir, "ANNIE");
//load the plugin.
Gate.getCreoleRegister().registerDirectories(
aPluginDir.toURI().toURL());
// create tokenizer
LanguageAnalyser pr = (LanguageAnalyser)
Factory.createResource(
"gate.creole.tokeniser.DefaultTokeniser");
// create serialAnalyzerController
SerialAnalyserController controller =
(SerialAnalyserController) Factory.createResource(
"gate.creole.SerialAnalyserController");
// add pr to the corpus
controller.add(pr);
// create a corpus
Corpus corpus = Factory.newCorpus("corpus");
corpus.add(doc); // add document to the corpus
controller.setCorpus(corpus); // set corpus
controller.execute(); // execute the controller




