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.util.List;
 import java.util.Map;
 
 import  org.cleartk.eval.AnnotationStatistics;
 import  org.cleartk.ml.crfsuite.CrfSuiteStringOutcomeDataWriter;
 import  org.cleartk.ml.jar.JarClassifierBuilder;
 
 
   public static int nFolds = 5;
   private List<IntegerallTrain = null;
   private boolean skipTrainComponents = false;
   
   interface MetaOptions extends Options {
     @Option
     boolean getSkipTrainComponents();  
   }
 
   public EvaluationOfMetaTimeExpressionExtractor(File baseDirectory,
       File rawTextDirectoryFile xmlDirectory,
       Subcorpus subcorpusFile xmiDirectoryFile treebankDirectory,
       List<IntegerallTrainClass<? extends AnnotationannotationClass) {
     super(baseDirectoryrawTextDirectoryxmlDirectoryxmlFormatsubcorpusxmiDirectory,
         treebankDirectoryannotationClass);
     this. = allTrain;
   }
 
   public static void main(String[] argsthrows Exception {
     MetaOptions options = CliFactory.parseArguments(MetaOptions.classargs);
     List<IntegerpatientSets = options.getPatients().getList();
     List<IntegertrainItems = null;
     List<IntegerdevItems = null;
     List<IntegertestItems = null;
     if(options.getXMLFormat() == .){
       trainItems = I2B2Data.getTrainPatientSets(options.getXMLDirectory());
       devItems = I2B2Data.getDevPatientSets(options.getXMLDirectory());
       testItems = I2B2Data.getTestPatientSets(options.getXMLDirectory());
     }else{
       trainItems = THYMEData.getPatientSets(patientSetsoptions.getTrainRemainders().getList());
       devItems = THYMEData.getPatientSets(patientSetsoptions.getDevRemainders().getList());
       testItems = THYMEData.getPatientSets(patientSetsoptions.getTestRemainders().getList());
     }
     List<IntegerallTrain = new ArrayList<>(trainItems);
     List<IntegerallTest = null;
     
     if(options.getTest()){
       allTrain.addAll(devItems);
       allTest = new ArrayList<>(testItems);
     }else{
       allTest = new ArrayList<>(devItems);
     }
 
            new File("target/eval/time-spans"), 
            options.getRawTextDirectory(), 
            options.getXMLDirectory(), 
            options.getXMLFormat(),
            options.getSubcorpus(),
            options.getXMIDirectory(), 
            options.getTreebankDirectory(),
            allTrain,
            TimeMention.class);
    if(options.getSkipTrainComponents()) eval.setSkipTrainComponents(true);
    if(options.getI2B2Output()!=nulleval.setI2B2Output(options.getI2B2Output());
    if(options.getPrintOverlappingSpans()) eval.printOverlapping = true;
    AnnotationStatistics<Stringstats = eval.trainAndTest(allTrainallTest);
    ..println(stats.toString());
  }
  private void setSkipTrainComponents(boolean skip) {
    this. = skip;
  }
  protected void train(CollectionReader collectionReaderFile directory)
      throws Exception {
    
    if(!this.){
      Class<? extends JCasAnnotator_ImplBase>[] annotatorClasses = MetaTimeAnnotator.getComponents();
      // add more annotator types?
      Map<Class<? extends JCasAnnotator_ImplBase>, String[]> annotatorTrainingArguments = Maps.newHashMap();
      annotatorTrainingArguments.put(BackwardsTimeAnnotator.classnew String[]{"-c""0.1"});
      annotatorTrainingArguments.put(TimeAnnotator.classnew String[]{"-c""0.1"});
      annotatorTrainingArguments.put(ConstituencyBasedTimeAnnotator.classnew String[]{"-c""0.3"});
      annotatorTrainingArguments.put(CRFTimeAnnotator.classnew String[]{"-p""c2=0.3"});
      JCasIterator[] casIters = new JCasIterator[];
      for (int fold = 0; fold < ; ++fold) {
        List<IntegerxfoldTrain = selectTrainItems(fold);
        List<IntegerxfoldTest = selectTestItems(fold);
        AggregateBuilder aggregateBuilder = this.getPreprocessorAggregateBuilder();
        File modelDirectory = getModelDirectory(new File("target/eval/time-spans/fold_"+fold));
        for (Class<? extends JCasAnnotator_ImplBaseannotatorClass : annotatorClasses) {
          EvaluationOfTimeSpans evaluation = new EvaluationOfTimeSpans(
              new File("target/eval/time-spans/" ),
              this.,
              this.,
              this.,
              this.,
              this.,
              this.,
              1,
              0,
              annotatorClass,
              false,
              annotatorTrainingArguments.get(annotatorClass));
          evaluation.prepareXMIsFor();
          String name = String.format("%s.errors"annotatorClass.getSimpleName());
          evaluation.setLogging(.new File("target/eval"name));
          // train on 4 of the folds of the training data:
          evaluation.train(evaluation.getCollectionReader(xfoldTrain), modelDirectory);
          if(fold == 0){
            // train the main model as well:
            evaluation.train(evaluation.getCollectionReader(), directory);
          }
        }
        casIters[fold] = new JCasIterator(getCollectionReader(xfoldTest), aggregateBuilder.createAggregate());
      }
      // run meta data-writer for this fold:
      AggregateBuilder writerBuilder = new AggregateBuilder();
      writerBuilder.add(CopyFromGold.getDescription(TimeMention.class));
      writerBuilder.add(this.getDataWriterDescription(directory));
      AnalysisEngine writer = writerBuilder.createAggregate();
      for(JCasIterator casIter : casIters){
        while(casIter.hasNext()){
          JCas jcas = casIter.next();
          SimplePipeline.runPipeline(jcaswriter);
        }
      }
      writer.collectionProcessComplete();
    }
    JarClassifierBuilder.trainAndPackage(getModelDirectory(directory), new String[]{"-p""c2=3.0"});
  }
  
  private static List<IntegerselectTrainItems(List<Integeritemsint numFoldsint fold) {
    List<IntegertrainItems = new ArrayList<>();
    for (int i = 0; i < items.size(); ++i) {
      if (i % numFolds != fold) {
        trainItems.add(items.get(i));
      }
    }
    return trainItems;
  }
  
  private static List<IntegerselectTestItems(List<Integeritemsint numFoldsint fold) {
    List<IntegertrainItems = new ArrayList<>();
    for (int i = 0; i < items.size(); ++i) {
      if (i % numFolds == fold) {
        trainItems.add(items.get(i));
      }
    }
    return trainItems;
  }
  
      throws ResourceInitializationException {
    return MetaTimeAnnotator.getDataWriterDescription(CrfSuiteStringOutcomeDataWriter.classdirectory);          
  }
  protected void trainAndPackage(File directorythrows Exception {
    ..println("\n\n***\n\n\nChanging the classifier setup\n\n\n");
    JarClassifierBuilder.trainAndPackage(getModelDirectory(directory), "-p""c2=0.3");
  }
      throws ResourceInitializationException {
    return MetaTimeAnnotator.getAnnotatorDescription(directory);
  }
  protected Collection<? extends AnnotationgetGoldAnnotations(JCas jCas,
      Segment segment) {
    return selectExact(jCasTimeMention.classsegment);
  }
  protected Collection<? extends AnnotationgetSystemAnnotations(JCas jCas,
      Segment segment) {
    return selectExact(jCasTimeMention.classsegment);
  }
  private static File getModelDirectory(File directory) {
    return new File(directory"MetaTimeAnnotator");
  }
New to GrepCode? Check out our FAQ X