All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gate.copyAS2AnoDoc.CopyAS2AnoDocMain Maven / Gradle / Ivy

There is a newer version: 8.5
Show newest version
/*
 *  CopyAS2AnoDocMain.java
 * 
 *  Yaoyong Li 08/10/2007
 *
 *  $Id: CopyAS2AnoDocMain.java, v 1.0 2009-05-10 11:44:16 +0000 yaoyong $
 */
package gate.copyAS2AnoDoc;

import gate.Annotation;
import gate.AnnotationSet;
import gate.Document;
import gate.Factory;
import gate.ProcessingResource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;
import gate.creole.metadata.CreoleParameter;
import gate.creole.metadata.CreoleResource;
import gate.creole.metadata.Optional;
import gate.creole.metadata.RunTime;
import gate.util.ExtensionFileFilter;
import gate.util.Files;
import gate.util.InvalidOffsetException;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

@CreoleResource(name="Copy Anns to Another Doc PR", comment="Copy the annotations from one document to another document", helpURL="http://gate.ac.uk/userguide/sec:misc-creole:copyAS2AnoDoc")
public class CopyAS2AnoDocMain extends AbstractLanguageAnalyser implements
ProcessingResource {

  private static final long serialVersionUID = -2874636782869415162L;

  URL sourceFilesURL = null;
  private String inputASName;
  private String outputASName;
  private List annotationTypes;

  File[] xmlFiles = null;
  boolean [] fileNotUsedYet = null;  

  /** Initialise this resource, and return it. */
  public gate.Resource init() throws ResourceInstantiationException {
    return this;
  } // init()

  /**
   * Run the resource.
   * 
   * @throws ExecutionException if an error occurs while running the PR
   */
  public void execute() throws ExecutionException {
    
    //  now we need to see if the corpus is provided
    if(corpus == null)
      throw new ExecutionException("Provided corpus is null!");

    if(corpus.size() == 0)
      throw new ExecutionException("No Document found in corpus!");
    
    int positionDoc = corpus.indexOf(document);

    // first document in the corpus
    
    if(positionDoc == 0) {
      System.out.println("\n\n------------ new session starts ------------\n");
      System.out.println("Copy the Annotation Set "+inputASName +" from the files in"
        +sourceFilesURL.getPath() + " to the files in the corpus as AS "+ outputASName);
      
      //collect all the file names in the source dir
      ExtensionFileFilter fileFilter = null;
      xmlFiles = Files.fileFromURL(this.sourceFilesURL)
        .listFiles(fileFilter);
      Arrays.sort(xmlFiles, new Comparator() {
        public int compare(File a, File b) {
          return a.getName().compareTo(b.getName());
        }
      });
      fileNotUsedYet = new boolean[xmlFiles.length];
      for(int i=0; i 0) {
        //String [] annTypes = annotationTypes.split(";");
        asToCopy = sourceAS.get(new HashSet(annotationTypes));
        //asToCopy = sourceAS.get();
      } else {
        // transfer everything
        asToCopy = sourceAS.get();
      }
      System.out.println("Copying from "+xmlFiles[filePos].getName() +" to "+ 
        document.getName());
      
      //get the target annotation set
      AnnotationSet targetAS = null;
      if(outputASName == null || outputASName.length()==0)
        targetAS = document.getAnnotations();
      else
        targetAS = document.getAnnotations(outputASName);
      
      //copy the annotations from source file to target file
      for(Object obj:asToCopy) {
        Annotation oneAnn = (Annotation)obj;
        targetAS.add(oneAnn.getStartNode().getOffset(), oneAnn.getEndNode().getOffset(),
          oneAnn.getType(), oneAnn.getFeatures());
      }
      
      Factory.deleteResource(docCorres);

    }
    catch(ResourceInstantiationException e) {
      e.printStackTrace();
    }
    catch(MalformedURLException e) {
      e.printStackTrace();
    }
    catch(InvalidOffsetException e) {
      e.printStackTrace();
    }
   
    
  }
  
  private int findCorresFile(String fileName, File [] xmlFiles, boolean [] notUsedYet) {
    int fpos =-1;
    int sameLenMax = 0;
    char [] fnChars = fileName.toCharArray();
    for(int i=0; ifnXmlFChars.length)
        lenStr = fnXmlFChars.length;
      int sameLen = lenStr;
      for(int j=0; j getAnnotationTypes() {
    return this.annotationTypes;
  }

  @RunTime
  @Optional
  @CreoleParameter(comment="The name of the annotation types for copying")
  public void setAnnotationTypes(List newTypes) {
    annotationTypes = newTypes;
  }

}
 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy