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.ae;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 import  org.cleartk.util.ViewURIUtil;
 import  org.cleartk.util.cr.UriCollectionReader;
 
 
 public class THYMEAnaforaXMLReader extends JCasAnnotator_ImplBase {
   private static Logger LOGGER = Logger.getLogger(THYMEAnaforaXMLReader.class);
 
   public static final String PARAM_ANAFORA_DIRECTORY = "anaforaDirectory";
 
       name = ,
       description = "root directory of the Anafora-annotated files, with one subdirectory for "
           + "each annotated file")
   private File anaforaDirectory;
 
   public static final String PARAM_ANAFORA_XML_SUFFIXES = "anaforaSuffixes";
 
       name = ,
       description = "list of suffixes that might be added to a file name to identify the Anafora "
           + "XML annotations file; only the first suffix corresponding to a file will be used")
   private String[] anaforaXMLSuffixes = new String[] {
       ".Gold_Temporal_Entities.xml",
       ".Gold_Temporal_Relations.xml",
       ".temporal.Temporal-Adjudication.gold.completed.xml",
       ".temporal.Temporal-Entities.gold.completed.xml",
       ".Temporal-Entity.gold.completed.xml",
       ".Temporal-Relation-Adjudication.gold.completed.xml",
       ".Temporal-Entity-Adjudication.gold.completed.xml",
       ".Temporal-Relation.gold.completed.xml"};
 
     return AnalysisEngineFactory.createPrimitiveDescription(THYMEAnaforaXMLReader.class);
   }
 
   public static AnalysisEngineDescription getDescription(File anaforaDirectory)
       throws ResourceInitializationException {
     return AnalysisEngineFactory.createPrimitiveDescription(
         THYMEAnaforaXMLReader.class,
         anaforaDirectory);
   }
 
   @Override
   public void process(JCas jCasthrows AnalysisEngineProcessException {
     // determine source text file
    File textFile = new File(ViewURIUtil.getURI(jCas));
    .info("processing " + textFile);
    // determine possible Anafora XML file names
    List<FilepossibleXMLFiles = Lists.newArrayList();
    for (String anaforaXMLSuffix : this.) {
      if (this. == null) {
        possibleXMLFiles.add(new File(textFile + anaforaXMLSuffix));
      } else {
        File subDir = new File(this.textFile.getName());
        possibleXMLFiles.add(new File(subDirtextFile.getName() + anaforaXMLSuffix));
      }
    }
    // find an Anafora XML file that actually exists
    File xmlFile = null;
    for (File possibleXMLFile : possibleXMLFiles) {
      if (possibleXMLFile.exists()) {
        xmlFile = possibleXMLFile;
        break;
      }
    }
    if (xmlFile == null) {
      throw new IllegalArgumentException("no Anafora XML file found from " + possibleXMLFiles);
    }
    // load the XML
    Element dataElem;
    try {
      dataElem = new SAXBuilder().build(xmlFile.toURI().toURL()).getRootElement();
    } catch (MalformedURLException e) {
      throw new AnalysisEngineProcessException(e);
    } catch (JDOMException e) {
      throw new AnalysisEngineProcessException(e);
    } catch (IOException e) {
      throw new AnalysisEngineProcessException(e);
    }
    for (Element annotationsElem : dataElem.getChildren("annotations")) {
      Map<StringAnnotationidToAnnotation = Maps.newHashMap();
      for (Element entityElem : annotationsElem.getChildren("entity")) {
        String id = removeSingleChildText(entityElem"id"null);
        Element spanElem = removeSingleChild(entityElem"span"id);
        String type = removeSingleChildText(entityElem"type"id);
        Element propertiesElem = removeSingleChild(entityElem"properties"id);
        // UIMA doesn't support disjoint spans, so take the span enclosing
        // everything
        int begin = .;
        int end = .;
        for (String spanString : spanElem.getText().split(";")) {
          String[] beginEndStrings = spanString.split(",");
          if (beginEndStrings.length != 2) {
            error("span not of the format 'number,number'"id);
          }
          int spanBegin = Integer.parseInt(beginEndStrings[0]);
          int spanEnd = Integer.parseInt(beginEndStrings[1]);
          if (spanBegin < begin) {
            begin = spanBegin;
          }
          if (spanEnd > end) {
            end = spanEnd;
          }
        }
        Annotation annotation;
        if (type.equals("EVENT")) {
          String docTimeRel = removeSingleChildText(propertiesElem"DocTimeRel"id);
          if (docTimeRel == null) {
            error("no docTimeRel, assuming OVERLAP"id);
            docTimeRel = "OVERLAP";
          }
          String eventType = removeSingleChildText(propertiesElem"Type"id);
          String degree = removeSingleChildText(propertiesElem"Degree"id);
          String polarity = removeSingleChildText(propertiesElem"Polarity"id);
          String contextualModality = removeSingleChildText(propertiesElem"ContextualModality"id);
          String contextualAspect = removeSingleChildText(propertiesElem"ContextualAspect"id);
          String permanence = removeSingleChildText(propertiesElem"Permanence"id);
          EventMention eventMention = new EventMention(jCasbeginend);
          Event event = new Event(jCas);
          EventProperties eventProperties = new EventProperties(jCas);
          eventProperties.setDocTimeRel(docTimeRel);
          eventProperties.setCategory(eventType);
          eventProperties.setDegree(degree);
          if (polarity.equals("POS")) {
            eventProperties.setPolarity(.);
          } else if (polarity.equals("NEG")) {
            eventProperties.setPolarity(.);
          } else {
            error("polarity that was not POS or NEG"id);
          }
          eventProperties.setContextualModality(contextualModality);
          eventProperties.setContextualAspect(contextualAspect);
          eventProperties.setPermanence(permanence);
          eventProperties.addToIndexes();
          event.setConfidence(1.0f);
          event.setProperties(eventProperties);
          event.setMentions(new FSArray(jCas, 1));
          event.setMentions(0, eventMention);
          event.addToIndexes();
          eventMention.setConfidence(1.0f);
          eventMention.setEvent(event);
          eventMention.addToIndexes();
          annotation = eventMention;
        } else if (type.equals("TIMEX3")) {
          String timeClass = removeSingleChildText(propertiesElem"Class"id);
          TimeMention timeMention = new TimeMention(jCasbeginend);
          timeMention.setTimeClass(timeClass);
          timeMention.addToIndexes();
          annotation = timeMention;
        } else if (type.equals("DOCTIME")) {
          TimeMention timeMention = new TimeMention(jCasbeginend);
          timeMention.setTimeClass(type);
          timeMention.addToIndexes();
          annotation = timeMention;
        } else if (type.equals("SECTIONTIME")) {
          TimeMention timeMention = new TimeMention(jCasbeginend);
          timeMention.setTimeClass(type);
          timeMention.addToIndexes();
          annotation = timeMention;
        } else {
          throw new UnsupportedOperationException("unsupported entity type: " + type);
        }
        // match the annotation to it's ID for later use
        idToAnnotation.put(idannotation);
        // make sure all XML has been consumed
        removeSingleChild(entityElem"parentsType"id);
        if (!propertiesElem.getChildren().isEmpty() || !entityElem.getChildren().isEmpty()) {
          List<Stringchildren = Lists.newArrayList();
          for (Element child : propertiesElem.getChildren()) {
            children.add(child.getName());
          }
          for (Element child : entityElem.getChildren()) {
            children.add(child.getName());
          }
          error("unprocessed children " + childrenid);
        }
      }
      for (Element relationElem : annotationsElem.getChildren("relation")) {
        String id = removeSingleChildText(relationElem"id"null);
        String type = removeSingleChildText(relationElem"type"id);
        Element propertiesElem = removeSingleChild(relationElem"properties"id);
        if (type.equals("TLINK")) {
          String sourceID = removeSingleChildText(propertiesElem"Source"id);
          String targetID = removeSingleChildText(propertiesElem"Target"id);
          String tlinkType = removeSingleChildText(propertiesElem"Type"id);
          TemporalTextRelation relation = new TemporalTextRelation(jCas);
          addRelation(jCasrelationsourceIDtargetIDtlinkTypeidToAnnotationid);
        } else if (type.equals("ALINK")) {
          String sourceID = removeSingleChildText(propertiesElem"Source"id);
          String targetID = removeSingleChildText(propertiesElem"Target"id);
          String alinkType = removeSingleChildText(propertiesElem"Type"id);
          AspectualTextRelation relation = new AspectualTextRelation(jCas);
          addRelation(jCasrelationsourceIDtargetIDalinkTypeidToAnnotationid);
        } else {
          throw new UnsupportedOperationException("unsupported relation type: " + type);
        }
        // make sure all XML has been consumed
        removeSingleChild(relationElem"parentsType"id);
        if (!propertiesElem.getChildren().isEmpty() || !relationElem.getChildren().isEmpty()) {
          List<Stringchildren = Lists.newArrayList();
          for (Element child : propertiesElem.getChildren()) {
            children.add(child.getName());
          }
          for (Element child : relationElem.getChildren()) {
            children.add(child.getName());
          }
          error("unprocessed children " + childrenid);
        }
      }
    }
  }
  private static Element getSingleChild(Element elemString elemNameString causeID) {
    List<Elementchildren = elem.getChildren(elemName);
    if (children.size() != 1) {
      error(String.format("not exactly one '%s' child"elemName), causeID);
    }
    return children.size() > 0 ? children.get(0) : null;
  }
  private static Element removeSingleChild(Element elemString elemNameString causeID) {
    Element child = getSingleChild(elemelemNamecauseID);
    elem.removeChildren(elemName);
    return child;
  }
  private static String removeSingleChildText(Element elemString elemNameString causeID) {
    Element child = getSingleChild(elemelemNamecauseID);
    String text = child.getText();
    if (text.isEmpty()) {
      error(String.format("an empty '%s' child"elemName), causeID);
      text = null;
    }
    elem.removeChildren(elemName);
    return text;
  }
  private static void addRelation(
      JCas jCas,
      BinaryTextRelation relation,
      String sourceID,
      String targetID,
      String category,
      Map<StringAnnotationidToAnnotation,
      String causeID) {
    if (sourceID != null && targetID != null) {
      Annotation source = getArgument(sourceIDidToAnnotationcauseID);
      Annotation target = getArgument(targetIDidToAnnotationcauseID);
      if (source != null && target != null) {
        RelationArgument sourceArg = new RelationArgument(jCas);
        sourceArg.setArgument(source);
        sourceArg.addToIndexes();
        RelationArgument targetArg = new RelationArgument(jCas);
        targetArg.setArgument(target);
        targetArg.addToIndexes();
        relation.setCategory(category);
        relation.setArg1(sourceArg);
        relation.setArg2(targetArg);
        relation.addToIndexes();
      }
    }
  }
  private static Annotation getArgument(
      String id,
      Map<StringAnnotationidToAnnotation,
      String causeID) {
    Annotation annotation = idToAnnotation.get(id);
    if (annotation == null) {
      error("no annotation with id " + idcauseID);
    }
    return annotation;
  }
  private static void error(String foundString id) {
    .error(String.format("found %s in annotation with ID %s"foundid));
  }
  public static void main(String[] argsthrows Exception {
    List<Filefiles = Lists.newArrayList();
    for (String path : args) {
      files.add(new File(path));
    }
    CollectionReader reader = UriCollectionReader.getCollectionReaderFromFiles(files);
    AnalysisEngine engine = AnalysisEngineFactory.createPrimitive(THYMEAnaforaXMLReader.class);
    SimplePipeline.runPipeline(readerengine);
  }
New to GrepCode? Check out our FAQ X