List<IdentifiedAnnotation> cTakesMentions = new ArrayList<IdentifiedAnnotation>();
cTakesMentions.addAll(JCasUtil.select(systemView, EntityMention.class));
cTakesMentions.addAll(JCasUtil.select(systemView, Modifier.class));
CasCopier copier = new CasCopier(systemView.getCas(), goldView.getCas());
for (IdentifiedAnnotation cTakesMention : cTakesMentions) {
Annotation copy = (Annotation) copier.copyFs(cTakesMention);
Feature sofaFeature = copy.getType().getFeatureByBaseName("sofa");
copy.setFeatureValue(sofaFeature, goldView.getSofa());
copy.addToIndexes();
}
// replace gold EntityMentions and Modifiers in relations with cTAKES ones
List<BinaryTextRelation> relations = new ArrayList<BinaryTextRelation>();
relations.addAll(JCasUtil.select(goldView, BinaryTextRelation.class));
for (BinaryTextRelation relation : relations) {
// attempt to replace the gold RelationArguments with system ones
int replacedArgumentCount = 0;
for (RelationArgument relArg : Arrays.asList(relation.getArg1(), relation.getArg2())) {
Annotation goldArg = relArg.getArgument();
Class<? extends Annotation> argClass = goldArg.getClass();
// find all annotations covered by the gold argument and of the same class (these should
// be the ones copied over from the cTAKES output earlier)
List<? extends Annotation> systemArgs = JCasUtil.selectCovered(
goldView,
argClass,
goldArg);
// no ctakes annotation found
if (systemArgs.size() == 0) {
String word = "no";
String className = argClass.getSimpleName();
String argText = goldArg.getCoveredText();
String message = String.format("%s %s for \"%s\"", word, className, argText);
this.getContext().getLogger().log(Level.FINE, message);
continue;
}
// if there's exactly one annotation, replace the gold one with that
if (systemArgs.size() == 1) {
relArg.setArgument(systemArgs.get(0));
replacedArgumentCount += 1;
}
else {
// multiple ctakes arguments found; look for one that matches exactly
// e.g. gold: "right breast", ctakes: "right breast", "breast"
for (Annotation systemArg : systemArgs) {
String goldArgText = goldArg.getCoveredText();
String systemArgText = systemArg.getCoveredText();
if (systemArgText.equals(goldArgText)) {
relArg.setArgument(systemArg);
replacedArgumentCount += 1;
}
}
if(replacedArgumentCount < 1) {
// issue a warning message
String word = "multiple";
String className = argClass.getSimpleName();
String argText = goldArg.getCoveredText();
String message = String.format("%s %s for \"%s\"", word, className, argText);
this.getContext().getLogger().log(Level.FINE, message);
System.out.println("gold argument: " + goldArg.getCoveredText());
System.out.println("gold type: " + ((IdentifiedAnnotation)goldArg).getTypeID());
for(Annotation systemArg : systemArgs) {
System.out.println("ctakes argument: " + systemArg.getCoveredText());
System.out.println("ctakes type: " + ((IdentifiedAnnotation)systemArg).getTypeID());
}