Log in Help
Print
HomewikiTrainingCourseJune2012track-2 〉 module-7-code.html
 

Code snippets for module 7

1. Basic Document stats PR

1.1. Code

package module7;

import gate.Resource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;

public class DocStats extends AbstractLanguageAnalyser {

  @Override
  public void execute() throws ExecutionException {
    int tokens = document.getAnnotations().get("Token").size();
    document.getFeatures().put("token_count", tokens);
  }

  @Override
  public Resource init() throws ResourceInstantiationException {
    System.out.println(getClass().getName() + " is initialising.");
    return this;
  }
}

1.2. creole.xml

<CREOLE-DIRECTORY>
  <JAR>module7.jar</JAR>
  <RESOURCE>
    <NAME>Document Statistics</NAME>
    <CLASS>module7.DocStats</CLASS>
  </RESOURCE>
</CREOLE-DIRECTORY>

2. Adding a parameter

2.1. Code

package module7;

import gate.Resource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;

public class DocStats extends AbstractLanguageAnalyser {

  private String annotTypeToCount;

  public void setAnnotTypeToCount(String t) { annotTypeToCount = t; }
  public String getAnnotTypeToCount() { return annotTypeToCount; }

  private String inputASName;

  public void setInputASName(String n) { inputASName = n; }
  public String getInputASName() { return inputASName; }
  

  @Override
  public void execute() throws ExecutionException {
    int tokens = document.getAnnotations(inputASName).get(
           annotTypeToCount).size();
    document.getFeatures().put(annotTypeToCount + "_count", tokens);
  }

  @Override
  public Resource init() throws ResourceInstantiationException {
    System.out.println(getClass().getName() + " is initialising.");
    return this;
  }
}

2.2. creole.xml

<CREOLE-DIRECTORY>
  <JAR>module7.jar</JAR>
  <RESOURCE>
    <NAME>Document Statistics</NAME>
    <CLASS>module7.DocStats</CLASS>
    <PARAMETER NAME="annotTypeToCount" RUNTIME="true"
        DEFAULT="Token">java.lang.String</PARAMETER>
    <PARAMETER NAME="inputASName" RUNTIME="true"
        OPTIONAL="true">java.lang.String</PARAMETER>
  </RESOURCE>
</CREOLE-DIRECTORY>

3. Confiuration using annotations

3.1. Code

package module7;

import gate.Resource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;
import gate.creole.metadata.*;

@CreoleResource(name = "Document Statistics",
        comment = "Counts annotations in a document")
public class DocStats extends AbstractLanguageAnalyser {

  private String annotTypeToCount;

  @RunTime
  @CreoleParameter(defaultValue = "Token")
  public void setAnnotTypeToCount(String t) { annotTypeToCount = t; }
  public String getAnnotTypeToCount() { return annotTypeToCount; }

  private String inputASName;

  @Optional
  @RunTime
  @CreoleParameter(comment = "The annotation set to use for input")
  public void setInputASName(String n) { inputASName = n; }
  public String getInputASName() { return inputASName; }
  

  @Override
  public void execute() throws ExecutionException {
    int tokens = document.getAnnotations(inputASName).get(
           annotTypeToCount).size();
    document.getFeatures().put(annotTypeToCount + "_count", tokens);
  }

  @Override
  public Resource init() throws ResourceInstantiationException {
    System.out.println(getClass().getName() + " is initialising.");
    return this;
  }
}

3.2. creole.xml

<CREOLE-DIRECTORY>
  <JAR SCAN="true">module7.jar</JAR>
</CREOLE-DIRECTORY>

There are examples of doing more complex statistics and exploring the CreoleRegister in the material from a previous training course.