this.annotatorNames = annotatorNames;
}
public Collection<KnowtatorAnnotation> parse(URI knowtatorXML) throws JDOMException, IOException {
Element annotationsElem = new SAXBuilder().build(knowtatorXML.toURL()).getRootElement();
// parse <annotation> elements
Set<String> ignoredAnnotators = new HashSet<String>();
Map<String, KnowtatorAnnotation> annotations = new HashMap<String, KnowtatorAnnotation>();
for (Element annotationElem : annotationsElem.getChildren("annotation")) {
for (Element annotatorElem : this.getChild(annotationElem, "annotator")) {
String annotatorName = annotatorElem.getText();
if (!this.annotatorNames.contains(annotatorName)) {
ignoredAnnotators.add(annotatorName);
} else {
for (Element mentionElem : this.getChild(annotationElem, "mention")) {
for (String id : this.getAttributeValue(mentionElem, "id")) {
KnowtatorAnnotation annotation = new KnowtatorAnnotation();
annotation.id = id;
annotations.put(id, annotation);
List<Element> spanElems = annotationElem.getChildren("span");
if (!spanElems.isEmpty()) {
for (Element spannedTextElem : this.getChild(annotationElem, "spannedText")) {
annotation.spannedText = spannedTextElem.getText();
}
for (Element spanElem : spanElems) {
for (String startStr : this.getAttributeValue(spanElem, "start")) {
for (String endStr : this.getAttributeValue(spanElem, "end")) {
annotation.addSpan(Integer.parseInt(startStr), Integer.parseInt(endStr));
}
}
}
}
}
}
}
}
}
LOGGER.fine(String.format("Ignored annotators %s in %s", ignoredAnnotators, knowtatorXML));
// parse <stringSlotMention> elements
Map<String, Slot<String>> stringSlots = new HashMap<String, Slot<String>>();
for (Element slotMentionElem : annotationsElem.getChildren("stringSlotMention")) {
for (IdAndSlot<String> idAndSlot : this.parseSlotMention(
slotMentionElem,
"stringSlotMentionValue")) {
stringSlots.put(idAndSlot.id, idAndSlot.slot);
}
}
// parse <booleanSlotMention> elements
Map<String, Slot<Boolean>> booleanSlots = new HashMap<String, Slot<Boolean>>();
for (Element slotMentionElem : annotationsElem.getChildren("booleanSlotMention")) {
for (IdAndSlot<String> idAndSlot : this.parseSlotMention(
slotMentionElem,
"booleanSlotMentionValue")) {
Slot<String> slot = idAndSlot.slot;
Boolean value = Boolean.parseBoolean(slot.value);
booleanSlots.put(idAndSlot.id, new Slot<Boolean>(slot.name, value));
}
}
// parse <complexSlotMention> elements
Map<String, Slot<KnowtatorAnnotation>> mentionSlots = new HashMap<String, Slot<KnowtatorAnnotation>>();
for (Element slotMentionElem : annotationsElem.getChildren("complexSlotMention")) {
for (IdAndSlot<String> idAndSlot : this.parseSlotMention(
slotMentionElem,
"complexSlotMentionValue")) {
Slot<String> slot = idAndSlot.slot;
KnowtatorAnnotation mention = annotations.get(slot.value);
if (mention != null) {
mentionSlots.put(idAndSlot.id, new Slot<KnowtatorAnnotation>(slot.name, mention));
}
}
}
// parse <classMention> elements
for (Element classMentionElem : annotationsElem.getChildren("classMention")) {
for (String id : this.getAttributeValue(classMentionElem, "id")) {
KnowtatorAnnotation annotation = annotations.get(id);
if (annotation == null) {
continue;
}