Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.ctakes.temporal.eval;
 
 import java.io.File;
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.cleartk.eval.AnnotationStatistics;
 import  org.cleartk.ml.jar.JarClassifierBuilder;
 import  org.cleartk.ml.libsvm.LibSvmStringOutcomeDataWriter;
 import  org.cleartk.util.ViewUriUtil;
 
 
 public class EvaluationOfEventEventRelations extends
 
 	private boolean baseline = false;
   public EvaluationOfEventEventRelations(      
       File baseDirectory,
       File rawTextDirectory,
       File xmlDirectory,
       XMLFormat xmlFormat,
       Subcorpus subcorpus,
       File xmiDirectory,
       File treebankDirectory,
       boolean printErrors,
       boolean printRelations,
       boolean baseline,
       ParameterSettings params){
     super(baseDirectoryrawTextDirectoryxmlDirectoryxmlFormatsubcorpusxmiDirectory,
         treebankDirectoryprintErrorsprintRelationsparams);
     this. = baseline;
   }
 
   @Override
   protected void train(CollectionReader collectionReaderFile directorythrows Exception
        {
     AggregateBuilder aggregateBuilder = this.getPreprocessorAggregateBuilder();
     aggregateBuilder.add(CopyFromGold.getDescription(EventMention.classTimeMention.classBinaryTextRelation.class));
     //	    aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(MergeContainsOverlap.class));
     aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(RemoveNonContainsRelations.class));
     aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(RemoveCrossSentenceRelations.class));
     // TODO -- see if this applies to this relation:
     //	    if (this.useClosure) {
     //	      aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(AddTransitiveContainsRelations.class));
     //	    }
     aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(PreserveEventEventRelations.class));
     aggregateBuilder.add(EventEventRelationAnnotator.createDataWriterDescription(
         LibSvmStringOutcomeDataWriter.class,
 //        	        TKSVMlightStringOutcomeDataWriter.class,
         directory,
         1.0));
     SimplePipeline.runPipeline(collectionReaderaggregateBuilder.createAggregate());
 
     HideOutput hider = new HideOutput();
    // libsvm:
    JarClassifierBuilder.trainAndPackage(directory,  "-t""0""-c""10");
    // tksvmlight with no tk features:
//    JarClassifierBuilder.trainAndPackage(directory, "-t", "0", "-c", "10", "-N", "0");
//    JarClassifierBuilder.trainAndPackage(directory,  "-t", "5", "-S", "0", "-N", "3", "-C", "+", "-T", "1.0");
    hider.restoreOutput();
  }
  protected AnnotationStatistics<Stringtest(
      CollectionReader collectionReaderFile directorythrows Exception {
    AggregateBuilder aggregateBuilder = this.getPreprocessorAggregateBuilder();
    aggregateBuilder.add(CopyFromGold.getDescription(EventMention.classTimeMention.class));
    //	    aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(MergeContainsOverlap.class,
    //	    		MergeContainsOverlap.PARAM_RELATION_VIEW,
    //	    		GOLD_VIEW_NAME));
    aggregateBuilder.add(
        AnalysisEngineFactory.createEngineDescription(RemoveNonContainsRelations.class),
        .,
        );
    aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(
        RemoveCrossSentenceRelations.class,
        .,
        ));
    // TODO - use if relevant.
    //	    if (this.useClosure) {
    //	      aggregateBuilder.add(
    //	          AnalysisEngineFactory.createEngineDescription(AddTransitiveContainsRelations.class),
    //	          CAS.NAME_DEFAULT_SOFA,
    //	          GOLD_VIEW_NAME);
    //	    }
    aggregateBuilder.add(
        AnalysisEngineFactory.createEngineDescription(PreserveEventEventRelations.class),
        .,
        );
    aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(RemoveRelations.class));
    aggregateBuilder.add(
        EventEventRelationAnnotator.createAnnotatorDescription(directory));
      public HashableArguments apply(BinaryTextRelation relation) {
        return new HashableArguments(relation);
      }
    };
    Function<BinaryTextRelationStringgetOutcome = AnnotationStatistics.annotationToFeatureValue("category");
    AnnotationStatistics<Stringstats = new AnnotationStatistics<String>();
    for(Iterator<JCascasIter = new JCasIterator(collectionReaderaggregateBuilder.createAggregate()); casIter.hasNext();){
      JCas jCas = casIter.next();
      JCas goldView = jCas.getView();
      JCas systemView = jCas.getView(.);
      Collection<BinaryTextRelationgoldRelations = JCasUtil.select(
          goldView,
          BinaryTextRelation.class);
      Collection<BinaryTextRelationsystemRelations = JCasUtil.select(
          systemView,
          BinaryTextRelation.class);
      stats.add(goldRelationssystemRelationsgetSpangetOutcome);
      if(this.){
        URI uri = ViewUriUtil.getURI(jCas);
        String[] path = uri.getPath().split("/");
        printRelationAnnotations(path[path.length - 1], systemRelations);
      }
      if(this.){
        Map<HashableArgumentsBinaryTextRelationgoldMap = Maps.newHashMap();
        for (BinaryTextRelation relation : goldRelations) {
          goldMap.put(new HashableArguments(relation), relation);
        }
        Map<HashableArgumentsBinaryTextRelationsystemMap = Maps.newHashMap();
        for (BinaryTextRelation relation : systemRelations) {
          systemMap.put(new HashableArguments(relation), relation);
        }
        Set<HashableArgumentsall = Sets.union(goldMap.keySet(), systemMap.keySet());
        List<HashableArgumentssorted = Lists.newArrayList(all);
        Collections.sort(sorted);
        for (HashableArguments key : sorted) {
          BinaryTextRelation goldRelation = goldMap.get(key);
          BinaryTextRelation systemRelation = systemMap.get(key);
          if (goldRelation == null) {
            ..println("System added: " + formatRelation(systemRelation));
          } else if (systemRelation == null) {
            ..println("System dropped: " + formatRelation(goldRelation));
          } else if (!systemRelation.getCategory().equals(goldRelation.getCategory())) {
            String label = systemRelation.getCategory();
            ..printf("System labeled %s for %s\n"labelformatRelation(goldRelation));
          } else{
            ..println("Nailed it! " + formatRelation(systemRelation));
          }
        }
      }
    }
    return stats;
  }
  public static void main(String[] argsthrows Exception{
    TempRelOptions options = CliFactory.parseArguments(TempRelOptions.classargs);
    List<IntegerpatientSets = options.getPatients().getList();
    List<IntegertrainItems = getTrainItems(options);
    List<IntegertestItems = getTestItems(options);
    File workingDir = new File("target/eval/temporal-relations/event-event/");
    ParameterSettings params = ;
        workingDir,
        options.getRawTextDirectory(),
        options.getXMLDirectory(),
        options.getXMLFormat(),
        options.getSubcorpus(),
        options.getXMIDirectory(),
        options.getTreebankDirectory(),
        options.getPrintErrors(),
        options.getPrintFormattedRelations(),
        options.getBaseline(),
        params);
    evaluation.prepareXMIsFor(patientSets);
    AnnotationStatistics<Stringstats = evaluation.trainAndTest(trainItemstestItems);
    ..println(stats);
  }
New to GrepCode? Check out our FAQ X