Multimap<List<Annotation>, BinaryTextRelation> annotationsToRelation = HashMultimap.create();
for (BinaryTextRelation relation : JCasUtil.select(jCas, BinaryTextRelation.class)){
String relationType = relation.getCategory();
if(validTemporalType(relationType)){
Annotation arg1 = relation.getArg1().getArgument();
Annotation arg2 = relation.getArg2().getArgument();
if(arg1==null || arg2==null){
System.out.println("Null argument at Doc: "+ fileName);
}else{
annotationsToRelation.put(Arrays.asList(arg1, arg2), relation);
}
}
}
for (List<Annotation> span: Lists.newArrayList(annotationsToRelation.keySet())){
Collection<BinaryTextRelation> relations = annotationsToRelation.get(span);
if(relations.size()>1){//if same span maps to multiple relations
Set<String> types = Sets.newHashSet();
for(BinaryTextRelation relation: relations){
types.add(relation.getCategory());
}
if(types.size()>1){
for(BinaryTextRelation relation: Lists.newArrayList(relations)){
annotationsToRelation.remove(span, relation);
relation.getArg1().removeFromIndexes();
relation.getArg2().removeFromIndexes();
relation.removeFromIndexes();
}
}else if(types.size()==1){
for (int i =1; i< relations.size(); i++){
BinaryTextRelation relation = (BinaryTextRelation) relations.toArray()[i];
annotationsToRelation.remove(span, relation);
relation.getArg1().removeFromIndexes();
relation.getArg2().removeFromIndexes();
relation.removeFromIndexes();
}
}
}
}
ArrayList<BinaryTextRelation> temporalRelation = new ArrayList<>(annotationsToRelation.values());//new ArrayList<BinaryTextRelation>();
// Map<List<Annotation>, BinaryTextRelation> temporalRelationLookup = new HashMap<List<Annotation>, BinaryTextRelation>();
//
// for (BinaryTextRelation relation : JCasUtil.select(jCas, BinaryTextRelation.class)){
// String relationType = relation.getCategory();
// if(validTemporalType(relationType)){
// Annotation arg1 = relation.getArg1().getArgument();
// Annotation arg2 = relation.getArg2().getArgument();
// BinaryTextRelation tempRelation = temporalRelationLookup.get(Arrays.asList(arg1, arg2));
// if( tempRelation == null){
// temporalRelation.add(relation);
// temporalRelationLookup.put(Arrays.asList(arg1, arg2), relation);
// }else{//if there is duplicate
// relation.getArg1().removeFromIndexes();
// relation.getArg2().removeFromIndexes();
// relation.removeFromIndexes();
// }
//
// }
// }
if (!temporalRelation.isEmpty()){
TLinkTypeArray2 relationArray = new TLinkTypeArray2(temporalRelation, new AnnotationIdCollection(temporalRelation));
int addedCount = 0;
for (BinaryTextRelation relation : relationArray.getClosedTlinks(jCas)) {
RelationArgument arg1 = relation.getArg1();
RelationArgument arg2 = relation.getArg2();
String relationType = relation.getCategory();
if(relationType.equals("CONTAINED-BY")||relationType.equals("AFTER")){//ignore these two categories, because their reciprocal already exist.
continue;
}
//check if the inferred relation new:
Collection<BinaryTextRelation> relations = annotationsToRelation.get(Arrays.asList(arg1.getArgument(), arg2.getArgument()));
if(relations.isEmpty()){ //if haven't seen this inferred relation before, then add this relation
arg1.addToIndexes();
arg2.addToIndexes();
relation.addToIndexes();
addedCount++;
}
}