ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tm...@apache.org
Subject svn commit: r1725761 - in /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal: ae/ eval/ utils/
Date Wed, 20 Jan 2016 17:23:10 GMT
Author: tmill
Date: Wed Jan 20 17:23:10 2016
New Revision: 1725761

URL: http://svn.apache.org/viewvc?rev=1725761&view=rev
Log:
Updated doctimerel and relation annotators to output probabilities to a separate view with
parameter.

Added:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/utils/SoftMaxUtil.java
Modified:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/DocTimeRelAnnotator.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/EventTimeSelfRelationAnnotator.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/THYMEAnaforaXMLReader.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/TemporalRelationExtractorAnnotator.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/EvaluationOfBothEEAndETRelations.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/DocTimeRelAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/DocTimeRelAnnotator.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/DocTimeRelAnnotator.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/DocTimeRelAnnotator.java
Wed Jan 20 17:23:10 2016
@@ -23,19 +23,15 @@ import java.util.ArrayList;
 //import java.io.IOException;
 import java.util.List;
 //import java.util.Map;
-
-
-
 import java.util.Map;
 
 import org.apache.ctakes.temporal.ae.feature.ClosestVerbExtractor;
-//import org.apache.ctakes.temporal.ae.feature.CoveredTextToValuesExtractor;
-import org.apache.ctakes.temporal.ae.feature.DateAndMeasurementExtractor;
 import org.apache.ctakes.temporal.ae.feature.EventPropertyExtractor;
 import org.apache.ctakes.temporal.ae.feature.NearbyVerbTenseXExtractor;
 import org.apache.ctakes.temporal.ae.feature.SectionHeaderExtractor;
 import org.apache.ctakes.temporal.ae.feature.TimeXExtractor;
 import org.apache.ctakes.temporal.ae.feature.UmlsSingleFeatureExtractor;
+import org.apache.ctakes.temporal.utils.SoftMaxUtil;
 import org.apache.ctakes.typesystem.type.refsem.Event;
 import org.apache.ctakes.typesystem.type.refsem.EventProperties;
 //import org.apache.ctakes.temporal.ae.feature.duration.DurationExpectationFeatureExtractor;
@@ -45,6 +41,8 @@ import org.apache.ctakes.typesystem.type
 import org.apache.uima.UimaContext;
 import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
 import org.apache.uima.fit.util.JCasUtil;
 import org.apache.uima.jcas.JCas;
@@ -118,6 +116,10 @@ public class DocTimeRelAnnotator extends
 //  private CoveredTextToValuesExtractor disSemExtractor;
 //  private DurationExpectationFeatureExtractor durationExtractor;
   
