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;
 
 
Print gold standard relations and their context.

Author(s):
dmitriy dligach
 
   
   static interface Options {
 
     @Option(longName = "xmi-dir")
     public File getInputDirectory();
     
     @Option(longName = "output-file")
     public File getOutputFile();
 
     @Option(longName = "patients")
   }
   
 	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 = getFilesFor(trainItemsoptions.getInputDirectory());
     CollectionReader collectionReader = getCollectionReader(trainFiles);
 		
     AnalysisEngine annotationConsumer = AnalysisEngineFactory.createEngine(
     		RelationContextPrinter.class,
     		"OutputFile",
     		options.getOutputFile());
     		
 		SimplePipeline.runPipeline(collectionReaderannotationConsumer);
 	}
 	  
 	private static CollectionReader getCollectionReader(List<FileinputFilesthrows Exception {
 
 	  List<StringfileNames = new ArrayList<String>();
 	  for(File file : inputFiles) {
 	    if(! (file.isHidden())) {
 	      fileNames.add(file.getPath());
 	    }
 	  }
 
 	  String[] paths = new String[fileNames.size()];
 	  fileNames.toArray(paths);
 
 	  return CollectionReaderFactory.createReader(
 	      XMIReader.class,
	      paths);
	}
	private static List<FilegetFilesFor(List<IntegerpatientSetsFile inputDirectory) {
	  
	  List<Filefiles = new ArrayList<File>();
	  
	  for (Integer set : patientSets) {
	    final int setNum = set;
	    for (File file : inputDirectory.listFiles(new FilenameFilter(){
	      @Override
	      public boolean accept(File dirString name) {
	        return name.contains(String.format("ID%03d"setNum));
	      }})) {
	      // skip hidden files like .svn
	      if (!file.isHidden()) {
	        files.add(file);
	      } 
	    }
	  }
	  
	  return files;
	}

  
Print gold standard relations and their context.

Author(s):
dmitriy dligach
  public static class RelationContextPrinter extends JCasAnnotator_ImplBase {
        name = "OutputFile",
        mandatory = true,
        description = "path to the file that stores relation data")
    private String outputFile;
    
    @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);
      }
      BufferedWriter writer = getWriter(true);
      try {
        for(BinaryTextRelation binaryTextRelation : JCasUtil.select(goldViewBinaryTextRelation.class)) {
          boolean sameSentence = false;
          Annotation arg1 = binaryTextRelation.getArg1().getArgument();
          Annotation arg2 = binaryTextRelation.getArg2().getArgument();
          String category = binaryTextRelation.getCategory();
          String text = getTextBetweenAnnotations(systemViewarg1arg2);
          List<Sentencesents1 = JCasUtil.selectCovering(systemViewSentence.classarg1.getBegin(), arg1.getEnd());
          List<Sentencesents2 = JCasUtil.selectCovering(systemViewSentence.classarg2.getBegin(), arg2.getEnd());
          if(sents1.size() == 1 && sents2.size() == 1){
            if(sents1.get(0) == sents2.get(0)){
              sameSentence = true;
            }
          }else{
            ..println("Could not find covering sent for relation: " + String.format("%s|%s|%s|%s\n"categoryarg1.getCoveredText(), arg2.getCoveredText(), text));
          }
          
          String output = String.format("%s|%s|%s|%s|%s|%s|%s\n"categoryarg1.getCoveredText(), arg2.getCoveredText(), textarg1.getType().toString(), arg2.getType().toString(), sameSentence ? "same" : "different");
      
          try {
            writer.write(output);
          } catch (IOException e) {
            throw new AnalysisEngineProcessException(e);
          }
        }
      } finally {      
        try {
          writer.close();
        } catch (IOException e) {
          throw new AnalysisEngineProcessException(e);
        }
      }
    }
    
    private static String getTextBetweenAnnotations(JCas jCasAnnotation arg1Annotation arg2) {
      
      final int windowSize = 15;
      
      String text = jCas.getDocumentText();
      int leftArgBegin = Math.min(arg1.getBegin(), arg2.getBegin());
      int rightArgEnd = Math.max(arg1.getEnd(), arg2.getEnd());
      int begin = Math.max(0, leftArgBegin - windowSize);
      int end = Math.min(text.length(), rightArgEnd + windowSize); 
      
      return text.substring(beginend).replaceAll("[\r\n]"" ");
    }
    
    private static BufferedWriter getWriter(String filePathboolean append) {
      BufferedWriter bufferedWriter = null;
      try {
        FileWriter fileWriter = new FileWriter(filePathappend);
        bufferedWriter = new BufferedWriter(fileWriter);
      } catch (IOException e) {
        e.printStackTrace();
      }
      return bufferedWriter;
    }
  }
New to GrepCode? Check out our FAQ X