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;
 
 
Print events of given UMLS semantic type.

Author(s):
dmitriy dligach
 
 public class EventPrinterPipeline {
 
   static interface Options {
 
     @Option(
         description = "specify the path to the directory containing the xmi files")
     public File getInputDirectory();
 
     @Option(
         description = "specify the UMLS semantic type (e.g. 5, i.e. procedure)")
     public int getUmlsSemanticType();
 
     @Option(
         description = "specify the path to the output file")
     public File getEventOutputFile();
   }
   
 	public static void main(String[] argsthrows Exception {
 		
 		Options options = CliFactory.parseArguments(Options.classargs);
 
 		List<FiletrainFiles = Arrays.asList(options.getInputDirectory().listFiles());
     CollectionReader collectionReader = getCollectionReader(trainFiles);
 		
     AnalysisEngine annotationConsumer = AnalysisEngineFactory.createEngine(
     		EventWriter.class,
     		"UmlsSemanticType",
     		options.getUmlsSemanticType(),
     		"EventOutputFile",
     		options.getEventOutputFile());
     		
 		SimplePipeline.runPipeline(collectionReaderannotationConsumer);
 	}

Print events with a given UMLS semantic type.
 
 	public static class EventWriter extends JCasAnnotator_ImplBase {
 
	      name = "EventOutputFile",
	      mandatory = true,
	      description = "path to the output file that will store the events")
	  private String eventOutputFile;
	      name = "UmlsSemanticType",
	      mandatory = true,
	      description = "umls semantic type of interest")
	  private int umlsSemanticType;
	  
	  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 eventWriter = getWriter(true);
	    try {
	      for(EventMention eventMention : JCasUtil.select(goldViewEventMention.class)) {
	        List<EventMentioncoveringSystemEventMentions = JCasUtil.selectCovered(
	            systemView
	            EventMention.class
	            eventMention.getBegin(), 
	            eventMention.getEnd());
	        
	        for(EventMention systemEventMention : coveringSystemEventMentions) {
	          if(systemEventMention.getTypeID() == ) { 
	            String output = String.format(
	                "%s|%s\n"
	                systemEventMention.getCoveredText().toLowerCase(),
	                expandToNP(systemVieweventMention).toLowerCase());
	            try {
	              eventWriter.write(output);
	            } catch (IOException e) {
	              throw new AnalysisEngineProcessException(e);
	            }
	          }
	        }
	      }
	    } finally {
	      try {
	        eventWriter.close();
	      } catch (IOException e) {
	        throw new AnalysisEngineProcessException(e);
	      }
	    }
	  }
	}
  public 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;
  }
  
	public 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);
	}
	public static String expandToNP(JCas jCasIdentifiedAnnotation identifiedAnnotation) {
	  // map each covering treebank node to its character length
	  Map<TreebankNodeIntegertreebankNodeSizes = new HashMap<TreebankNodeInteger>();
	  for(TreebankNode treebankNode : JCasUtil.selectCovering(
	      jCas
	      TreebankNode.class
	      identifiedAnnotation.getBegin(), 
	      identifiedAnnotation.getEnd())) {
	    // only expand nouns (and not verbs or adjectives)
	    if(treebankNode instanceof TerminalTreebankNode) {
	      if(! treebankNode.getNodeType().startsWith("N")) {
	        return identifiedAnnotation.getCoveredText();
	      }
	    }
	    // because only nouns are expanded, look for covering NPs
	    if(treebankNode.getNodeType().equals("NP")) {
	      treebankNodeSizes.put(treebankNodetreebankNode.getCoveredText().length());
	    }
	  }
	  // find the shortest covering treebank node
	  List<TreebankNodesortedTreebankNodes = new ArrayList<TreebankNode>(treebankNodeSizes.keySet());
	  Function<TreebankNodeIntegergetValue = Functions.forMap(treebankNodeSizes);
	  Collections.sort(sortedTreebankNodes, Ordering.natural().onResultOf(getValue));
	  if(sortedTreebankNodes.size() > 0) {
	    return sortedTreebankNodes.get(0).getCoveredText();
	  } 
	  return identifiedAnnotation.getCoveredText();
	}
New to GrepCode? Check out our FAQ X