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.duration;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
Analyze duration information for the relation arguments of CONTAINS relation.

Author(s):
dmitriy dligach
 
 public class EventTimeDurationStatistics {
 
   static interface Options {
 
     @Option(longName = "xmi-dir")
     public File getInputDirectory();
 
     @Option(longName = "patients")
 
     @Option(longName = "output-file")
     public File getOutputFile();
   }
 
   public static void main(String[] argsthrows Exception {
 
     Options options = CliFactory.parseArguments(Options.classargs);
 
     List<IntegerpatientSets = options.getPatients().getList();
     List<IntegertrainItems = THYMEData.getPatientSets(patientSets.);
     List<FiletrainFiles = Utils.getFilesFor(trainItemsoptions.getInputDirectory());
     CollectionReader collectionReader = Utils.getCollectionReader(trainFiles);
 
     AnalysisEngine annotationConsumer = AnalysisEngineFactory.createEngine(
         AnalyseRelationArgumentDuration.class,
         "OutputFile",
         options.getOutputFile());
 
     SimplePipeline.runPipeline(collectionReaderannotationConsumer);
   }

  
Look at those event-time relations whose event argument has duration data and whose time argument can be normalized using Bethard timex normalizer.
 
   public static class AnalyseRelationArgumentDuration extends JCasAnnotator_ImplBase {                                               
 
         name = "OutputFile",
         mandatory = true,
         description = "path to the file that stores relation data")
     private String outputFile;
 
     public static final String GOLD_VIEW_NAME = "GoldView";
 
    @Override                                                                                                                  
    public void process(JCas jCasthrows AnalysisEngineProcessException {                                                     
      File durationLookup = new File(.);                      
      Map<StringMap<StringFloat>> textToDistribution = null;                                                                 
      try {                                                                                                                      
        textToDistribution = Files.readLines(durationLookup.new Utils.Callback());                                    
      } catch(IOException e) {                                                                                                   
        e.printStackTrace();                                                                                                     
        return;                                                                                                                  
      }  
      JCas goldView;                                                                                                           
      try {                                                                                                                    
        goldView = jCas.getView();                                                                               
      } catch (CASException e) {                                                                                               
        throw new AnalysisEngineProcessException(e);                                                                           
      }                                                                                                                                                                                                                                         
      // find event-time relations where both arguments have duration information
      for(BinaryTextRelation relation : Lists.newArrayList(JCasUtil.select(goldViewBinaryTextRelation.class))) {
        if(! relation.getCategory().equals("CONTAINS")) {
          continue;
        }
        
        RelationArgument arg1 = relation.getArg1();                                                                             
        RelationArgument arg2 = relation.getArg2(); 
        String eventText;
        String timeText;
        if(arg1.getArgument() instanceof TimeMention && arg2.getArgument() instanceof EventMention) {
          timeText = arg1.getArgument().getCoveredText().toLowerCase(); 
          eventText = Utils.normalizeEventText(jCasarg2.getArgument());
        } else if(arg1.getArgument() instanceof EventMention && arg2.getArgument() instanceof TimeMention) {
          eventText = Utils.normalizeEventText(jCasarg1.getArgument());
          timeText = arg2.getArgument().getCoveredText().toLowerCase();  
        } else {
          // this is not a event-time relation
          continue;
        }    
        HashSet<StringtimeUnits = Utils.getTimeUnits(timeText);
        if(textToDistribution.containsKey(eventText) && timeUnits.size() > 0) {
          // there is duration information and we are able to get time units
          Map<StringFloateventDistribution = textToDistribution.get(eventText);
          Map<StringFloattimeDistribution = Utils.convertToDistribution(timeUnits.iterator().next());
          float eventExpectedDuration = Utils.expectedDuration(eventDistribution);
          float timeExpectedDuration = Utils.expectedDuration(timeDistribution);
          String context = Utils.getTextBetweenAnnotations(goldViewarg1.getArgument(), arg2.getArgument());
          String out = String.format("%s|%.5f|%s|%.5f|%s\n"
              timeUnits.iterator().next(), timeExpectedDuration * 3650, 
              eventTexteventExpectedDuration * 3650, 
              context.length() < 80 ? context : "...");
          try {
            Files.append(outnew File(), .);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X