Log in Help
Homewikijape-repository 〉 ontologies.html

JAPE and Ontologies

See first the user guide section named Ontology-Aware JAPE Transducer.


1. Annotating Text with Ontological Information

Rule: Location  
// create an annotation set consisting of all the annotations for each tag  
gate.AnnotationSet mentionSet = (gate.AnnotationSet)bindings.get(‘mention’);  
// create the ontology and class features  
   FeatureMap features = Factory.newFeatureMap();  
   features.put(‘ontology’, ontology.getURL());  
   features.put(‘class’, ‘Location’);  
// create the new annotation  
 annotations.add(mentionSet.firstNode(), mentionSet.lastNode(), ’Mention’,  

2. Populating Ontologies

Rule: FindEntities
  //find the annotation matched by LHS
  //we know the annotation set returned
  //will always contain a single annotation
  Annotation mentionAnn = mentionAnnots.iterator().next();
  //find the class of the mention
  String className = (String)mentionAnn.getFeatures().
  // should normalize class name and avoid invalid class names here!
  OClass aClass = ontology.getOClass(ontology.createOURIForName(className));
  if(aClass == null) { 
    System.err.println("Error class \"" + className + "\" does not exist!");
  //find the text covered by the annotation
  String theMentionText = gate.Utils.stringFor(doc, mentionAnn);
  // when creating a URI from text that came from a document you must take care
  // to ensure that the name does not contain any characters that are illegal
  // in a URI.  The following method does this nicely for English but you may
  // want to do your own normalization instead if you have non-English text.
  String mentionName = OUtils.toResourceName(theMentionText);

  // get the property to store mention texts for mention instances
  DatatypeProperty prop =

  OURI mentionURI = ontology.createOURIForName(mentionName);
  // if that mention instance does not already exist, add it
  if (!ontology.containsOInstance(mentionURI)) {
    OInstance inst = ontology.addOInstance(mentionURI, aClass);
    // add the actual mention text to the instance
    try {
        new Literal(theMentionText, OConstants.ENGLISH));
    catch(InvalidValueException e) {
      throw new JapeException(e);