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.data.analysis;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 import  org.cleartk.util.ViewURIUtil;
 
Print gold standard relations and their context.

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

  
Print gold standard relations and their context.

Author(s):
dmitriy dligach
 
   public static class RelationContextPrinter extends JCasAnnotator_ImplBase {
     
     @Override
     public void process(JCas jCasthrows AnalysisEngineProcessException {
       
       JCas goldView;
       try {
         goldView = jCas.getView("GoldView");
       } catch (CASException e) {
         throw new AnalysisEngineProcessException(e);
       }
       
       JCas systemView;
       try {
        systemView = jCas.getView("_InitialView");
      } catch (CASException e) {
        throw new AnalysisEngineProcessException(e);
      }
      // can't iterate over binary text relations in a sentence, so need
      // a lookup from pair of annotations to binary text relation
      Map<List<Annotation>, BinaryTextRelationrelationLookup = new HashMap<>();
      for(BinaryTextRelation relation : JCasUtil.select(goldViewBinaryTextRelation.class)) {
        Annotation arg1 = relation.getArg1().getArgument();
        Annotation arg2 = relation.getArg2().getArgument();
        relationLookup.put(Arrays.asList(arg1arg2), relation);
      }
      File noteFile = new File(ViewURIUtil.getURI(jCas).toString());
      String fileName = noteFile.getName();
      
      for(Sentence sentence : JCasUtil.select(systemViewSentence.class)) {
        List<StringformattedRelationsInSentence = new ArrayList<>();
        List<EventMentioneventMentionsInSentence = JCasUtil.selectCovered(goldViewEventMention.classsentence);
        List<TimeMentiontimeMentionsInSentence = JCasUtil.selectCovered(goldViewTimeMention.classsentence);
        
        // retrieve event-time relations in this sentece
        for(EventMention eventMention : eventMentionsInSentence) {
          for(TimeMention timeMention : timeMentionsInSentence) {
            // there are relations where arg1=time and arg2=event
            BinaryTextRelation timeEventRel = relationLookup.get(Arrays.asList(timeMentioneventMention));
            if(timeEventRel != null) {
              String text = String.format("%s(%s, %s)"timeEventRel.getCategory(), timeMention.getCoveredText(), eventMention.getCoveredText());
              formattedRelationsInSentence.add(text);
            } 
            // and relations where arg1=event and arg2=time
            BinaryTextRelation eventTimeRel = relationLookup.get(Arrays.asList(eventMentiontimeMention));
            if(eventTimeRel != null) {
              String text = String.format("%s(%s, %s)"eventTimeRel.getCategory(), eventMention.getCoveredText(), timeMention.getCoveredText());
              formattedRelationsInSentence.add(text);
            }
          }
        }
        
        // retrieve event-event relations in this sentence
        for(EventMention mention1 : eventMentionsInSentence) {
          for(EventMention mention2 : eventMentionsInSentence) {
            if(mention1 == mention2) {
              continue;
            }
            BinaryTextRelation relation = relationLookup.get(Arrays.asList(mention1mention2));
            if(relation != null) {
              String text = String.format("%s(%s, %s)"relation.getCategory(), mention1.getCoveredText(), mention2.getCoveredText());
              formattedRelationsInSentence.add(text);
            }
          }
        }
        
        if(formattedRelationsInSentence.size() > 0) {
          ..println(fileName + ": " + sentence.getCoveredText());
          for(String text : formattedRelationsInSentence) {
            ..println(text);
          }
          ..println();
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X