Log in Help
Print
Homewikijape-repository 〉 ontologies.html
 

JAPE and Ontologies

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

Contents

1. Annotating Text with Ontological Information

Rule: Location  
 
({Location}):mention  
 
-->  
{  
// 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’,  
 features);  
}

2. Populating Ontologies

Rule: FindEntities
({Mention}):mention
-->
:mention{
  //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().
    get(gate.creole.ANNIEConstants.LOOKUP_CLASS_FEATURE_NAME);
  // 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!");
    return; 
  } 
    
  //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 =
    ontology.getDatatypeProperty(ontology.createOURIForName("mentionText"));

  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 {
      inst.addDatatypePropertyValue(prop, 
        new Literal(theMentionText, OConstants.ENGLISH));
    }
    catch(InvalidValueException e) {
      throw new JapeException(e);
    }
  }
}