+  public static final String PARAM_PROB_VIEW = "ProbView";
+  @ConfigurationParameter(name=PARAM_PROB_VIEW, mandatory=false)
+  private String probViewname = null;
+  
   @Override
   public void initialize(UimaContext context) throws ResourceInitializationException {
     super.initialize(context);
@@ -177,13 +179,36 @@ public class DocTimeRelAnnotator extends
     	  }
       } else {
 //        String outcome = this.classifier.classify(features);
+        Map<String,Double> scores = this.classifier.score(features);
         Map.Entry<String, Double> maxEntry = null;
-        for( Map.Entry<String, Double> entry: this.classifier.score(features).entrySet()
){
+        for( Map.Entry<String, Double> entry: scores.entrySet() ){
         	if(maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0){
         		maxEntry = entry;
         	}
         }
         
+        if (probViewname != null){
+          Map<String,Double> probs = SoftMaxUtil.getDistributionFromScores(scores);
+          try {
+            JCas probView = jCas.getView(probViewname);
+            for(String label : probs.keySet()){
+              EventMention mention = new EventMention(probView);
+              mention.setId(eventMention.getId());
+              mention.setConfidence(probs.get(label).floatValue());
+              Event event = new Event(probView);
+              EventProperties props = new EventProperties(probView);
+              props.setDocTimeRel(label);
+              event.setProperties(props);
+              mention.setEvent(event);
+              mention.addToIndexes();
+            }
+          } catch (CASException e) {
+            e.printStackTrace();
+            throw new AnalysisEngineProcessException(e);
+          }
+          
+        }
+        
         if (eventMention.getEvent() == null) {
           Event event = new Event(jCas);
           eventMention.setEvent(event);

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/EventTimeSelfRelationAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/EventTimeSelfRelationAnnotator.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/EventTimeSelfRelationAnnotator.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/EventTimeSelfRelationAnnotator.java
Wed Jan 20 17:23:10 2016
@@ -238,13 +238,13 @@ public class EventTimeSelfRelationAnnota
 			}
 		}
 
-		if(category!=null){
-			if(!((EventMention)arg1).getClass().equals(EventMention.class)){
-				System.out.println("find system-event relations: "+ arg1.getCoveredText() + " -"+category+"-
" + arg2.getCoveredText());
-			}else{
-				System.out.println("find gold-event relations: "+ arg1.getCoveredText() + " -"+category+"-
" + arg2.getCoveredText());
-			}
-		}
+//		if(category!=null){
+//			if(!((EventMention)arg1).getClass().equals(EventMention.class)){
+//				System.out.println("find system-event relations: "+ arg1.getCoveredText() + " -"+category+"-
" + arg2.getCoveredText());
+//			}else{
+//				System.out.println("find gold-event relations: "+ arg1.getCoveredText() + " -"+category+"-
" + arg2.getCoveredText());
+//			}
+//		}
 
 		if (category == null && coin.nextDouble() <= this.probabilityOfKeepingANegativeExample)
{
 			category = NO_RELATION_CATEGORY;

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/THYMEAnaforaXMLReader.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/THYMEAnaforaXMLReader.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/THYMEAnaforaXMLReader.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/THYMEAnaforaXMLReader.java
Wed Jan 20 17:23:10 2016
@@ -150,6 +150,10 @@ public class THYMEAnaforaXMLReader exten
       throw new AnalysisEngineProcessException(e);
     }
 
+    int curEventId = 1;
+    int curTimexId = 1;
+    int curRelId = 1;
+    
     for (Element annotationsElem : dataElem.getChildren("annotations")) {
 
       Map<String, Annotation> idToAnnotation = Maps.newHashMap();
@@ -214,6 +218,7 @@ public class THYMEAnaforaXMLReader exten
           event.setMentions(new FSArray(jCas, 1));
           event.setMentions(0, eventMention);
           event.addToIndexes();
+          eventMention.setId(curEventId++);
           eventMention.setConfidence(1.0f);
           eventMention.setDiscoveryTechnique(CONST.NE_DISCOVERY_TECH_GOLD_ANNOTATION);
           eventMention.setEvent(event);
@@ -223,18 +228,21 @@ public class THYMEAnaforaXMLReader exten
         } else if (type.equals("TIMEX3")) {
           String timeClass = removeSingleChildText(propertiesElem, "Class", id);
           TimeMention timeMention = new TimeMention(jCas, begin, end);
+          timeMention.setId(curTimexId++);
           timeMention.setTimeClass(timeClass);
           timeMention.addToIndexes();
           annotation = timeMention;
 
         } else if (type.equals("DOCTIME")) {
           TimeMention timeMention = new TimeMention(jCas, begin, end);
+          timeMention.setId(curTimexId++);
           timeMention.setTimeClass(type);
           timeMention.addToIndexes();
           annotation = timeMention;
 
         } else if (type.equals("SECTIONTIME")) {
           TimeMention timeMention = new TimeMention(jCas, begin, end);
+          timeMention.setId(curTimexId++);
           timeMention.setTimeClass(type);
           timeMention.addToIndexes();
           annotation = timeMention;
@@ -278,6 +286,7 @@ public class THYMEAnaforaXMLReader exten
           String targetID = removeSingleChildText(propertiesElem, "Target", id);
           String tlinkType = removeSingleChildText(propertiesElem, "Type", id);
           TemporalTextRelation relation = new TemporalTextRelation(jCas);
+          relation.setId(curRelId++);
           addRelation(jCas, relation, sourceID, targetID, tlinkType, idToAnnotation, id);
 
         } else if (type.equals("ALINK")) {

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/TemporalRelationExtractorAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/TemporalRelationExtractorAnnotator.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/TemporalRelationExtractorAnnotator.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/TemporalRelationExtractorAnnotator.java
Wed Jan 20 17:23:10 2016
@@ -33,12 +33,14 @@ import org.apache.ctakes.relationextract
 import org.apache.ctakes.relationextractor.ae.features.PhraseChunkingExtractor;
 import org.apache.ctakes.relationextractor.ae.features.RelationFeaturesExtractor;
 import org.apache.ctakes.relationextractor.ae.features.TokenFeaturesExtractor;
+import org.apache.ctakes.temporal.utils.SoftMaxUtil;
 import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
 import org.apache.ctakes.typesystem.type.relation.RelationArgument;
 import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
 import org.apache.uima.UimaContext;
 import org.apache.uima.UimaContextAdmin;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CASException;
 import org.apache.uima.fit.descriptor.ConfigurationParameter;
 import org.apache.uima.fit.util.JCasUtil;
 import org.apache.uima.jcas.JCas;
@@ -58,6 +60,10 @@ public abstract class TemporalRelationEx
 
   public static final String NO_RELATION_CATEGORY = "-NONE-";
 
+  public static final String PARAM_PROB_VIEW = "ProbView";
+  @ConfigurationParameter(name=PARAM_PROB_VIEW, mandatory=false)
+  private String probViewname = null;
+
   public static final String PARAM_PROBABILITY_OF_KEEPING_A_NEGATIVE_EXAMPLE =
       "ProbabilityOfKeepingANegativeExample";
 
@@ -201,9 +207,10 @@ public abstract class TemporalRelationEx
         // annotations
         else {
 //          String predictedCategory = this.classify(features);
+          Map<String,Double> scores = this.classifier.score(features);
           
           Map.Entry<String, Double> maxEntry = null;
-          for( Map.Entry<String, Double> entry: this.classifier.score(features).entrySet()
){
+          for( Map.Entry<String, Double> entry: scores.entrySet() ){
           	if(maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0){
           		maxEntry = entry;
           	}
@@ -217,7 +224,7 @@ public abstract class TemporalRelationEx
           }
           
           // add a relation annotation if a true relation was predicted
-          if (!predictedCategory.equals(NO_RELATION_CATEGORY)) {
+          if (predictedCategory != null && !predictedCategory.equals(NO_RELATION_CATEGORY))
{
 
             // if we predict an inverted relation, reverse the order of the
             // arguments
@@ -230,6 +237,19 @@ public abstract class TemporalRelationEx
 
             createRelation(jCas, arg1, arg2, predictedCategory, confidence);
           }
+          if(probViewname != null){
+            try {
+              JCas probView = jCas.getView(probViewname);
+              Map<String,Double> probs = SoftMaxUtil.getDistributionFromScores(scores);
+              
+              for(String label : probs.keySet()){
+                createRelation(probView, arg1, arg2, label, probs.get(label));
+              }
+            } catch (CASException e) {
+              e.printStackTrace();
+              throw new AnalysisEngineProcessException(e);
+            }
+          }
         }
       } // end pair in pairs
     } // end for(Sentence)

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/EvaluationOfBothEEAndETRelations.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/EvaluationOfBothEEAndETRelations.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/EvaluationOfBothEEAndETRelations.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/EvaluationOfBothEEAndETRelations.java
Wed Jan 20 17:23:10 2016
@@ -34,18 +34,16 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.ctakes.relationextractor.eval.RelationExtractorEvaluation.HashableArguments;
+import org.apache.ctakes.temporal.ae.DocTimeRelAnnotator;
 import org.apache.ctakes.temporal.ae.EventEventRelationAnnotator;
 import org.apache.ctakes.temporal.ae.EventTimeSelfRelationAnnotator;
+import org.apache.ctakes.temporal.ae.TemporalRelationExtractorAnnotator;
 //import org.apache.ctakes.temporal.ae.EventTimeSyntacticAnnotator;
 //import org.apache.ctakes.temporal.ae.EventTimeRelationAnnotator;
 //import org.apache.ctakes.temporal.ae.EventEventRelationAnnotator;
 import org.apache.ctakes.temporal.ae.baselines.RecallBaselineEventTimeRelationAnnotator;
-import org.apache.ctakes.temporal.eval.EvaluationOfEventEventThymeRelations.AddEEPotentialRelations;
-import org.apache.ctakes.temporal.eval.EvaluationOfEventTimeRelations.AddPotentialRelations;
 import org.apache.ctakes.temporal.eval.EvaluationOfEventTimeRelations.Overlap2Contains;
 import org.apache.ctakes.temporal.eval.EvaluationOfEventTimeRelations.ParameterSettings;
-import org.apache.ctakes.temporal.eval.EvaluationOfTemporalRelations_ImplBase.RemoveGoldAttributes;
-import org.apache.ctakes.temporal.eval.EvaluationOfTemporalRelations_ImplBase.RemoveNonContainsRelations;
 //import org.apache.ctakes.temporal.eval.Evaluation_ImplBase.WriteI2B2XML;
 //import org.apache.ctakes.temporal.eval.Evaluation_ImplBase.XMLFormat;
 import org.apache.ctakes.temporal.utils.AnnotationIdCollection;
@@ -57,14 +55,17 @@ import org.apache.ctakes.typesystem.type
 import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
 import org.apache.ctakes.typesystem.type.textsem.TimeMention;
 import org.apache.ctakes.typesystem.type.textspan.Sentence;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.CASException;
 import org.apache.uima.collection.CollectionReader;
 import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.component.ViewCreatorAnnotator;
 import org.apache.uima.fit.descriptor.ConfigurationParameter;
 import org.apache.uima.fit.factory.AggregateBuilder;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.factory.ConfigurationParameterFactory;
 import org.apache.uima.fit.pipeline.JCasIterator;
 import org.apache.uima.fit.pipeline.SimplePipeline;
 import org.apache.uima.fit.util.JCasUtil;
@@ -110,6 +111,9 @@ EvaluationOfTemporalRelations_ImplBase{
 
 		@Option
 		public boolean getSkipTrain();
+		
+		@Option
+		public boolean getWriteProbabilities();
 	}
 
 	//  protected static boolean DEFAULT_BOTH_DIRECTIONS = false;
@@ -178,6 +182,7 @@ EvaluationOfTemporalRelations_ImplBase{
 					params);
 			//			evaluation.prepareXMIsFor(patientSets);
 			if(options.getI2B2Output()!=null) evaluation.setI2B2Output(options.getI2B2Output() + "/temporal-relations/both");
+			if(options.getAnaforaOutput()!=null) evaluation.anaforaOutput = options.getAnaforaOutput();
 			List<Integer> training = trainItems;
 			List<Integer> testing = null;
 			if(options.getTest()){
@@ -193,6 +198,8 @@ EvaluationOfTemporalRelations_ImplBase{
 			}else{
 				evaluation.prepareXMIsFor(patientSets);
 			}
+			evaluation.writeProbabilities = options.getWriteProbabilities();
+			
 			params.stats = evaluation.trainAndTest(training, testing);//training);//
 			//      System.err.println(options.getKernelParams() == null ? params : options.getKernelParams());
 //			System.err.println("No closure on gold::Closure on System::Recall Mode");
@@ -232,6 +239,7 @@ EvaluationOfTemporalRelations_ImplBase{
 	protected boolean useGoldAttributes;
 	protected boolean skipTrain=false;
 	//  protected boolean printRelations = false;
+  private boolean writeProbabilities = false;
 
 	public EvaluationOfBothEEAndETRelations(
 			File baseDirectory,
@@ -354,6 +362,11 @@ EvaluationOfTemporalRelations_ImplBase{
 			throws Exception {
 		this.useClosure=false;
 		AggregateBuilder aggregateBuilder = this.getPreprocessorAggregateBuilder();
+    aggregateBuilder.add( AnalysisEngineFactory.createEngineDescription(
+        ViewCreatorAnnotator.class,
+        ViewCreatorAnnotator.PARAM_VIEW_NAME,
+        PROB_VIEW_NAME ) );
+
 		aggregateBuilder.add(CopyFromGold.getDescription(EventMention.class, TimeMention.class));
 
 		aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(
@@ -389,12 +402,33 @@ EvaluationOfTemporalRelations_ImplBase{
 				GOLD_VIEW_NAME);
 
 		aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(RemoveRelations.class));
-		aggregateBuilder.add(this.baseline ? RecallBaselineEventTimeRelationAnnotator.createAnnotatorDescription(directory)
:
-			EventEventRelationAnnotator.createAnnotatorDescription(new File(directory,"event-event")));
-		aggregateBuilder.add(EventTimeSelfRelationAnnotator.createEngineDescription(new File(directory,"event-time")));
+		AnalysisEngineDescription aed = this.baseline ? RecallBaselineEventTimeRelationAnnotator.createAnnotatorDescription(directory)
:
+      EventEventRelationAnnotator.createAnnotatorDescription((new File(directory,"event-event/model.jar")).getAbsolutePath());
+    if(this.writeProbabilities){
+      ConfigurationParameterFactory.addConfigurationParameter(aed, 
+          TemporalRelationExtractorAnnotator.PARAM_PROB_VIEW, 
+          PROB_VIEW_NAME);
+    }
+		aggregateBuilder.add(aed);
+		aed = EventTimeSelfRelationAnnotator.createEngineDescription(new File(directory,"event-time/model.jar").getAbsolutePath());
+		if(this.writeProbabilities){
+		  ConfigurationParameterFactory.addConfigurationParameter(aed, 
+		      TemporalRelationExtractorAnnotator.PARAM_PROB_VIEW, 
+		      PROB_VIEW_NAME);
+		}
+		aggregateBuilder.add(aed);
+		
+		aed = DocTimeRelAnnotator.createAnnotatorDescription(new File("target/eval/event-properties/train_and_test/docTimeRel/model.jar").getAbsolutePath());
+		if(this.writeProbabilities){
+		  ConfigurationParameterFactory.addConfigurationParameters(
+		      aed,    
+		      DocTimeRelAnnotator.PARAM_PROB_VIEW, 
+		      PROB_VIEW_NAME);
+		}
+		aggregateBuilder.add(aed);
 
-		if(this.i2b2Output != null){
-			aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(WriteAnaforaXML.class,
WriteAnaforaXML.PARAM_OUTPUT_DIR, this.i2b2Output), "TimexView", CAS.NAME_DEFAULT_SOFA);
+		if(this.anaforaOutput != null){
+			aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(WriteAnaforaXML.class,
WriteAnaforaXML.PARAM_OUTPUT_DIR, this.anaforaOutput, WriteAnaforaXML.PARAM_PROB_VIEW, PROB_VIEW_NAME),
"TimexView", CAS.NAME_DEFAULT_SOFA);
 		}
 
 		File outf = null;

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java?rev=1725761&r1=1725760&r2=1725761&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java
Wed Jan 20 17:23:10 2016
@@ -116,6 +116,8 @@ public abstract class Evaluation_ImplBas
 
    public static final String GOLD_VIEW_NAME = "GoldView";
 
+   public static final String PROB_VIEW_NAME = "ProbView";
+   
    public enum XMLFormat {Knowtator, Anafora, I2B2}
 
    public enum Subcorpus {Colon, Brain, DeepPhe}
@@ -172,6 +174,9 @@ public abstract class Evaluation_ImplBas
 
       @Option( defaultToNull = true )
       public String getI2B2Output();
+      
+      @Option( defaultToNull = true )
+      public String getAnaforaOutput();
 
       @Option
       public boolean getSkipTrain();
@@ -220,6 +225,8 @@ public abstract class Evaluation_ImplBas
 
    protected String i2b2Output = null;
 
+   protected String anaforaOutput = null; 
+   
    protected String[] kernelParams;
 
    public Evaluation_ImplBase(
@@ -990,6 +997,10 @@ public abstract class Evaluation_ImplBas
       public static final String PARAM_OUTPUT_DIR = "PARAM_OUTPUT_DIR";
       @ConfigurationParameter( mandatory = true, description = "Output directory to write
xml files to.", name = PARAM_OUTPUT_DIR )
       protected String outputDir;
+      
+      public static final String PARAM_PROB_VIEW = "ProbView";
+      @ConfigurationParameter(name=PARAM_PROB_VIEW, mandatory=false)
+      public String probViewname = null;
 
       @Override
       public void process( JCas jcas ) throws AnalysisEngineProcessException {
@@ -1003,6 +1014,12 @@ public abstract class Evaluation_ImplBas
                outDir.mkdirs();
             }
 
+            
+            // get maps from ids to entities and relations:
+            JCas probView = jcas.getView(probViewname);
+            Map<Integer, List<EventMention>> mentions = probViewname == null?
null : getMentionIdMap(jcas, probView);
+            Map<String, List<TemporalTextRelation>> rels = probViewname == null
? null : getRelationIdMap(jcas, probView);
+            
             // build the xml
             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
@@ -1046,7 +1063,20 @@ public abstract class Evaluation_ImplBas
                   //add properties
                   Element property = doc.createElement( "properties" );
                   Element docTimeRE = doc.createElement( "DocTimeRel" );
-                  docTimeRE.setTextContent( event.getEvent().getProperties().getDocTimeRel()
);
+                  String dtrContent = null;
+                  if(probViewname == null){
+                    dtrContent = event.getEvent().getProperties().getDocTimeRel();
+                  }else{
+                    StringBuffer buff = new StringBuffer();
+                    for(EventMention probMention : mentions.get(event.getId())){
+                      buff.append(probMention.getEvent().getProperties().getDocTimeRel());
+                      buff.append(':');
+                      buff.append(probMention.getConfidence());
+                      buff.append("::");
+                    }
+                    dtrContent = buff.substring(0, buff.length()-3);
+                  }
+                  docTimeRE.setTextContent( dtrContent );
                   Element eventTypeE = doc.createElement( "Type" );
                   eventTypeE.setTextContent( "N/A" );
                   Element degreeE = doc.createElement( "Degree" );
@@ -1130,11 +1160,26 @@ public abstract class Evaluation_ImplBas
                Element property = doc.createElement( "properties" );
 
                Annotation arg1 = rel.getArg1().getArgument();
+               Annotation arg2 = rel.getArg2().getArgument();
                Element sourceE = doc.createElement( "Source" );
                sourceE.setTextContent( argToId.get( arg1 ) );
                Element relTypeE = doc.createElement( "Type" );
-               relTypeE.setTextContent( rel.getCategory() );
-               Annotation arg2 = rel.getArg2().getArgument();
+               String relContent = null;
+               if(probViewname == null){
+                 relContent = rel.getCategory();
+               }else{
+                 String key = getRelationId(rel);
+                 StringBuffer buff = new StringBuffer();
+                 for(TemporalTextRelation probRel : rels.get(key)){
+                   buff.append(probRel.getCategory());
+                   buff.append(':');
+                   buff.append(probRel.getConfidence());
+                   buff.append("::");
+                 }
+                 relContent = buff.substring(0, buff.length()-3);
+               }
+               
+               relTypeE.setTextContent( relContent );
                Element targetE = doc.createElement( "Target" );
                targetE.setTextContent( argToId.get( arg2 ) );
 
@@ -1163,17 +1208,70 @@ public abstract class Evaluation_ImplBas
             StreamResult result = new StreamResult( new File( outDir, outFile + ".xml" )
);
             transformer.transform( source, result );
          } catch ( ParserConfigurationException e ) {
-            e.printStackTrace();
-            throw new AnalysisEngineProcessException( e );
+           e.printStackTrace();
+           throw new AnalysisEngineProcessException( e );
          } catch ( TransformerConfigurationException e ) {
-            e.printStackTrace();
-            throw new AnalysisEngineProcessException( e );
+           e.printStackTrace();
+           throw new AnalysisEngineProcessException( e );
          } catch ( TransformerException e ) {
-            e.printStackTrace();
-            throw new AnalysisEngineProcessException( e );
-         }
+           e.printStackTrace();
+           throw new AnalysisEngineProcessException( e );
+         } catch (CASException e) {
+           e.printStackTrace();
+           throw new AnalysisEngineProcessException( e );
+        }
 
       }
-
+      
+      private static Map<Integer, List<EventMention>> getMentionIdMap(JCas jcas,
JCas probView){
+        HashMap<Integer, List<EventMention>> map = new HashMap<>();
+        
+        for(EventMention mention : JCasUtil.select(jcas, EventMention.class)){
+          List<EventMention> variations = new ArrayList<>();
+          for(EventMention probMention : JCasUtil.select(probView, EventMention.class)){
+            if(mention.getId() == probMention.getId()){
+              variations.add(probMention);
+            }
+          }
+          map.put(mention.getId(), variations);
+        }
+        return map;
+      }
+      
+      private static Map<String, List<TemporalTextRelation>> getRelationIdMap(JCas
jcas, JCas probView){
+        HashMap<String, List<TemporalTextRelation>> map = new HashMap<>();
+        
+        for(TemporalTextRelation rel : JCasUtil.select(jcas, TemporalTextRelation.class)){
+          List<TemporalTextRelation> variations = new ArrayList<>();
+          String idStr = getRelationId(rel);
+          for(TemporalTextRelation probRel : JCasUtil.select(probView, TemporalTextRelation.class)){
+            String probStr = getRelationId(probRel);            
+            if(idStr.equals(probStr)){
+              variations.add(probRel);
+            }
+          }
+          map.put(idStr, variations);
+        }
+        
+        return map;
+      }
+   }
+   public static String getRelationId(TemporalTextRelation rel){
+     StringBuffer buffer = new StringBuffer();
+     if(rel.getArg1().getArgument().getClass().getSimpleName().equals("EventMention")){
+       buffer.append('e');
+     }else{
+       buffer.append('t');
+     }
+     buffer.append(((IdentifiedAnnotation)rel.getArg1().getArgument()).getId());
+     buffer.append(':');
+     if(rel.getArg2().getArgument().getClass().getSimpleName().equals("EventMention")){
+       buffer.append('e');
+     }else{
+       buffer.append('t');
+     }
+     buffer.append(((IdentifiedAnnotation)rel.getArg2().getArgument()).getId());
+     return buffer.toString();     
    }
 }
+                                                           

Added: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/utils/SoftMaxUtil.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/utils/SoftMaxUtil.java?rev=1725761&view=auto
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/utils/SoftMaxUtil.java
(added)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/utils/SoftMaxUtil.java
Wed Jan 20 17:23:10 2016
@@ -0,0 +1,45 @@
+package org.apache.ctakes.temporal.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class SoftMaxUtil {
+
+   public static <X> Map<X,Double> getDistributionFromScores(Map<X,Double>
scores){
+     Map<X,Double> dists = new HashMap<>();
+     if(isDistribution(scores)){
+       return scores;
+     }
+     
+     double sum = 0.0;
+     for(Map.Entry<X, Double> entry : scores.entrySet()){
+       double val = entry.getValue();
+       double adjusted = 1.0 / (1.0 + Math.exp(-val));
+       dists.put(entry.getKey(), adjusted);
+       sum += adjusted;
+     }
+     
+     for(X key : scores.keySet()){
+       dists.put(key, dists.get(key) / sum);
+     }
+     
+     return dists;
+   }
+   
+   public static <X> boolean isDistribution(Map<X,Double> scores){
+      double sum = 0.0;
+      for(Double val : scores.values()){
+        if(val < 0){
+          return false;
+        }
+        sum += val;
+      }
+      
+      // check if the sum is close to 1:
+      if(Math.abs(sum - 1.0) < 0.01){
+        return true;
+      }
+      
+      return false;
+   }
+}



Mime
View raw message