Code snippets for module 6
1. JAPE
1.1. Slide 17
Rule: HelloWorld
(
{Token.string == "Hello"}
{Token.string == "World"}
):hello
-->
{
System.out.println("Hello world");
}
1.2. Exercise 1 (Slide 23)
Rule: ListEntities
({Person}|{Organization}|{Location}):ent
-->
{
AnnotationSet ents = bindings.get("ent");
for(Annotation e : ents) {
System.out.println("Found " + e.getType()
+ " annotation");
System.out.println(" features: "
+ e.getFeatures());
}
}
1.3. Slide 25
-->
:uniTown{
uniTownAnnots.iterator().next().getFeatures()
.put("hasUniversity", Boolean.TRUE);
}
1.4. Slide 27
-->
:uniTown{
String townString = doc.getContent().getContent(
uniTownAnnots.firstNode().getOffset(),
uniTownAnnots.lastNode().getOffset())
.toString();
// don't add an annotation if this town has been seen before. If we
// return, the UniversityTown annotation will not be created.
if(!((Set)doc.getFeatures().get("knownTowns"))
.add(townString)) return;
},
:uniTown.UniversityTown = {}
1.5. Slide 29
Rule: LcString
({Token}):tok
-->
:tok {
for(Annotation a : tokAnnots) {
// get the FeatureMap for the annotation
FeatureMap fm = a.getFeatures();
// get the ``string'' feature
String str = (String)fm.get("string");
// convert it to lower case and store
fm.put("lcString", str.toLowerCase());
}
}
1.6. Exercise 2 (Slide 30)
Rule: GeneralizePOSTag
({Token}):tok
-->
:tok {
for(Annotation t : tokAnnots) {
String pos = (String)t.getFeatures()
.get("category");
if(pos != null) {
int gpLen = pos.length();
if(gpLen > 2) gpLen = 2;
t.getFeatures().put("generalCategory",
pos.substring(0, gpLen));
}
}
}
1.7. Slide 31
Rule: Location
({Lookup.majorType = "location"}):loc
-->
:loc.Location = { kind = :loc.Lookup.minorType,
rule = "Location"},
:loc {
inputAS.removeAll(locAnnots);
}
1.8. Slide 32
Rule: Location
({Lookup.majorType = "location"}):loc
-->
:loc {
try {
String str = doc.getContent().getContent(
locAnnots.firstNode().getOffset(),
locAnnots.lastNode().getOffset())
.toString();
}
catch(InvalidOffsetException e) {
// can't happen, but won't compile without the catch
}
}
1.9. Slide 34
Rule: NPTokens
({NounPhrase}):np
-->
:np {
List<String> posTags = new ArrayList<String>();
for(Annotation tok : inputAS.get("Token")
.getContained(start(npAnnots), end(npAnnots))) {
posTags.add(
(String)tok.getFeatures().get("category"));
}
FeatureMap fm =
npAnnots.iterator().next().getFeatures();
fm.put("posTags", posTags);
fm.put("numTokens", (long)posTags.size());
}
1.10. Exercise 3 (Slide 35)
Imports: { import static gate.Utils.*; }
Phase: NumNouns
Input: Sentence
Options: control = appelt
Rule: CountNouns
({Sentence}):sent
-->
:sent {
AnnotationSet tokens = inputAS.get("Token")
.getContained(start(sentAnnots), end(sentAnnots));
long numNouns = 0;
for(Annotation t : tokens) {
if("NN".equals(t.getFeatures()
.get("generalCategory"))) {
numNouns++;
}
}
sentAnnots.iterator().next().getFeatures()
.put("numNouns", numNouns);
}
1.11. Slide 36
Rule: Section
({SectionHeading}):sect
-->
:sect {
doc.getFeatures().put("currentSection",
stringFor(doc, sectAnnots));
}
Rule: Entity
({Entity}):ent
-->
:ent {
entAnnots.iterator().next().getFeatures()
.put("inSection",
doc.getFeatures().get("currentSection"));
}
2. Ontologies
2.1. Slide 46
Gate.init();
// load the Ontology plugin
Gate.getCreoleRegister().registerDirectories(
new File(Gate.getPluginsHome(), "Ontology")
.toURI().toURL());
Ontology emptyOnto = (Ontology)Factory.createResource(
"gate.creole.ontology.impl.sesame.OWLIMOntology");
2.2. Slide 47
// init GATE and load plugin as before...
URL owl = new File("ontology.owl").toURI().toURL();
FeatureMap params = Factory.newFeatureMap();
params.put("rdfXmlURL", owl);
Ontology theOntology = (Ontology)Factory.createResource(
"gate.creole.ontology.impl.sesame.OWLIMOntology",
params);
2.3. Slide 50
// get all the `top' classes
Set<OClass> tops = ontology.getOClasses(true);
// list them along with their labels
for(OClass c : tops) {
System.out.println(c.getONodeID() +
" (" + c.getLabels() + ")");
}
// find a class by URI
OURI uri = ontology.createOURIForName("Person");
OClass personClass = ontology.getOClass(uri);
2.4. Slide 51
// get direct instances of a class
Set<OInstance> people = ontology.getOInstances(
personClass, OConstants.Closure.DIRECT_CLOSURE);
// get instances of a class or any of its subclasses
Set<OInstance> allPeople = ontology.getOInstances(
personClass, OConstants.Closure.TRANSITIVE_CLOSURE);
2.5. Slide 52
// get a datatype property
OURI namePropURI = ontology.createOURI(
"http://example.org/stuff/1.0/hasName");
DatatypeProperty nameProp = ontology
.getDatatypeProperty(namePropURI);
// find property values for an instance
for(OInstance person : allPeople) {
List<Literal> names =
ontology.getDatatypePropertyValues(nameProp);
for(Literal name : names) {
System.out.println("Person " + person.getONodeID()
+ " hasName " + name.toTurtle());
}
}
2.6. Slide 53
// University of Sheffield instance
OURI uosURI = ontology.createOURIForName(
"UniversityOfSheffield");
OInstance uosInstance = ontology.getOInstance(uosURI);
// worksFor property
OURI worksForURI = ontology.createOURIForName(
"worksFor");
ObjectProperty worksFor = ontology.getObjectProperty(
worksForURI);
// find all the people who work for the University of Sheffield
List<OResource> uniEmployees =
ontology.getOResourcesWith(worksFor, uosInstance);
2.7. Slide 55
OURI personURI = ontology.createOURIForName("Person");
OClass personClass = ontology.getOClass(personURI);
// create a new class as a subclass of an existing class
OURI empURI = ontology.createOURIForName("Employee");
OClass empClass = ontology.addOClass(empURI);
personClass.addSubClass(empClass);
// create an instance
OURI fredURI = ontology.createOURIForName("FredSmith");
OInstance fred = ontology.addOInstance(fredURI,
empClass);
// Fred works for the University of Sheffield
fred.addObjectPropertyValue(worksFor, uosInstance);
2.8. Exercise 1 (Slide 59)
// Create an instance of the City class representing Sheffield
OURI cityURI = ontology.createOURIForName("City");
OClass cityClass = ontology.getOClass(cityURI);
OURI sheffieldURI = ontology.generateOURI("Sheffield");
OInstance sheffield = ontology.addOInstance(sheffieldURI,
cityClass);
// Create a new class named "University" as a subclass of Organization
OURI orgURI = ontology.createOURIForName("Organization");
OURI uniURI = ontology.createOURIForName("University");
OClass orgClass = ontology.getOClass(orgURI);
OClass uniClass = ontology.addOClass(uniURI);
orgClass.addSubClass(uniClass);
// Create an instance of the University class representing the University of Sheffield
// note OUtils.toResourceName to escape spaces
OURI unishefURI = ontology.generateOURI(
OUtils.toResourceName("University of Sheffield"));
OInstance unishef = ontology.addOInstance(unishefURI,
uniClass);
// Create an object property basedAt with domain Organization and range Location
OURI locationURI = ontology.createOURIForName("Location");
OClass locationClass = ontology.getOClass(locationURI);
OURI basedAtURI = ontology.createOURIForName("basedAt");
ObjectProperty basedAt = ontology.addObjectProperty(
basedAtURI, Collections.singleton(orgClass),
Collections.singleton(locationClass));
//TIP: Collections.singleton returns an immutable set containing only the given
object
// Specify that the University of Sheffield is basedAt Sheffield
try {
unishef.addObjectPropertyValue(basedAt, sheffield);
} catch(InvalidValueException e) {
throw new JapeException(e);
}
2.9. Exercise 2 (Slide 66)
// Create some useful objects - rdfs:label property and a Literal for the covered text.
AnnotationProperty rdfsLabel = ontology.getAnnotationProperty(
ontology.createOURI(OConstants.RDFS.LABEL));
Literal text = new Literal(stringFor(doc, locAnnots));
for(Annotation m: locAnnots) {
// determine the right class
OURI classUri = ontology.createOURI(
(String)m.getFeatures().get("class"));
OClass clazz = ontology.getOClass(classUri);
// get all existing instances of that class
Set<OInstance> instances = ontology.getOInstances(clazz,
OConstants.DIRECT_CLOSURE);
// see if any of them have the right label -- if so, we assume they're the same
OInstance inst = null;
for(OInstance candidate : instances) {
if(candidate.getAnnotationPropertyValues(
rdfsLabel).contains(text)) {
// found it!
inst = candidate;
break;
}
}
if(inst == null) {
// not found an existing instance, create one with a generated name
String instName = OUtils.toResourceName(text.getValue());
OURI instURI = ontology.generateOURI(instName + "_");
inst = ontology.addOInstance(instURI, clazz);
// and label it with the covered text
inst.addAnnotationPropertyValue(rdfsLabel, text);
}
// finally, store the URI of the (new or existing) instance on the annotation
m.getFeatures().put("inst",
inst.getONodeID().toString());
}