in ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/ae/JointRelationTokenBasedAnnotator.java [91:242]
public void process(JCas jCas) throws AnalysisEngineProcessException {
if(timexMode == OutputMode.IndexTags && !this.isTraining()){
final String timexIdxMapFile = "target/eval/thyme/train_and_test/event-event/timex_idx.txt";
try {
timex_idx = TimexIdxReader(FileLocator.getAsStream(timexIdxMapFile));
} catch (IOException e) {
e.printStackTrace();
}
}
// Map<EventMention, Collection<EventMention>> coveringMap =
// JCasUtil.indexCovering(jCas, EventMention.class, EventMention.class);
// get all gold relation lookup
Map<List<Annotation>, BinaryTextRelation> relationLookup;
relationLookup = new HashMap<>();
if (this.isTraining()) {
relationLookup = new HashMap<>();
for (BinaryTextRelation relation : JCasUtil.select(jCas, BinaryTextRelation.class)) {
Annotation arg1 = relation.getArg1().getArgument();
Annotation arg2 = relation.getArg2().getArgument();
// The key is a list of args so we can do bi-directional lookup
List<Annotation> key = Arrays.asList(arg1, arg2);
if(relationLookup.containsKey(key)){
String reln = relationLookup.get(key).getCategory();
System.err.println("Error in: "+ ViewUriUtil.getURI(jCas).toString());
System.err.println("Error! This attempted relation " + relation.getCategory() +
" already has a relation " + reln + " at this span: " +
arg1.getCoveredText() + " -- " + arg2.getCoveredText());
} else {
relationLookup.put(key, relation);
}
}
}
for(Sentence sentence : JCasUtil.select(jCas, Sentence.class)) {
// collect all relevant relation arguments from the sentence
List<IdentifiedAnnotationPair> candidatePairs = getCandidateRelationArgumentPairs(jCas, sentence);
// walk through the pairs of annotations
for (IdentifiedAnnotationPair pair : candidatePairs) {
IdentifiedAnnotation arg1 = pair.getArg1();
IdentifiedAnnotation arg2 = pair.getArg2();
String context;
String cuis;
if(arg2.getBegin() < arg1.getBegin()) {
// ... event2 ... event1 ... scenario
if(timexMode == OutputMode.TokenSeq){
String arg2tag = "e2";
if(arg2 instanceof TimeMention){
arg2tag="t";
}
context = ArgContextProvider.getTokenContext(jCas, sentence, arg2, arg2tag, arg1, "e1", 2);
}else{
context = getTokenTimexContext(jCas, sentence, arg2, "e2", arg1, "e1", 2);
}
} else {
// ... event1 ... event2 ... scenario
if(timexMode == OutputMode.TokenSeq){
String arg2tag ="e2";
if(arg2 instanceof TimeMention){
arg2tag="t";
}
context = ArgContextProvider.getTokenContext(jCas, sentence, arg1, "e1", arg2, arg2tag, 2);
}else{
context = getTokenTimexContext(jCas, sentence, arg1, "e1", arg2, "e2", 2);
}
}
//get CUIs for two arguments
// Set<String> CUIs = getCuiDtrel(jCas, arg1);
// CUIs.addAll(getCuiDtrel(jCas, arg2));
//
// cuis = String.join(" ", CUIs);
//derive features based on context:
List<Feature> feats = new ArrayList<>();
String[] tokens = context.split(" ");
// String[] tokens = (context + "|" + cuis).split(" ");
for (String token: tokens){
feats.add(new Feature(token.toLowerCase()));
}
// during training, feed the features to the data writer
if(this.isTraining()) {
String category = getRelationCategory(relationLookup, arg1, arg2);
// drop some portion of negative examples during training
// if(category == null && coin.nextDouble() <= 0.5) {
// continue; // skip this negative example
// }
if(category == null) {
category = NO_RELATION_CATEGORY;
} else{
category = category.toLowerCase();
}
this.dataWriter.write(new Instance<>(category, feats));
} else {
String predictedCategory = this.classifier.classify(feats);
// add a relation annotation if a true relation was predicted
if (predictedCategory != null && !predictedCategory.equals(NO_RELATION_CATEGORY)) {
// if we predict an inverted relation, reverse the order of the
// arguments
//if for event-time relations:
if(arg1 instanceof TimeMention || arg2 instanceof TimeMention){
if(predictedCategory.endsWith("-1")) {
predictedCategory = predictedCategory.substring(0, predictedCategory.length() - 2);
if(arg1 instanceof TimeMention){
IdentifiedAnnotation temp = arg1;
arg1 = arg2;
arg2 = temp;
}
} else {
if(arg1 instanceof EventMention){
IdentifiedAnnotation temp = arg1;
arg1 = arg2;
arg2 = temp;
}
}
// createRelation(jCas, arg1, arg2, predictedCategory.toUpperCase(), 0.0);
}else{//if for event-event relations:
if (predictedCategory.endsWith("-1")) {
predictedCategory = predictedCategory.substring(0, predictedCategory.length() - 2);
IdentifiedAnnotation temp = arg1;
arg1 = arg2;
arg2 = temp;
}
// createRelation(jCas, arg1, arg2, predictedCategory.toUpperCase(), 0.0);
}
createRelation(jCas, arg1, arg2, predictedCategory.toUpperCase(), 0.0);
}
}
}
}
if(timexMode== OutputMode.IndexTags && !this.isTraining()){//in test time update the hashmap file for each cas
try {
TimexIdxWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}